C# 控件拖动

时间:2024-11-09 16:05:08

https://zhidao.baidu.com/question/2116834779399609987.html

[DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
public static extern void ReleaseCapture();
[DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern void SendMessage(int hwnd, int wMsg, int wParam, int lParam);

把上面语句加载窗体类文件中,然后在拖拽的控件上加事件,比如我是一个panel

        private void panelForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage((int)移动的对象.Handle, 0xA1, 2, 0);
                //是窗体自身就是
                //SendMessage((int)this.Handle, 0xA1, 2, 0);
                //panel就是SendMessage((int)panel1.Handle, 0xA1, 2, 0);
            }
        }

这种才是windows标准的拖拽事件,下面那位,拖动的时候窗体会闪烁不停,一直在渲染背景