在窗体的非客户区(如标题栏)中添加菜单栏或按钮
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
标题栏,窗口边框等地方都是所谓的非客户区,窗口标准处理过程在接收到 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
相关文章推荐
- Delphi组件开发-在窗体标题栏添加按钮
- Delphi组件开发-在窗体标题栏添加按钮(使用MakeObjectInstance(NewWndProc),并处理好多消息)
- C#Windows窗体界面设计_05_添加菜单栏 工具栏 状态栏 按钮
- 如何在标题栏上添加按钮&通过例程分析状态条用法
- Python3 Tkinter基础 Button command在窗体中添加一个按钮,单击按钮 在IDLE中打印一行字
- VC MFC 钩子 实现 自绘 窗体 标题栏 非客户区
- 如何在标题栏上添加自己的按钮
- Activity标题栏添加返回按钮
- VC 实现 自绘 窗体 标题栏 非客户区
- jqGrid添加详细按钮,单击弹出窗体
- VC 实现 自绘 窗体 标题栏 非客户区
- Android标题栏中添加返回按钮功能
- GUI_窗体添加按钮并对按钮添加事件案例、布局
- C#源代码—在windows窗体中添加窗体控件,btnOK按钮的Click事件
- 修改窗体非客户区大小更改窗体标题栏高度
- C# WPF Win7 标题栏添加按钮(模拟)
- 如何取消窗体标题栏关闭按钮
- Android标题栏上添加多个Menu按钮
- 添加菜单栏自绘按钮
- C#给WinForm(窗体)下DataGridView控件添加单选选或多选按钮列