首页 > 代码库 > WinForm特效:拦截窗体上各个部位的点击
WinForm特效:拦截窗体上各个部位的点击
windows窗体的标题栏无法直接通过一些默认的事件来控制,需要了解和WM_NCHITTEST相关的windows消息。 以下示例演示了最简单的效果片断: 他会把客户区和标题栏的效果互换,比如无法按住标题栏拖动窗体而是改为了安抓客户区拖动,并禁用了关闭按钮。 其中m.Result从-2到21都有定义,分别度应了整个窗体的各个部位,比如1代表客户区,8代表最小化按钮等等。
[csharp] view plaincopy
- 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中摘取:
[html] view plaincopy
- { 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;
WinForm特效:拦截窗体上各个部位的点击
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。