欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-16 16:55:10
本文实例讲述了winform实现拦截窗体上各个部位的点击特效,是一个非常实用的技巧。分享给大家供大家参考。具体分析如下: 一般来说,windows窗体的标题栏无法直接通过...

本文实例讲述了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#程序设计有所帮助。

上一篇:

下一篇: