我正在尝试在表单的扩展玻璃框架上绘制一个 TextBox。我不会描述这种技术,它是众所周知的。以下是未听说过的示例:http ://www.danielmoth.com/Blog/Vista-Glass-In-C.aspx
问题是,在这个玻璃框架上绘图很复杂。由于黑色被认为是 0-alpha 颜色,任何黑色都会消失。
显然有一些方法可以解决这个问题:绘制复杂的 GDI+ 形状不受这种 alpha-ness 的影响。例如,此代码可用于在玻璃上绘制标签(注意:GraphicsPath
使用它而不是DrawString
为了解决可怕的 ClearType 问题):
public class GlassLabel : Control
{
public GlassLabel()
{
this.BackColor = Color.Black;
}
protected override void OnPaint(PaintEventArgs e)
{
GraphicsPath font = new GraphicsPath();
font.AddString(
this.Text,
this.Font.FontFamily,
(int)this.Font.Style,
this.Font.Size,
Point.Empty,
StringFormat.GenericDefault);
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.FillPath(new SolidBrush(this.ForeColor), font);
}
}
类似地,这种方法可用于在玻璃区域上创建容器。请注意使用多边形而不是矩形 - 使用矩形时,其黑色部分被视为 alpha。
public class GlassPanel : Panel
{
public GlassPanel()
{
this.BackColor = Color.Black;
}
protected override void OnPaint(PaintEventArgs e)
{
Point[] area = new Point[]
{
new Point(0, 1),
new Point(1, 0),
new Point(this.Width - 2, 0),
new Point(this.Width - 1, 1),
new Point(this.Width -1, this.Height - 2),
new Point(this.Width -2, this.Height-1),
new Point(1, this.Height -1),
new Point(0, this.Height - 2)
};
Point[] inArea = new Point[]
{
new Point(1, 1),
new Point(this.Width - 1, 1),
new Point(this.Width - 1, this.Height - 1),
new Point(this.Width - 1, this.Height - 1),
new Point(1, this.Height - 1)
};
e.Graphics.FillPolygon(new SolidBrush(Color.FromArgb(240, 240, 240)), inArea);
e.Graphics.DrawPolygon(new Pen(Color.FromArgb(55, 0, 0, 0)), area);
base.OnPaint(e);
}
}
现在我的问题是:如何绘制文本框?经过大量谷歌搜索,我想出了以下解决方案:
- 子类化 TextBox 的
OnPaint
方法。这是可能的,尽管我无法让它正常工作。它应该涉及画一些我还不知道怎么做的神奇的东西。 - 做我自己的定制
TextBox
,也许就一个TextBoxBase
。如果有人有好的、有效的和有效的例子,并且认为这可能是一个很好的整体解决方案,请告诉我。 - 使用
BufferedPaintSetAlpha
. (http://msdn.microsoft.com/en-us/library/ms649805.aspx)。这种方法的缺点可能是文本框的角落可能看起来很奇怪,但我可以忍受。如果有人知道如何从 Graphics 对象正确实现该方法,请告诉我。我个人没有,但这似乎是迄今为止最好的解决方案。老实说,我找到了一篇很棒的 C++ 文章,但我懒得转换它。http://weblogs.asp.net/kennykerr/archive/2007/01/23/controls-and-the-desktop-window-manager.aspx
注意:如果我成功使用 BufferedPaint 方法,我发誓我将制作一个简单的 DLL,其中包含可在玻璃上绘制的所有常见 Windows 窗体控件。