问题描述
我想画我的形式的扩展玻璃框架上一个文本框。我就不一一介绍了这种技术,这是众所周知的。下面是对于那些还没有听说过谁的例子:
I am trying to draw a TextBox on the extended glass frame of my form. I won't describe this technique, it's well-known. Here's an example for those who haven't heard of it: http://www.danielmoth.com/Blog/Vista-Glass-In-C.aspx
的事情是,它是复杂的绘制在这个玻璃框架。由于黑色被认为是在0-α颜色,任何黑色消失
The thing is, it is complex to draw over this glass frame. Since black is considered to be the 0-alpha color, anything black disappears.
有明显对付这个问题的途径:绘制复杂GDI +形状并不受此阿尔法-ness。例如,该代码可以用来绘制在玻璃上的标签(注:的GraphicsPath
则使用的DrawString
的为了避开可怕的ClearType的问题):
There are apparently ways of countering this problem: drawing complex GDI+ shapes are not affected by this alpha-ness. For example, this code can be used to draw a Label on glass (note: GraphicsPath
is used instead of DrawString
in order to get around the horrible ClearType problem):
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);
}
}
类似地,这样的方法可以用于创建在玻璃区域的容器。注意使用的多边形的,而不是矩形 - 利用矩形时,其黑色部分被认为是阿尔法。
Similarly, such an approach can be used to create a container on the glass area. Note the use of the polygons instead of the rectangle - when using the rectangle, its black parts are considered as 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 ?
经过大量谷歌搜索,我想出了以下解决方案:
Now my problem is: How can I draw a TextBox?After lots of Googling, I came up with the following solutions:
- 子类文本框的
的OnPaint
方法。这是的可能的,虽然我不能让它正常工作。它应该包括画一些神奇的事情,我不知道该怎么办呢。 - 制作自己的自定义
文本框
,也许在TextBoxBase
。如果任何人有的好的,有效的和工作的例子,认为这可能是一个很好的整体解决方案,请告诉我。 - 使用
BufferedPaintSetAlpha
。 ()。这种方法的缺点可能是文本框的边角可能看起来很奇怪,但我可以忍受的。如果有人知道如何正确地实施该方法从Graphics对象,请告诉我。我个人不这样做,但这似乎最好的解决办法为止。说实话,我发现了一个伟大的C ++的文章,但我太懒惰将其转换。
- Subclassing the TextBox's
OnPaint
method. This is possible, although I could not get it to work properly. It should involve painting some magic things I don't know how to do yet. - Making my own custom
TextBox
, perhaps on aTextBoxBase
. If anyone has good, valid and working examples, and thinks this could be a good overall solution, please tell me. - Using
BufferedPaintSetAlpha
. (http://msdn.microsoft.com/en-us/library/ms649805.aspx). The downsides of this method may be that the corners of the textbox might look odd, but I can live with that. If anyone knows how to implement that method properly from a Graphics object, please tell me. I personally don't, but this seems the best solution so far. To be honest, I found a great C++ article, but I am way too lazy to convert it. http://weblogs.asp.net/kennykerr/archive/2007/01/23/controls-and-the-desktop-window-manager.aspx
请注意:如果我曾经与BufferedPaint方法成功了,我发誓S / O,我将与所有常见的Windows窗体控件可绘制在玻璃上简单的DLL
Note: If I ever succeed with the BufferedPaint methods, I swear to s/o that I will make a simple DLL with all the common Windows Forms controls drawable on glass.
推荐答案
我花了一些时间在这个话题前一阵子。基本上你需要的是一个透明的文本框。我最初的方法是使用CodeProject上文本框。但是我是有该控件的一些难以解决的问题。有一段时间我已经找到解决方案需要后,只能在Windows XP和后续工作。 ,以及获得这种控制表现得像单行文本框设置RichTextBox.Multiline为false
I have spent some time on this topic a while ago. Basically what you need is a transparent textbox. My initial approach was to use codeproject AlphaBlendTextBox - A transparent/translucent textbox for .NET. But I was having a few difficult to solve issues with that control. After a while I have found required solution, it will work only on Windows XP and up. As well to get this control to behave like single line text box set RichTextBox.Multiline to false.
// Source:
// http://www.dotnetjunkies.com/WebLog/johnwood/archive/2006/07/04/transparent_richtextbox.aspx
// It seems there are 4 versions of the RichEdit control out there - when I'm talking about the
// RichEdit control, I'm talking about the C DLL that either comes with Windows or some version
// of Office. The files are named either RICHEDXX.DLL (XX is the version number), or MSFTEDIT.DLL
// and they're in the System32 folder.
// .Net RichTextBox control is bound to version 2. The biggest problem with this version (at least
// for me) is that it does not render properly if you try to make the window transparent. Later versions,
// however, do.
// We can fix that. If you create a control deriving from the original RichTextBox control, but overriding
// the CreateParams property, you can put in a new Windows class name (this is the window class name,
// nothing to do with classes in the C# sense). This effectively gives us a free upgrade. When the .Net
// RichTextBox control instantiates, it will now use the latest RichEdit control and not the old, archaic,
// version 2.
// There are other benefits too - version 3 and beyond of the RichEdit control support quite an extensive
// array of layout features, such as tables and full text justification. This is the version of the RichEdit
// that WordPad uses in Windows XP. To really see what it's capable of displaying you can create documents in
// Word and save them in RTF, load these into the new RichEdit and in a lot of cases it'll look identical,
// it's that powerful. A full list of features can be found here:
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/richedit/richeditcontrols/aboutricheditcontrols.asp
// There are a couple of caveats:
//
// 1. The control that this is bound to was shipped with Windows XP, and so this code won't work in
// Windows 2000 or earlier.
//
// 2. The RichTextBox control in C# only knows about version 2, so the interface doesn't include
// all the new features. You can wrap a few of the features yourself through new methods on the
// RichEdit class.
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
internal class RichEdit : RichTextBox
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr LoadLibrary(string lpFileName);
protected override CreateParams CreateParams
{
get
{
CreateParams parameters = base.CreateParams;
if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
{
parameters.ExStyle |= 0x020; // transparent
parameters.ClassName = "RICHEDIT50W";
}
return parameters;
}
}
}
这篇关于在绘制扩展玻璃相框一个TextBox W / O WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!