C# WinForm 中在窗口标题栏上加按钮
实现主要是通过重写窗口过程(WndProc),处理一些非客户区消息(WM_NCxxxx);
1)、C#中重写窗口过程不用再调用SetWindowLong API了,直接overide一个WndProc就可以了。
2)、Windows API中的HDC可以通过Graphics.FromHdc()转换为(创建出)System.Drawing.Graphics,然后就可以用.NET Framework (GID+??)提供的绘图功能方便地进行画图了。终于可以抛开讨厌的GDI API了(说实在话,在C#中调用Windows API真的太麻烦了).
1
[DllImport("User32.dll")]
2
private static extern IntPtr GetWindowDC(IntPtr hwnd);
3
[DllImport("User32.dll")]
4
private static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
5
[DllImport("Kernel32.dll")]
6
private static extern int GetLastError();
7
//标题栏按钮的矩形区域。
8
Rectangle m_rect = new Rectangle(100, 6, 120, 20);
9
protected override void WndProc(ref Message m)
10
{
11
base.WndProc(ref m);
12
switch (m.Msg)
13
{
14
case 0x86://WM_NCACTIVATE
15
goto case 0x85;
16
case 0x85://WM_NCPAINT
17
{
18
IntPtr hDC = GetWindowDC(m.HWnd);
19
//把DC转换为.NET的Graphics就可以很方便地使用Framework提供的绘图功能了
20
Graphics gs = Graphics.FromHdc(hDC);
21
gs.FillRectangle(new LinearGradientBrush(m_rect, Color.Pink, Color.Purple, LinearGradientMode.BackwardDiagonal), m_rect);
22
StringFormat strFmt = new StringFormat();
23
strFmt.Alignment = StringAlignment.Center;
24
strFmt.LineAlignment = StringAlignment.Center;
25
gs.DrawString("√", this.Font, Brushes.BlanchedAlmond, m_rect, strFmt);
26
gs.Dispose();
27
//释放GDI资源
28
ReleaseDC(m.HWnd, hDC);
29
break;
30
}
31
case 0xA1://WM_NCLBUTTONDOWN
32
{
33
Point mousePoint = new Point((int)m.LParam);
34
mousePoint.Offset(-this.Left, -this.Top);
35
if (m_rect.Contains(mousePoint))
36
{
37
MessageBox.Show("hello");
38
}
39
break;
40
}
41
}
42
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1)、C#中重写窗口过程不用再调用SetWindowLong API了,直接overide一个WndProc就可以了。
2)、Windows API中的HDC可以通过Graphics.FromHdc()转换为(创建出)System.Drawing.Graphics,然后就可以用.NET Framework (GID+??)提供的绘图功能方便地进行画图了。终于可以抛开讨厌的GDI API了(说实在话,在C#中调用Windows API真的太麻烦了).