c# Wndproc的使用方法


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); 
}

  

  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函數來同時實現無標題欄的窗體移動和禁止雙擊窗體最大化

 

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;
             }
         }

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM