1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Drawing.Drawing2D;
- using System.Runtime.InteropServices;
- namespace TestProject
- {
- public partial class UserPanel : Panel
- {
- //所有用到了headBar的部分都需要这段代码
- [DllImport("user32.dll")]
- public static extern bool ReleaseCapture();
- [DllImport("user32.dll")]
- public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
- [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
- public static extern int GetWindowLong(HandleRef hWnd, int nIndex);
- [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
- public static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);
- public const int WM_SYSCOMMAND = 0x0112;
- public const int SC_MOVE = 0xF010;
- public const int HTCAPTION = 0x0002;
- public UserPanel()
- {
- InitializeComponent();
- }
- private void UserPanel_Paint(object sender, PaintEventArgs e)
- {
- Graphics g = e.Graphics;
- Pen p = new Pen(Color.CadetBlue, 2);
- Rectangle rect = new Rectangle(this.Location, this.Size);
- LinearGradientBrush b3 = new LinearGradientBrush(rect, Color.LightSkyBlue, Color.White, LinearGradientMode.Vertical);
- g.FillRectangle(Brushes.White, rect);
- g.DrawString("通知公告", new Font("微软雅黑", 14F, FontStyle.Regular, GraphicsUnit.Point, 134), Brushes.Black, this.Width / 2 - 40, 2);
- //绘制关闭按钮
- b3 = new LinearGradientBrush(rect, Color.IndianRed, Color.White, LinearGradientMode.Vertical);
- rect = new Rectangle(new Point(this.Location.X + this.Size.Width - this.Size.Height, 0), new Size(this.Size.Height, this.Size.Height));
- //绘制关闭按钮
- GraphicsPath Rect = CreateRoundedRectanglePath(rect, 1);
- g.FillPath(Brushes.Tomato, Rect);
- 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));
- 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));
- //绘制最小化按钮
- 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));
- //使用路径绘制
- Rect = CreateRoundedRectanglePath(rect, 1);
- g.FillPath(Brushes.Tomato, Rect);
- 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));
- }
- internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
- {
- GraphicsPath roundedRect = new GraphicsPath();
- roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
- roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
- roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
- roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
- roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
- roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
- roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
- roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
- roundedRect.CloseFigure();
- return roundedRect;
- }
- private void UserPanel_MouseDown(object sender, MouseEventArgs e)
- {
- ReleaseCapture();
- SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
- if (e.Location.X >= this.Location.X + this.Size.Width - this.Size.Height)
- {
- this.FindForm().Close();
- }
- 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)
- {
- FindForm().WindowState = FormWindowState.Minimized;
- }
- }
- }
- }
|