C#移动无标题栏窗体的四种代码

第一种采用,需注意窗体上的控件是否把窗体覆盖了。。。MouseDown、MouseMove、MouseUp事件应该是鼠标所处位置最顶层的控件的事件
在窗体的类中声明两个变量
private Point mouseOffset; //记录鼠标指针的坐标
private bool isMouseDown = false; //记录鼠标按键是否按下

创建该窗体 MouseDown、MouseMove、MouseUp事件的相应处理程序
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    int xOffset;
    int yOffset;
    if (e.Button == MouseButtons.Left) 
    {
        xOffset = -e.X ;
        yOffset = -e.Y ;
        mouseOffset = new Point(xOffset, yOffset);
        isMouseDown = true;
    }
}

private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (isMouseDown) 
    {
        Point mousePos = Control.MousePosition;
        mousePos.Offset(mouseOffset.X, mouseOffset.Y);
        Location = mousePos;
    }
}

private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
    // 修改鼠标状态isMouseDown的值
    // 确保只有鼠标左键按下并移动时,才移动窗体
    if (e.Button == MouseButtons.Left) 
    {
        isMouseDown = false;
    }
}

第二种调用API 未验证
        using System.Runtime.InteropServices;

[DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
       
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

第三种未验证 
        private bool isMouseDown = false;
        private Point FormLocation; //form的location
        private Point mouseOffset; //鼠标的按下位置
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();

[DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

private const int WM_SYSCOMMAND = 0x0112;//点击窗口左上角那个图标时的系统信息
        private const int SC_MOVE = 0xF010;//移动信息
        private const int HTCAPTION = 0x0002;//表示鼠标在窗口标题栏时的系统信息
        private const int WM_NCHITTEST = 0x84;//鼠标在窗体客户区(除了标题栏和边框以外的部分)时发送的消息
        private const int HTCLIENT = 0x1;//表示鼠标在窗口客户区的系统消息
        private const int SC_MAXIMIZE = 0xF030;//最大化信息
        private const int SC_MINIMIZE = 0xF020;//最小化信息

protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_SYSCOMMAND:
                    if (m.WParam == (IntPtr)SC_MAXIMIZE)
                    {
                        m.WParam = (IntPtr)SC_MINIMIZE;
                    }
                    break;
                case WM_NCHITTEST: //如果鼠标移动或单击
                    base.WndProc(ref m);//调用基类的窗口过程——WndProc方法处理这个消息
                    if (m.Result == (IntPtr)HTCLIENT)//如果返回的是HTCLIENT
                    {
                        m.Result = (IntPtr)HTCAPTION;//把它改为HTCAPTION
                        return;//直接返回退出方法
                    }
                    break;
            }
            base.WndProc(ref m);//如果不是鼠标移动或单击消息就调用基类的窗口过程进行处理
        }

最后一种,最简单:

protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0201)
{
m.Msg = 0x00A1;//更改消息为非客户区按下鼠标
m.LParam = IntPtr.Zero;
m.WParam = new IntPtr(2);//鼠标放在标题栏内
}
base.WndProc(ref m);
}

总结:这三种都可以实现窗体移动,但是发现光移动,没有重绘了,不知道怎么解决这个问题

上一篇:Android卸载程序之后跳转到指定的反馈页面


下一篇:android的程序运行数据存放在哪里?