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

这个问题来自论坛提问,.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. [DllImport( "user32.dll" )]
  13. private static extern IntPtrGetSystemMenu(IntPtrhWnd, bool bRevert);
  14. protected override void WndProc( ref Messagem)
  15. {
  16. if (m.Msg==0x112)
  17. {
  18. if (m.WParam.ToInt32()==61539||m.WParam.ToInt32()==61587)
  19. {
  20. Console.WriteLine(m.WParam);
  21. return ;
  22. }
  23. }
  24. base .WndProc( ref m);
  25. }
  26. }
  27. }

你可能感兴趣的:(windows,.net,WinForm)