NoTitleForm.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Windows.Forms;
  7. namespace CustomWindow.Forms
  8. {
  9. public partial class NoTitleForm : Form
  10. {
  11. #region 属性
  12. //窗体边框粗细
  13. private int _Border = 1;
  14. [Category("Style")]
  15. [Description("窗体边框粗细")]
  16. [DefaultValue(typeof(int), "1")]
  17. public int Border
  18. {
  19. get { return _Border; }
  20. set
  21. {
  22. if (_Border != value)
  23. {
  24. _Border = value;
  25. SetBorder();
  26. }
  27. }
  28. }
  29. //窗体边框颜色
  30. private Color _BorderColor = Color.Black;
  31. [Category("Style")]
  32. [Description("窗体边框颜色")]
  33. [DefaultValue(typeof(Color), "Black")]
  34. public Color BorderColor
  35. {
  36. get { return _BorderColor; }
  37. set
  38. {
  39. if (_BorderColor != value)
  40. {
  41. _BorderColor = value;
  42. SetBorder();
  43. }
  44. }
  45. }
  46. #endregion
  47. public NoTitleForm()
  48. {
  49. InitializeComponent();
  50. }
  51. private void NoTitleForm_Load(object sender, EventArgs e)
  52. {
  53. SetBorder();
  54. }
  55. /// <summary>
  56. /// 设置无标题窗口可拖动
  57. /// </summary>
  58. /// <param name="e"></param>
  59. protected override void OnMouseMove(MouseEventArgs e)
  60. {
  61. base.OnMouseMove(e);
  62. if (e.Button == MouseButtons.Left)
  63. {
  64. ReleaseCapture();
  65. SendMessage(Handle, 0x00A1, 2, 0);
  66. }
  67. }
  68. /// <summary>
  69. /// 设置窗口边框
  70. /// </summary>
  71. protected void SetBorder()
  72. {
  73. if (_Border > 0)
  74. {
  75. Label BorderTop = new Label();
  76. BorderTop.BackColor = _BorderColor;
  77. BorderTop.Width = Width;
  78. BorderTop.Height = _Border;
  79. Controls.Add(BorderTop);
  80. BorderTop.BringToFront();
  81. BorderTop.Top = 0;
  82. BorderTop.Left = 0;
  83. Label BorderBottom = new Label();
  84. BorderBottom.BackColor = _BorderColor;
  85. BorderBottom.Width = Width;
  86. BorderBottom.Height = _Border;
  87. Controls.Add(BorderBottom);
  88. BorderBottom.BringToFront();
  89. BorderBottom.Top = Height - _Border;
  90. BorderBottom.Left = 0;
  91. Label BorderLeft = new Label();
  92. BorderLeft.BackColor = _BorderColor;
  93. BorderLeft.Width = _Border;
  94. BorderLeft.Height = Height;
  95. Controls.Add(BorderLeft);
  96. BorderLeft.BringToFront();
  97. BorderLeft.Top = 0;
  98. BorderLeft.Left = 0;
  99. Label BorderRight = new Label();
  100. BorderRight.BackColor = _BorderColor;
  101. BorderRight.Width = _Border;
  102. BorderRight.Height = Height;
  103. Controls.Add(BorderRight);
  104. BorderRight.BringToFront();
  105. BorderRight.Top = 0;
  106. BorderRight.Left = Width - _Border;
  107. }
  108. }
  109. #region API
  110. [DllImport("user32.dll")]
  111. public static extern bool ReleaseCapture();
  112. [DllImport("user32.dll")]
  113. public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
  114. #endregion
  115. }
  116. }