如何禁用双击窗体图标关闭窗体

这个问题来自论坛提问,.Net由于对WindowsAPI的透明度增强,很多Winform开发者都不太了解经典的win32消息方面的知识。

所有窗体上的鼠标反映都可以用WndProc拦截,除了系统菜单、标题栏、系统按纽还包括客户区。

  1. using  System;
  2. using  System.Windows.Forms;
  3. using  System.Runtime.InteropServices;
  4. namespace  WindowsApplication1
  5. {
  6.      public  partial  class  Form1 : Form
  7.     {
  8.          public  Form1()
  9.         {
  10.             InitializeComponent();
  11.             
  12.         }
  13.         [DllImport( "user32.dll" )]
  14.          private   static   extern  IntPtr GetSystemMenu(IntPtr hWnd,  bool  bRevert);
  15.          protected   override   void  WndProc( ref    Message m)
  16.         {
  17.              if  (m.Msg == 0x112)
  18.             {
  19.                  if  (m.WParam.ToInt32() == 61539 || m.WParam.ToInt32() == 61587)
  20.                 {
  21.                     Console.WriteLine(m.WParam);
  22.                      return ;
  23.                 }
  24.             }
  25.              base .WndProc( ref    m);
  26.         }  
  27.     }
  28. }
原文地址:https://www.cnblogs.com/cl1024cl/p/6204870.html