WinForm实现拦截窗体上各个部位的点击特效实例_C#教程

分享到:

本文实例讲述了WinForm实现拦截窗体上各个部位的点击特效,是一个非常实用的技巧。分享给大家供大家参考。具体分析如下:

一般来说,windows窗体的标题栏无法直接通过一些默认的事件来控制,需要了解和WM_NCHITTEST相关的windows消息。

以下示例演示了最简单的效果片断:他会把客户区和标题栏的效果互换,比如无法按住标题栏拖动窗体而是改为了按住客户区拖动,并禁用了关闭按钮。 其中m.Result从-2到21都有定义,分别对应了整个窗体的各个部位,比如1代表客户区,8代表最小化按钮等等。

具体功能代码如下:

using System; 
using System.Windows.Forms; 
namespace WindowsApplication42 
{ 
 public partial class Form1 : Form 
 { 
  public Form1() { InitializeComponent(); this.Text = "双击左上角图标关闭窗体"; } 
  //拦击标题栏鼠标位置事件 
  const int WM_NCHITTEST = 0x84; 
  protected override void WndProc(ref Message m) 
  { 
   base.WndProc(ref m); if (m.Msg == WM_NCHITTEST) 
   { 
    switch (m.Result.ToInt32()) 
    { 
     case 1://客户区 
      m.Result = new IntPtr(2); break; 
     case 2://标题栏 
      m.Result = new IntPtr(1); break; 
     case 20: m.Result = new IntPtr(0); break; 
     default: Console.WriteLine(m); break; 
    } 
   } 
  } 
 } 
}

下面是各个常量的意义:从Delphi7 VCL中摘取:

{ WM_NCHITTEST and MOUSEHOOKSTRUCT Mouse Position Codes } 
 {$EXTERNALSYM HTERROR} 
 HTERROR = -2; 
 {$EXTERNALSYM HTTRANSPARENT} 
 HTTRANSPARENT = -1; 
 {$EXTERNALSYM HTNOWHERE} 
 HTNOWHERE = 0; 
 {$EXTERNALSYM HTCLIENT} 
 HTCLIENT = 1; 
 {$EXTERNALSYM HTCAPTION} 
 HTCAPTION = 2; 
 {$EXTERNALSYM HTSYSMENU} 
 HTSYSMENU = 3; 
 {$EXTERNALSYM HTGROWBOX} 
 HTGROWBOX = 4; 
 {$EXTERNALSYM HTSIZE} 
 HTSIZE = HTGROWBOX; 
 {$EXTERNALSYM HTMENU} 
 HTMENU = 5; 
 {$EXTERNALSYM HTHSCROLL} 
 HTHSCROLL = 6; 
 {$EXTERNALSYM HTVSCROLL} 
 HTVSCROLL = 7; 
 {$EXTERNALSYM HTMINBUTTON} 
 HTMINBUTTON = 8; 
 {$EXTERNALSYM HTMAXBUTTON} 
 HTMAXBUTTON = 9; 
 {$EXTERNALSYM HTLEFT} 
 HTLEFT = 10; 
 {$EXTERNALSYM HTRIGHT} 
 HTRIGHT = 11; 
 {$EXTERNALSYM HTTOP} 
 HTTOP = 12; 
 {$EXTERNALSYM HTTOPLEFT} 
 HTTOPLEFT = 13; 
 {$EXTERNALSYM HTTOPRIGHT} 
 HTTOPRIGHT = 14; 
 {$EXTERNALSYM HTBOTTOM} 
 HTBOTTOM = 15; 
 {$EXTERNALSYM HTBOTTOMLEFT} 
 HTBOTTOMLEFT = 16; 
 {$EXTERNALSYM HTBOTTOMRIGHT} 
 HTBOTTOMRIGHT = 17; 
 {$EXTERNALSYM HTBORDER} 
 HTBORDER = 18; 
 {$EXTERNALSYM HTREDUCE} 
 HTREDUCE = HTMINBUTTON; 
 {$EXTERNALSYM HTZOOM} 
 HTZOOM = HTMAXBUTTON; 
 {$EXTERNALSYM HTSIZEFIRST} 
 HTSIZEFIRST = HTLEFT; 
 {$EXTERNALSYM HTSIZELAST} 
 HTSIZELAST = HTBOTTOMRIGHT; 
 {$EXTERNALSYM HTOBJECT} 
 HTOBJECT = 19; 
 {$EXTERNALSYM HTCLOSE} 
 HTCLOSE = 20; 
 {$EXTERNALSYM HTHELP} 
 HTHELP = 21;

希望本文所述对大家的C#程序设计有所帮助。

昵    称:
验证码:

相关文档: