[转]C#鼠标拖动任意控件

时间:2024-08-30 11:37:02
分类: c#2011-08-15 22:51 178人阅读 评论(0) 收藏 举报
         using System.Runtime.InteropServices;
//并为控件 添加 MouseDown 事件 // C#鼠标拖动任意控件 // 利用Windows的API函数:SendMessage 和 ReleaseCapture
const uint WM_SYSCOMMAND = 0x0112;
const uint SC_MOVE = 0xF010;
const uint HTCAPTION = 0x0002; [DllImport("user32.dll", EntryPoint = "SendMessageA")]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, uint wParam, uint lParam);
[DllImport("user32.dll")]
private static extern int ReleaseCapture(); void ControlMouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage((sender as Control).Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, );
}

来自:https://www.cnblogs.com/yangruiGB2312/p/5823943.html

[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;
/// <summary>
/// 为了是主界面能够移动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, );
}