您的位置:首页 > 其它

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

2008-08-02 19:36 381 查看
在窗体的非客户区(如标题栏)中添加菜单栏或按钮( C#)

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

using System.Runtime.InteropServices;

using System.Drawing.Drawing2D;

[DllImport("user32.dll")]

private static extern IntPtr GetWindowDC(IntPtr hWnd);

[DllImport("user32.dll")]

private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

private const int WM_NCPAINT = 0x0085;

private const int WM_NCACTIVATE = 0x0086;

private const int WM_NCLBUTTONDOWN = 0x00A1;

protected override void WndProc(ref Message m)

{

base.WndProc(ref m);

Rectangle vRectangle = new Rectangle((Width - 75) / 2, 3, 75, 25);

switch (m.Msg)

{

case WM_NCPAINT:

case WM_NCACTIVATE:

IntPtr vHandle = GetWindowDC(m.HWnd);

Graphics vGraphics = Graphics.FromHdc(vHandle);

vGraphics.FillRectangle(new LinearGradientBrush(vRectangle,

Color.Pink, Color.Purple, LinearGradientMode.BackwardDiagonal),

vRectangle);

StringFormat vStringFormat = new StringFormat();

vStringFormat.Alignment = StringAlignment.Center;

vStringFormat.LineAlignment = StringAlignment.Center;

vGraphics.DrawString("About", Font, Brushes.BlanchedAlmond,

vRectangle, vStringFormat);

vGraphics.Dispose();

ReleaseDC(m.HWnd, vHandle);

break;

case WM_NCLBUTTONDOWN:

Point vPoint = new Point((int)m.LParam);

vPoint.Offset(-Left, -Top);

if (vRectangle.Contains(vPoint))

MessageBox.Show(vPoint.ToString());

break;

}

}

另一个例子:

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Runtime.InteropServices;

namespace ShortButComplete

{

public class Form1 : System.Windows.Forms.Form

{

private System.ComponentModel.Container components = null;

[DllImport("user32.dll")]

public static extern IntPtr GetWindowDC(IntPtr hWnd);

[DllImport("user32.dll")]

public static extern IntPtr GetDC(IntPtr hWnd);

[DllImport("user32.dll")]

public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

//Message constants

public const int WM_NCPAINT = 0x0085;

public Form1()

{

InitializeComponent();

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

protected override void WndProc(ref Message m)

{

switch (m.Msg)

{

case WM_NCPAINT:

base.WndProc (ref m);

IntPtr hDC = GetWindowDC(m.HWnd);

Graphics g = Graphics.FromHdc(hDC);

PaintNC(m.HWnd);

g.Dispose();

ReleaseDC(m.HWnd, hDC);

m.Result = IntPtr.Zero;

break;

default :

base.WndProc(ref m);

break;

}

}

protected void PaintNC(System.IntPtr hWnd)

{

IntPtr hDC = GetWindowDC(hWnd);

Graphics g = Graphics.FromHdc(hDC);

int CaptionHeight = Bounds.Height - ClientRectangle.Height; //Titlebar

Size CloseButtonSize = SystemInformation.CaptionButtonSize;

int X = Bounds.Width - 4 - CloseButtonSize.Width * 2;

int Y = 6;

ControlPaint.DrawButton(g, X, Y, 15, 15, ButtonState.Normal);

g.Dispose();

ReleaseDC(hWnd, hDC);

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(322, 295);

this.MaximizeBox = false;

this.MinimizeBox = false;

this.Name = "Form1";

this.Text = "Form1";

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

}

}

参照下面的连接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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: