ResizbleForm.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Windows.Forms;
  3. namespace BluePointLilac.Controls
  4. {
  5. /// <summary>限制水平、竖直方向调整大小的窗体</summary>
  6. public class ResizbleForm : Form
  7. {
  8. /// <summary>水平方向可调整大小</summary>
  9. public bool HorizontalResizable { get; set; } = true;
  10. /// <summary>竖直方向可调整大小</summary>
  11. public bool VerticalResizable { get; set; } = true;
  12. protected override void WndProc(ref Message m)
  13. {
  14. base.WndProc(ref m);
  15. if(m.Msg == WM_NCHITTEST && this.WindowState == FormWindowState.Normal)
  16. {
  17. IntPtr hNowhere = new IntPtr((int)HitTest.Nowhere);
  18. HitTest value = (HitTest)m.Result;
  19. switch(value)
  20. {
  21. case HitTest.Top:
  22. case HitTest.Bottom:
  23. if(!VerticalResizable) m.Result = hNowhere;
  24. break;
  25. case HitTest.Left:
  26. case HitTest.Right:
  27. if(!HorizontalResizable) m.Result = hNowhere;
  28. break;
  29. case HitTest.TopLeft:
  30. case HitTest.TopRight:
  31. case HitTest.BottomLeft:
  32. case HitTest.BottomRight:
  33. if(!VerticalResizable || !HorizontalResizable) m.Result = hNowhere;
  34. break;
  35. }
  36. }
  37. }
  38. const int WM_NCHITTEST = 0x84;//光标移动或鼠标按下、释放时的消息
  39. /// <summary>鼠标击中位置</summary>
  40. public enum HitTest : int
  41. {
  42. Error = -2,
  43. Transparent = -1,
  44. Nowhere = 0,
  45. Client = 1,
  46. TitleBar = 2,
  47. SysMenu = 3,
  48. Size = 4,
  49. GrowBox = 5,
  50. Hscroll = 6,
  51. Vscroll = 7,
  52. MinButton = 8,
  53. MaxButton = 9,
  54. Left = 10,
  55. Right = 11,
  56. Top = 12,
  57. TopLeft = 13,
  58. TopRight = 14,
  59. Bottom = 15,
  60. BottomLeft = 16,
  61. BottomRight = 17,
  62. Border = 18,
  63. Close = 20,
  64. Help = 21
  65. }
  66. }
  67. }