.Net winform拖动工作区移动窗体
来源:互联网 发布:淘宝直播右下角的心 编辑:程序博客网 时间:2024/05/18 01:19
winform程序中,可以点击标题栏来拖动窗体,如果我们将窗体的边框样式设为None,类似于MSN右下角弹出的消息框,没有标题栏,该如何用鼠标来移动窗体呢?
下面将使用三个方法分别实现无边框窗体的移动
方案1:通过重载消息处理实现。重写窗口过程(WndProc),处理一些非客户区消息(WM_NCxxxx),C#中重写窗口过程不用再调用SetWindowLong API了,直接overide一个WndProc就可以了,不用声明api函数。
鼠标的拖动只对窗体本身有效,不能在窗体上的控件区域点击拖动
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x84)
{
switch (m.Result.ToInt32())
{
case 1:
m.Result = new IntPtr(2);
break;
}
}
}
方案2:调用非托管的动态链接库,通过控件的鼠标按下事件(MouseDown)发送一个拖动的消息,可以给控件添加MouseDown事件后,拖动这个控件来移动窗体
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);}
方案3:能达到的效果和方案2一样,不需要使用外部的动态链接库,直接编写逻辑代码实现,这种方法最简单,最容易理解。可用于任意有鼠标事件的控件和上,可以拖动没有句柄的控件。
Point downPoint;private void Form1_MouseDown(object sender, MouseEventArgs e){ downPoint = new Point(e.X, e.Y); }private void Form1_MouseMove(object sender, MouseEventArgs e){ if (e.Button == MouseButtons.Left) { this.Location = new Point(this.Location.X + e.X - downPoint.X, this.Location.Y + e.Y - downPoint.Y); } }
(转自:http://hi.baidu.com/smalltutu/blog/item/52bbb1b7fe9132fa31add11e.html)
- .Net winform拖动工作区移动窗体
- WinForm 无边框窗体 拖动工作区移动窗体
- WinForm 无边框窗体 拖动工作区移动窗体
- 拖动窗体工作区移动
- WinForm 禁止拖动窗体
- winform拖动窗体
- 通过鼠标任意拖动.NET Winform窗体中的控件
- 通过鼠标任意拖动.NET Winform窗体中的控件
- 通过鼠标任意拖动.NET Winform窗体中的控件
- 通过鼠标任意拖动.NET Winform窗体中的控件
- WinForm在工作区全拖动窗口
- winForm pictureBox拖动图片and动态new 窗体&移动Panel窗体头
- [VB.net][WinForm]Panel控件移动\鼠标拖动
- 让鼠标拖动WinForm窗体
- Winform无标题栏拖动窗体
- C# WinForm自定义拖动窗体
- WPF和Winform拖动窗体
- Winform 窗体移动
- 了解OLE及ActiveX
- 开发板硬件调试的一般过程:以ARM为例
- Android基础(二) LinearLayout
- 统计服务器响应时间的利器
- 两个DataTable连接实例
- .Net winform拖动工作区移动窗体
- D3DPT一系列枚举值的含义
- POJ 1331 Mutiply strtol()进制转换函数
- C++中的零散重要知识点
- 从【各大软件公司笔试压轴题】学习SQL语句
- Zenoss
- Secure Software Installation on Smartphones 笔记
- 11年上班的第一天,老板要我换部门或是换公司。
- java.net.URL例子