在窗体的非客户区(如标题栏)中添加菜单栏或按钮

25 篇文章 0 订阅

在窗体的非客户区(如标题栏)中添加菜单栏或按钮( C#)

 

  标题栏,窗口边框等地方都是所谓的非客户区,窗口标准处理过程在接收到 WM_NCPAINT(Nonclient Paint)消息后对上述非客户区进行描绘工作,可以通过拦截 WM_NCPAINT 消息来自定义描绘按钮。

  1. using System.Runtime.InteropServices;
  2. using System.Drawing.Drawing2D;
  3. [DllImport("user32.dll")]
  4. private static extern IntPtr GetWindowDC(IntPtr hWnd);
  5. [DllImport("user32.dll")]
  6. private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
  7. private const int WM_NCPAINT = 0x0085;
  8. private const int WM_NCACTIVATE = 0x0086;
  9. private const int WM_NCLBUTTONDOWN = 0x00A1;
  10. protected override void WndProc(ref Message m)
  11. {
  12.     base.WndProc(ref m);
  13.     Rectangle vRectangle = new Rectangle((Width - 75) / 2, 3, 75, 25);
  14.     switch (m.Msg)
  15.     {
  16.         case WM_NCPAINT:
  17.         case WM_NCACTIVATE:
  18.             IntPtr vHandle = GetWindowDC(m.HWnd);
  19.             Graphics vGraphics = Graphics.FromHdc(vHandle);
  20.             vGraphics.FillRectangle(new LinearGradientBrush(vRectangle, 
  21.                 Color.Pink, Color.Purple, LinearGradientMode.BackwardDiagonal),
  22.                 vRectangle);
  23.             
  24.             StringFormat vStringFormat = new StringFormat();
  25.             vStringFormat.Alignment = StringAlignment.Center;
  26.             vStringFormat.LineAlignment = StringAlignment.Center;
  27.             vGraphics.DrawString("About", Font, Brushes.BlanchedAlmond, 
  28.                 vRectangle, vStringFormat);
  29.             vGraphics.Dispose();
  30.             ReleaseDC(m.HWnd, vHandle);
  31.             break;
  32.         case WM_NCLBUTTONDOWN:
  33.             Point vPoint = new Point((int)m.LParam);
  34.             vPoint.Offset(-Left, -Top);
  35.             if (vRectangle.Contains(vPoint))
  36.                 MessageBox.Show(vPoint.ToString());
  37.             break;
  38.     }
  39. }

另一个例子:

  1. using System; 
  2. using System.Drawing; 
  3. using System.Collections; 
  4. using System.ComponentModel; 
  5. using System.Windows.Forms; 
  6. using System.Data; 
  7. using System.Runtime.InteropServices; 
  8. namespace ShortButComplete 
  9.     public class Form1 : System.Windows.Forms.Form 
  10.     { 
  11.         private System.ComponentModel.Container components = null
  12.         [DllImport("user32.dll")] 
  13.     public static extern IntPtr GetWindowDC(IntPtr hWnd); 
  14.     [DllImport("user32.dll")] 
  15.     public static extern IntPtr GetDC(IntPtr hWnd); 
  16.     [DllImport("user32.dll")] 
  17.     public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
  18.     //Message constants 
  19.     public const int WM_NCPAINT   = 0x0085; 
  20.         public Form1() 
  21.         { 
  22.             InitializeComponent(); 
  23.    } 
  24.   /// <summary> 
  25.   /// Clean up any resources being used. 
  26.   /// </summary> 
  27.   protected override void Dispose( bool disposing ) 
  28.   { 
  29.        if( disposing ) 
  30.        { 
  31.             if (components != null
  32.             { 
  33.                  components.Dispose(); 
  34.             } 
  35.        } 
  36.        base.Dispose( disposing ); 
  37.   } 
  38.   protected override void WndProc(ref Message m) 
  39.   { 
  40.        switch (m.Msg) 
  41.        { 
  42.         case WM_NCPAINT: 
  43.              base.WndProc (ref m); 
  44.              IntPtr hDC = GetWindowDC(m.HWnd); 
  45.              Graphics g = Graphics.FromHdc(hDC); 
  46.              PaintNC(m.HWnd); 
  47.              g.Dispose(); 
  48.              ReleaseDC(m.HWnd, hDC); 
  49.              m.Result = IntPtr.Zero; 
  50.              break
  51.         default : 
  52.              base.WndProc(ref m); 
  53.              break
  54.        } 
  55.    } 
  56.    protected void PaintNC(System.IntPtr hWnd) 
  57.    { 
  58.        IntPtr hDC = GetWindowDC(hWnd); 
  59.        Graphics g = Graphics.FromHdc(hDC); 
  60.        int CaptionHeight = Bounds.Height - ClientRectangle.Height; //Titlebar 
  61.        Size CloseButtonSize = SystemInformation.CaptionButtonSize; 
  62.        int X = Bounds.Width - 4 - CloseButtonSize.Width * 2; 
  63.        int Y = 6; 
  64.        ControlPaint.DrawButton(g, X, Y, 15, 15, ButtonState.Normal); 
  65.        g.Dispose(); 
  66.        ReleaseDC(hWnd, hDC); 
  67.     } 
  68.   #region Windows Form Designer generated code 
  69.     /// <summary> 
  70.     /// Required method for Designer support - do not modify 
  71.     /// the contents of this method with the code editor. 
  72.     /// </summary> 
  73.     private void InitializeComponent() 
  74.     { 
  75.        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 
  76.        this.ClientSize = new System.Drawing.Size(322, 295); 
  77.        this.MaximizeBox = false
  78.        this.MinimizeBox = false
  79.        this.Name = "Form1"
  80.        this.Text = "Form1"
  81.      } 
  82.   #endregion 
  83.       /// <summary> 
  84.       /// The main entry point for the application. 
  85.       /// </summary> 
  86.       [STAThread] 
  87.       static void Main() 
  88.       { 
  89.            Application.Run(new Form1()); 
  90.       } 
  91.   } 

参照下面的连接URL
http://bytes.com/forum/thread233080.html
http://www.dotnet247.com/247reference/msgs/41/207281.aspx
http://groups.google.com/group/microsoft.public.dotnet.framework.drawing/browse_thread/thread/7b8e66d3803d8c7?hl=en&lr=&ie=UTF-8&oe=UTF-8&rnum=3

http://topic.csdn.net/u/20080404/14/3aae7d8e-c4d1-4316-a4d4-34ea3cbf4d77.html?1584948438

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值