失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 在WPF中处理Windows消息

在WPF中处理Windows消息

时间:2022-07-04 02:52:45

相关推荐

在WPF中处理Windows消息

在Winform中 处理Windows消息通过重写WndProc方法

在WPF中 使用的是System.Windows. Sytem.Windows.Controls等名字空间,没有WndProc函数

WPF中处理消息首先要获取窗口句柄,创建HwndSource对象 通过HwndSource对象添加消息处理回调函数。

此外 WPF中没有Handle属性,不能直接获得窗口的句柄

可以在构造函数中指定消息进行消息处理的回调函数,也可以在资源初始化后指定,在很多地方都可以指定。

以下是在资源初始化后指定

protectedoverridevoidOnSourceInitialized(EventArgse)

{

base.OnSourceInitialized(e);

HwndSourcehwndSource=PresentationSource.FromVisual(this)asHwndSource;

if(hwndSource!=null)

hwndSource.AddHook(newHwndSourceHook(this.WndProc));

}

注意要使用System.Windows.Interop名字空间,以上增加了一个回调委托

protectedvirtualIntPtrWndProc(IntPtrhwnd,intmsg,IntPtrwParam,IntPtrlParam,refboolhandled)

{

switch(msg)

{

caseWin32.WM_NCHITTEST:

{

PointMouseScreenPoint=newPoint(lParam.ToInt32()&0xFFFF,lParam.ToInt32()>>16);

PointMouseWindowPoint=this.PointFromScreen(MouseScreenPoint);

if(MouseWindowPoint.X<10&&MouseWindowPoint.Y<10)

{

handled=true;

return(IntPtr)Win32.HTTOPLEFT;

}

break;

}

}

returnIntPtr.Zero;

}

这里有一个引用传递的参数handled ,处理消息后设置为true 告诉系统这个消息已经处理过了。

如果觉得《在WPF中处理Windows消息》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。