110,539
社区成员
发帖
与我相关
我的任务
分享
try
{
if (you.believe(it) == true || you.believe(it) == false)
{
I.believe(it);
}
}
catch(Exception e)
{
throw new Exception("It's a miracle!");
}
finally
{
it.justHappened();
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
base.WndProc(ref m);//基类执行
if(m.Msg == 132)//鼠标的移动消息(包括非窗口的移动)
{
//基类执行后m有了返回值,鼠标在窗口的每个地方的返回值都不同
if((IntPtr)2 == m.Result)//如果返回值是2,则说明鼠标是在标题拦
{
//将返回值改为1(窗口的客户区),这样系统就以为是
//在客户区拖动的鼠标,窗体就不会移动
m.Result = (IntPtr)1;
}
}
}
标题一 C# 中在显示标题栏的时候,禁止鼠标拖动窗体
标题二 C# 中屏蔽窗体的移动
实现这个功能需用到拦截系统消息的知识,拦截系统的消息有两种实现方式,在窗体中重写 WndProc(ref Message m)方法,还有就是创建一个新类或在现有的类中继承System.Windows.Forms.IMessageFilter 接口,并实现这个接口来实现拦截系统消息
方法一通过重写方法来实现
const int WM_NCLBUTTONDOWN = 0x00A1;
const int HTCAPTION = 2;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCLBUTTONDOWN && m.WParam.ToInt32() == HTCAPTION)
return;
base.WndProc(ref m);
}
方法二 通过继承接口来实现
public class MessageFilter : System.Windows.Forms.IMessageFilter
{
const int WM_NCLBUTTONDOWN = 0x00A1;
const int HTCAPTION = 2;
public bool PreFilterMessage(ref System.Windows.Forms.Message m)
{
if (m.Msg == WM_NCLBUTTONDOWN && m.WParam.ToInt32() == HTCAPTION)
return true;
return false;
}
}
创建完这个类后,创建一个对象,并把该对象添加到应用程序里边,如下列代码,下列代码是Program文件当中的入口方法
static class Program
{
private static MessageFilter filter = new MessageFilter();
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.AddMessageFilter(filter);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainFrm());
}
}