c# Wndproc的使用方法

本文详细介绍了如何通过重写 C# 中的 WndProc 方法来实现多种窗口行为定制,包括屏蔽关闭按钮、最小化到系统栏、无标题栏窗体的移动以及禁止双击最大化等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

c# Wndproc的使用方法
?
1
2
3
4
5
6
7
8
9
10
11
12
protected override void WndProc( ref Message m)
{
     const int WM_SYSCOMMAND = 0x0112;
     const int SC_CLOSE = 0xF060;
     if (m.Msg == WM_SYSCOMMAND && ( int ) m.WParam == SC_CLOSE)
     {
         // 屏蔽传入的消息事件
         this .WindowState = FormWindowState.Minimized;
         return ;
      }
     base .WndProc( ref m);
}

  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
protected override void WndProc( ref    Message m)
         {
             const int WM_SYSCOMMAND = 0x0112;
             const int SC_CLOSE = 0xF060;
             const int SC_MINIMIZE = 0xF020;
             if (m.Msg == WM_SYSCOMMAND && (( int )m.WParam == SC_MINIMIZE || ( int )m.WParam == SC_CLOSE))
             {
                 //最小化到系统栏
                this .Hide();
                 return ;
             }
             base .WndProc( ref    m);
         }

重写 WndProc函数来同时实现无标题栏的窗体移动和禁止双击窗体最大化

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
protected override void WndProc( ref Message m)
          {
              const int WM_NCHITTEST = 0x84;
              const int HTCLIENT = 0x01;
              const int HTCAPTION = 0x02;
              const int WM_SYSCOMMAND = 0x112;
              const int SC_MAXMIZE = 0xF030;
              const int WM_NCLBUTTONDBLCLK = 0xA3;
              switch (m.Msg)
              {
                  case 0x4e:
                  case 0xd:
                  case 0xe:
                  case 0x14:
                      base .WndProc( ref m);
                      break ;
                  case WM_NCHITTEST: //鼠标点任意位置后可以拖动窗体
                      
                     this .DefWndProc( ref m);
                      if (m.Result.ToInt32() == HTCLIENT)
                      {
                          m.Result = new IntPtr(HTCAPTION);
                          return ;
                      }
                      break ;
                  case WM_NCLBUTTONDBLCLK: //禁止双击最大化
                      Console.WriteLine( this .WindowState);
                      
                         return ;
  
                    
                     break ;
  
                 default :
                      
                     base .WndProc( ref m);
                      break ;
              }
          }

 

  

 

0
0
« 上一篇: NET异常 在 getsockopt 或 setsockopt 调用中指定的一个未知的、无效的或不受支持的选项或层次。
» 下一篇: Access使用参数化UPDATE数据时,数据无法更新的问题
posted on 2014-08-13 11:50 拼博之路 阅读( 7747) 评论( 0) 编辑 收藏


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值