我想在窗体的扩展玻璃框上绘制文本框。我不会描述这种技术,这是众所周知的。以下是一些没有听说过的例子:http://www.danielmoth.com/Blog/Vista-Glass-In-C.aspx在没有WPF的扩展玻璃框中绘制文本框
问题是,画这个玻璃框很复杂。由于黑色被认为是0-alpha颜色,黑色消失。
有明显的方法来解决这个问题:绘制复杂的GDI +形状不受这个alpha-ness的影响。例如,该代码可以被用来绘制在玻璃上的标签(注:GraphicsPath
为了避开可怕的ClearType问题是用来代替DrawString
):
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);
}
}
现在我的问题是:如何绘制文本框? 经过大量的谷歌搜索,我想出了以下解决方案:
- 子类文本框的
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方法成功了,我发誓S/O,我会做一个简单的DLL与所有常见的Windows窗体控件可绘制在玻璃上。
回答自己在另一个线程:http://stackoverflow.com/questions/7061531/rendering-controls-on-glass-solution-found-needs-double-buffering-perfecting – Lazlo 2012-01-12 04:54:23