110,534
社区成员
发帖
与我相关
我的任务
分享
protected override CreateParams CreateParams {
get
{
CreateParams cp = base.CreateParams;
cp.Style |= 0x00040000; //这个40000就是昨天查出来的值,给窗体加个可变边框
return cp;
}
}
现在他就可以自动对齐,不过我测试的效果是上面大概有个3像素的白条。so,还需要改进。
既然明白他跟窗体是否可调整有关,那么我们就考虑另一个消息
WM_NCCALCSIZE
当必须计算窗口的客户区域的大小和位置时发送。通过处理这个消息,当窗口的大小或位置改变时,应用程序可以控制窗口的客户区域的内容。
也就是你没有边框的情况,window Form不在处理这个消息了。所以你需要自己处理
ok,查一下资料
https://blog.csdn.net/ljfth/article/details/6955800
果然问题在这个WM_NCCALCSIZE 上。不过这个WM_NCCALCSIZE 的参数有点麻烦,我懒得继续实验下去了
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SetWindowLongW(Handle, -16, (IntPtr)(0x00040000L | 0x00020000L | 0x00010000L));
}
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetWindowLongW(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetWindowLongW(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SendMessageW(IntPtr hWnd, int msg, IntPtr wParam = default, IntPtr lParam = default);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("dwmapi.dll", SetLastError = true)]
public static extern IntPtr DwmExtendFrameIntoClientArea(IntPtr hWnd, int[] margins);
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x0001:
{
SetWindowPos(Handle, IntPtr.Zero, Left, Top, Width, Height, 0x0020);
}
break;
case 0x0006:
var rs = DwmExtendFrameIntoClientArea(Handle, new int[] { 3, 3, 3, 3 });
break;
case 0x0085:
break;
case 0x0201:
SendMessageW(Handle, 0x0112, (IntPtr)(0xF010 + 2));
break;
case 0x0083:
{
SetWindowPos(Handle, IntPtr.Zero, Left, Top, Width, Height, 0x0020);
}
break;
}
base.WndProc(ref m);
}
}
}
UnsafeNativeMethods.SendMessage( CriticalHandle, WindowMessage.WM_SYSCOMMAND, (IntPtr)NativeMethods.SC_MOUSEMOVE, IntPtr.Zero);
UnsafeNativeMethods.SendMessage( CriticalHandle, WindowMessage.WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);