随笔 - 750  文章 - 1  评论 - 107  阅读 - 34万

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

 

C#鼠标拖动任意控件(winform)

分类: c#
复制代码
 1         using System.Runtime.InteropServices;  
 2         //并为控件 添加 MouseDown 事件 
 3 
 4         // C#鼠标拖动任意控件 
 5 
 6         // 利用Windows的API函数:SendMessage 和 ReleaseCapture 
 7         const uint WM_SYSCOMMAND = 0x0112;
 8         const uint SC_MOVE = 0xF010;
 9         const uint HTCAPTION = 0x0002;
10 
11         [DllImport("user32.dll", EntryPoint = "SendMessageA")]
12         private static extern int SendMessage(IntPtr hwnd, uint wMsg, uint wParam, uint lParam);
13         [DllImport("user32.dll")]
14         private static extern int ReleaseCapture();
15 
16         void ControlMouseDown(object sender, MouseEventArgs e)
17         {
18             ReleaseCapture();
19             SendMessage((sender as Control).Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
20         }
复制代码

 来自: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, 0);
}  
复制代码

 

posted on   z5337  阅读(227)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示