1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// override the WndProc method and trap the WM_NCPAINT message to draw
//the caption text with the color you need. For example:
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WinAPI.WM_NCPAINT)
{
int x = WinAPI.GetSystemMetrics(WinAPI.SM_CXSIZE);
int y = WinAPI.GetSystemMetrics(WinAPI.SM_CYFRAME) +
WinAPI.GetSystemMetrics(WinAPI.SM_CYBORDER);
IntPtr hdc = WinAPI.GetWindowDC(m.HWnd);
Graphics g = Graphics.FromHdc(hdc);
Rectangle clientRecToScreen = new Rectangle(
this.PointToScreen(new Point(this.ClientRectangle.X,
this.ClientRectangle.Y)), new System.Drawing.Size(
this.ClientRectangle.Width, this.ClientRectangle.Height));
Rectangle clientRectangle = new Rectangle(clientRecToScreen.X -
this.Location.X, clientRecToScreen.Y - this.Location.Y,
clientRecToScreen.Width, clientRecToScreen.Height);
g.ExcludeClip(clientRectangle); //Remove client area
Size size = TextRenderer.MeasureText(this.Text,
SystemFonts.CaptionFont);
Rectangle rect = new Rectangle(x, y, size.Width,
size.Height);
g.FillRectangle(Brushes.Aquamarine, rect);
Color CaptionForeColor = Color.Red;
using (SolidBrush br = new SolidBrush(CaptionForeColor))
{
g.DrawString(this.Text, SystemFonts.CaptionFont, br,
rect);
}
this.Refresh();
}
}
}
class WinAPI
{
public const int SM_CXSIZE = 30;
public const int SM_CYSIZE = 31;
public const int SM_CXBORDER = 5;
public const int SM_CYBORDER = 5;
public const int SM_CXFRAME = 32;
public const int SM_CYFRAME = 33;
public const int WM_NCPAINT = 0x85;
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
public static extern int GetSystemMetrics(int abc);
[DllImport("User32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hwnd);
}
} |
Partager