Pārlūkot izejas kodu

自定义导航栏Demo

章政 8 gadi atpakaļ
vecāks
revīzija
0127051891
1 mainītis faili ar 88 papildinājumiem un 0 dzēšanām
  1. 88 0
      TestProject/Form2.cs

+ 88 - 0
TestProject/Form2.cs

@@ -1,11 +1,34 @@
 using System;
 using System.Windows.Forms;
 using BenQGuru.eMES.DLLService;
+using System.Drawing.Drawing2D;
+using System.Drawing;
+using System.Runtime.InteropServices;
 
 namespace TestProject
 {
     public partial class Form2 : Form
     {
+
+        //所有用到了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;
+
+        private Panel TitlePanel;
+
         public Form2()
         {
             InitializeComponent();
@@ -41,13 +64,28 @@ namespace TestProject
 
         private void InitializeComponent()
         {
+            this.TitlePanel = new System.Windows.Forms.Panel();
             this.SuspendLayout();
             // 
+            // TitlePanel
+            // 
+            this.TitlePanel.Dock = System.Windows.Forms.DockStyle.Top;
+            this.TitlePanel.Location = new System.Drawing.Point(0, 0);
+            this.TitlePanel.Name = "TitlePanel";
+            this.TitlePanel.Size = new System.Drawing.Size(435, 40);
+            this.TitlePanel.TabIndex = 0;
+            this.TitlePanel.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
+            this.TitlePanel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TitlePanel_MouseDown);
+            // 
             // Form2
             // 
+            this.BackColor = System.Drawing.Color.White;
             this.ClientSize = new System.Drawing.Size(435, 184);
+            this.Controls.Add(this.TitlePanel);
+            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
             this.Name = "Form2";
             this.Load += new System.EventHandler(this.Form2_Load_1);
+            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form2_Paint);
             this.ResumeLayout(false);
 
         }
@@ -56,5 +94,55 @@ namespace TestProject
         {
 
         }
+
+        private void Form2_Paint(object sender, PaintEventArgs e)
+        {
+          
+        }
+
+        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 panel1_Paint(object sender, PaintEventArgs e)
+        {
+            Graphics g = e.Graphics;
+            Pen p = new Pen(Color.CadetBlue, 2);
+            Rectangle rect = new Rectangle(TitlePanel.Location, TitlePanel.Size);
+            LinearGradientBrush b3 = new LinearGradientBrush(rect, Color.LightSkyBlue, Color.White, LinearGradientMode.Vertical);
+            g.FillRectangle(b3, rect);
+            g.DrawString("通知公告", new Font("微软雅黑", 14F, FontStyle.Regular, GraphicsUnit.Point, 134), Brushes.Black, TitlePanel.Width / 2 - 40, 2);
+            //绘制关闭按钮矩形
+            b3 = new LinearGradientBrush(rect, Color.IndianRed, Color.White, LinearGradientMode.Vertical);
+            rect = new Rectangle(new Point(TitlePanel.Location.X + TitlePanel.Size.Width - TitlePanel.Size.Height, 0), new Size(TitlePanel.Size.Height, TitlePanel.Size.Height));
+            //绘制关闭按钮实线
+            GraphicsPath Rect = CreateRoundedRectanglePath(rect, 1);
+            g.FillPath(Brushes.Tomato, Rect);
+            g.DrawLine(new Pen(Color.White, 4), new Point(TitlePanel.Location.X + TitlePanel.Size.Width - TitlePanel.Size.Height + 12, 12), new Point(TitlePanel.Location.X + TitlePanel.Size.Width - 12, TitlePanel.Size.Height - 12));
+            g.DrawLine(new Pen(Color.White, 4), new Point(TitlePanel.Location.X + TitlePanel.Size.Width - TitlePanel.Size.Height + 12, TitlePanel.Size.Height - 12), new Point(TitlePanel.Location.X + TitlePanel.Size.Width - 12, 12));
+            //绘制最小化按钮
+            rect = new Rectangle(new Point(TitlePanel.Location.X + TitlePanel.Size.Width - TitlePanel.Size.Height * 2 - 10, 0), new Size(TitlePanel.Size.Height, TitlePanel.Size.Height));
+            //使用路径绘制
+            Rect = CreateRoundedRectanglePath(rect, 1);
+            g.FillPath(Brushes.Tomato, Rect);
+            g.DrawLine(new Pen(Color.White, 4), new Point(TitlePanel.Location.X + TitlePanel.Size.Width - TitlePanel.Size.Height*2 , 20), new Point(TitlePanel.Location.X + TitlePanel.Size.Width - TitlePanel.Size.Height - 20, 20));
+        }
+
+        private void TitlePanel_MouseDown(object sender, MouseEventArgs e)
+        {
+            ReleaseCapture();
+            SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
+        }
     }
 }