创建半透明面板/控制。有没有万无一失的方法? [英] Creating semi-transparent panels/controls. Is there a foolproof way?
问题描述
我试图创建 System.Windows.Forms.Panel
导出一个半透明的控制。 :
I am trying to create a semi-transparent control derived from System.Windows.Forms.Panel
. :
我已经通过大量的网络文章以及SO问题消失了,都拿出了这一点:
I have gone through numerous web articles as well as SO questions and have come up with this:
class SeeThroughPanel : Panel
{
public SeeThroughPanel()
{
}
protected override CreateParams CreateParams {
get {
var cp = base.CreateParams;
cp.ExStyle |= 0x00000020;
return cp;
}
}
protected override void OnPaint(PaintEventArgs e)
{
//base.OnPaint(e);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(50, 0, 0, 0)), this.ClientRectangle);
}
}
这会导致这样(请注意,这code也不会使它真正半透明的,上面的图像显示在全彩色按钮,不像。):
Which results this (Note that this code too does not make it really semi-transparent. The button is displayed in full color, unlike in above image):
然而,当我做一些事情,导致的半透明面板的范围内的任何其他控件重画,这是透支了(在这种情况下,我已滚动的底层控制):
However, as soon as I do something that causes repaint of any other controls within the bounds of the semi-transparent panel, it is overdrawn (In this case, I have scrolled the underlying control):
所以我的问题是,如何才能避免这种情况?这里的的的某种方式。毕竟,Web浏览器处理这种渲染所有的时间。
So my question is, how can I avoid this? There has to be some way. After all, web browsers handle this kind of rendering all the time.
推荐答案
我现在已经做了一些测试。
I have done a few tests now.
正在mixiing几个问题成一体,所以让我们把他们分开:
You are mixiing several problems into one, so let's take them apart:
-
没有web浏览器使用的WinForms C#编写的。因此,这是没有理由这必须是可能的。
No WebBrowser is written in C# using Winforms. So this is no reason why this must be possible.
您的按钮几乎可以肯定不是在窗体的控件集合。您需要为脚本,它是还是使用一个小窍门UI(*),以将其放置在另一个控制,而不将其添加到它。
Your button almost certainly is not in the Form's Controls collection. You need to either script it to be or use a little UI trick (*) to place it over another Control without adding it to it.
在滚动,你会看到一切滚动控制报告,因为其表面。
When you scroll you will see whatever the scrolling control report as its surface.
下面是两张截图:
这是启动后:
..这是我后滚动有点向右:
..and this is after I scrolled a little to the right:
我用这个code,确保Z顺序是正确的:
I have use this code to make sure the Z-order is right:
button1.Parent = this;
panel1.BringToFront();
button1.BringToFront();
seeThroughPanel1.BringToFront();
请注意如何按钮的空间更是做足了;这显示了旧表面正在被使用。
Note how the the space of the button is spared; this shows how the old surface is being used.
要解决这一点,你就一定得在目前的形式surfacse(也许Control.DrawToBitmap),然后使用的,正确的部分画你的半透明面板。当然,它会隐藏你捕捉窗体的当前表面之前。
To work around this you would have to get at the current Form surfacse (maybe by Control.DrawToBitmap) and then use the right part of that to paint your 'semi-transparent' panel. Of course it would have to hide before you capture the form's current surface.
所有可怕的想法我有过,这似乎是一个最糟糕的。
Of all terrible ideas I have had, this seems to be one of the worst..
*诀窍是将其移动到容器与键盘,不可以鼠标;这一招也只是移动不chnging其父容器。也有用放置控件上的TabControl的标签来...但我太懒了,所以我codeD吧..
* The trick is to move it over the container with the keyboard, not the mouse; with this trick it just moves without chnging its parent container. Also useful for placing a Control on top of the tabs of a TabControl... But I was too lazy for that, so I coded it..
这篇关于创建半透明面板/控制。有没有万无一失的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!