[收藏]winform拖动方法

参考:http://topic.csdn.net/u/20080330/14/6b26bc14-0932-4016-9704-6f012ef04f66.html

 

方案1:通过重载消息处理实现。(优点:不用声明api函数。缺点是:窗体上放着Panel等容器就会失效) 

C# code
const int WM_NCHITTEST = 0x0084;
const int HTLEFT = 10;
const int HTCAPTION = 2;
const int HTCLIENT = 1;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case WM_NCHITTEST:
if (m.Result == (IntPtr)HTCLIENT)
m.Result
= (IntPtr)HTCAPTION;
break;
}
}



方案2:发送一个拖动的消息(优点是:可以放在任意控件鼠标按下事件中) 
C# code
using System.Runtime.InteropServices;

[DllImport(
"User32.DLL")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport(
"User32.DLL")]
public static extern bool ReleaseCapture();
public const uint WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 61456;
public const int HTCAPTION = 2;

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE
| HTCAPTION, 0);
}


 

posted on 2009-03-26 12:25  散步的蠕虫  阅读(586)  评论(0编辑  收藏  举报

导航