您的位置:首页 > 编程语言 > C#

c#无标题窗口的拖动

2012-08-29 22:14 225 查看
c#桌面程序的窗口美化少不了自己重绘窗口,去掉标题栏自己添加元素是最简单的实现,不过没有了标题栏鼠标拖动窗体活动成了问题,百度搜到两种c#实现重绘的方法。

一、重绘WndProc

protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0201) //鼠标左键按下去的消息
{
m.Msg = 0x00A1; //更改消息为非客户区按下鼠标
m.LParam = IntPtr.Zero; //默认值
m.WParam = new IntPtr(2); //鼠标放在标题栏内
}
base.WndProc(ref m);
}


二、调用API函数

using System.Runtime.InteropServices;
//然后,在程序中声明我们要用到的API函数及要用到的参数常量。

[DllImport("user32")]
private static extern bool ReleaseCapture();

[DllImport("user32")]
private 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;

//其中,WM_SYSCOMMAND常量代表要向窗口发送消息,SC_MOVE常代表要向窗口发送移动的消息。
//然后,接下来就很简单了。在Form的MouseDown事件中加入以下代码即可:

ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION,0);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: