UserPanel.cs 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Drawing.Drawing2D;
  10. using System.Runtime.InteropServices;
  11. namespace TestProject
  12. {
  13. public partial class UserPanel : Panel
  14. {
  15. //所有用到了headBar的部分都需要这段代码
  16. [DllImport("user32.dll")]
  17. public static extern bool ReleaseCapture();
  18. [DllImport("user32.dll")]
  19. public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  20. [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
  21. public static extern int GetWindowLong(HandleRef hWnd, int nIndex);
  22. [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
  23. public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);
  24. public const int WM_SYSCOMMAND = 0x0112;
  25. public const int SC_MOVE = 0xF010;
  26. public const int HTCAPTION = 0x0002;
  27. public UserPanel()
  28. {
  29. InitializeComponent();
  30. }
  31. private void UserPanel_Paint(object sender, PaintEventArgs e)
  32. {
  33. Graphics g = e.Graphics;
  34. Pen p = new Pen(Color.CadetBlue, 2);
  35. Rectangle rect = new Rectangle(this.Location, this.Size);
  36. LinearGradientBrush b3 = new LinearGradientBrush(rect, Color.LightSkyBlue, Color.White, LinearGradientMode.Vertical);
  37. g.FillRectangle(Brushes.White, rect);
  38. g.DrawString("通知公告", new Font("微软雅黑", 14F, FontStyle.Regular, GraphicsUnit.Point, 134), Brushes.Black, this.Width / 2 - 40, 2);
  39. //绘制关闭按钮
  40. b3 = new LinearGradientBrush(rect, Color.IndianRed, Color.White, LinearGradientMode.Vertical);
  41. rect = new Rectangle(new Point(this.Location.X + this.Size.Width - this.Size.Height, 0), new Size(this.Size.Height, this.Size.Height));
  42. //绘制关闭按钮
  43. GraphicsPath Rect = CreateRoundedRectanglePath(rect, 1);
  44. g.FillPath(Brushes.Tomato, Rect);
  45. g.DrawLine(new Pen(Color.White, 4), new Point(this.Location.X + this.Size.Width - this.Size.Height + 12, 12), new Point(this.Location.X + this.Size.Width - 12, this.Size.Height - 12));
  46. g.DrawLine(new Pen(Color.White, 4), new Point(this.Location.X + this.Size.Width - this.Size.Height + 12, this.Size.Height - 12), new Point(this.Location.X + this.Size.Width - 12, 12));
  47. //绘制最小化按钮
  48. rect = new Rectangle(new Point(this.Location.X + this.Size.Width - this.Size.Height * 2 - 10, 0), new Size(this.Size.Height, this.Size.Height));
  49. //使用路径绘制
  50. Rect = CreateRoundedRectanglePath(rect, 1);
  51. g.FillPath(Brushes.Tomato, Rect);
  52. g.DrawLine(new Pen(Color.White, 4), new Point(this.Location.X + this.Size.Width - this.Size.Height * 2, 20), new Point(this.Location.X + this.Size.Width - this.Size.Height - 20, 20));
  53. }
  54. internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
  55. {
  56. GraphicsPath roundedRect = new GraphicsPath();
  57. roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
  58. roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
  59. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
  60. roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
  61. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
  62. roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
  63. roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
  64. roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
  65. roundedRect.CloseFigure();
  66. return roundedRect;
  67. }
  68. private void UserPanel_MouseDown(object sender, MouseEventArgs e)
  69. {
  70. ReleaseCapture();
  71. SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
  72. if (e.Location.X >= this.Location.X + this.Size.Width - this.Size.Height)
  73. {
  74. this.FindForm().Close();
  75. }
  76. if (e.X > this.Location.X + this.Size.Width - this.Size.Height * 2 && e.X < this.Location.X + this.Size.Width - this.Size.Height - 20)
  77. {
  78. FindForm().WindowState = FormWindowState.Minimized;
  79. }
  80. }
  81. }
  82. }