Do not create a new Graphics object, but use e.Graphics
provided in the PaintEventArgs
argument.
I am not sure what you are trying to achieve with the GraphicsPath
. Probably you could just use TextRenderer
instead.
protected override void OnPaint(PaintEventArgs e)
{
TextRenderer.DrawText(e.Graphics, "LLOOOOLL", Font, ClientRectangle, ForeColor,
TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
}
UPDATE:
I switched a form to Aero Glass and made some tests. Both approaches with TextRenderer
and with GraphicsPath
work, however the TextRenderer
does not perform very well because ClearType produces artifacts on glass.
These API declarations are required
[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
public int Left;
public int Right;
public int Top;
public int Bottom;
}
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern void DwmExtendFrameIntoClientArea (IntPtr hwnd,
ref MARGINS margins);
[DllImport("dwmapi.dll", PreserveSig = false)]
public static extern bool DwmIsCompositionEnabled();
In the form's constructor I have this code
InitializeComponent();
if (DwmIsCompositionEnabled()) {
// Stretch the margins into the form for the glass effect.
MARGINS margins = new MARGINS();
margins.Top = 300;
DwmExtendFrameIntoClientArea(this.Handle, ref margins);
}
The custom Label
must have a black background. Black parts will display as glass. It must have a minimum size of about (125, 70) to fit your text because you start drawing at (55, 55). (Was your label too small?) You have to change the AutoSize
to false
in order to be able to change the size of the label. Here is the code for the custom label
protected override void OnPaint(PaintEventArgs e)
{
GraphicsPath path = new GraphicsPath();
SolidBrush br = new SolidBrush(Color.FromArgb(1, 0, 0));
path.AddString("LLOOOOLL", Font.FontFamily, (int)Font.Style, Font.SizeInPoints,
new Point(55, 55), StringFormat.GenericDefault);
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.FillPath(br, path);
}
With a few differences, it is the same code as yours. An important difference is that the text color must be different from black; otherwise, it would appear as glass. I just take the font properties of the actual label font. This way you can change its appearance in the properties window.
I found the code for the glass effect in this article of TheCodeKing.