首页 | 新闻 | 交流 | 问吧 | 文档 | 手册 | 下载 | 博客

C#重写WndProc方法,截获WM_NCHITTEST消息,并通过消息的返回值找出鼠标的当前位置.

作者:  时间: 2011-03-11

private const int WM_NCHITTEST = 0x84;
 
        /// <summary>
        
/// 重写WndProc方法,截获WM_NCHITTEST消息,并通过消息的返回值找出鼠标的当前位置.
        
/// </summary>
        
/// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            
switch (m.Msg)
            {
                
case WM_NCHITTEST:
                    {
                        
///让父类调用一次,以便能获取m的返回值
                        base.WndProc(ref m);
                        
switch ((int)m.Result)
                        {
                            
case 1: Text = "客户区";
                                
break;
                            
case 10: Text = "左边缘";
                                
break;
                            
case 11: Text = "右边缘";
                                
break;
                            
case 12: Text = "上边缘";
                                
break;
                            
case 18: Text = "窗口底部";
                                
break;
                            
case 15: Text = "下边缘";
                                
break;
                            
case 13: Text = "左上斜角";
                                
break;
                            
case 14: Text = "右上斜角";
                                
break;
                            
case 16: Text = "左下斜角";
                                
break;
                            
case 17: Text = "右下斜角";
                                
break;
                            
default:
                                
break;
                        }
                        
break;
                    }

                
defaultbreak;
            }
            
            
base.WndProc(ref m);
        }