WPF中处理Windows消息

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

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

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

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

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

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

 

protected   override   void  OnSourceInitialized(EventArgs e)
        {
            
base .OnSourceInitialized(e);
            HwndSource hwndSource 
=  PresentationSource.FromVisual( this as  HwndSource;
            
if  (hwndSource  !=   null )
                hwndSource.AddHook(
new  HwndSourceHook( this .WndProc));
        }

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

protected   virtual  IntPtr WndProc(IntPtr hwnd,  int  msg, IntPtr wParam, IntPtr lParam,  ref   bool  handled)
{
    
switch  (msg)
    {
        
case  Win32.WM_NCHITTEST:
            {
                Point MouseScreenPoint 
=   new  Point(lParam.ToInt32()  &   0xFFFF , lParam.ToInt32()  >>   16 );
                Point MouseWindowPoint 
=   this .PointFromScreen(MouseScreenPoint);
                
if  (MouseWindowPoint.X  <   10   &&  MouseWindowPoint.Y  <   10 )
                {
                    handled 
=   true ;
                    
return  (IntPtr)Win32.HTTOPLEFT;
                }
                
                
break ;
            }
    }
    
return  IntPtr.Zero;
}

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

转载于:https://www.cnblogs.com/yuanfan/archive/2011/04/06/2007316.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值