简体   繁体   English

如何制作真正透明的控件?

[英]How do I make a genuinely transparent Control?

I need to make a Control which shows only an outline, and I need to place it over a control that's showing a video. 我需要创建一个只显示轮廓的控件,我需要将它放在显示视频的控件上。 If I make my Control transparent, then the video is obscured, because transparent controls are painted by their parent control and the video isn't painted by the control; 如果我使我的控件透明,那么视频会被遮挡,因为透明控件是由其父控件绘制的,而视频不是由控件绘制的; it's shown using DirectShow or another library, so instead the parent control paints its BackColor. 它使用DirectShow或其他库显示,因此父控件绘制其BackColor。

So - can I make a control that doesn't get painted at all , except where it's opaque? 所以-我可以说没有得到画可言 ,除非它是不透明的控制? That way, the parent control wouldn't paint over the video. 这样,父控件就不会在视频上绘画。

I know I could make the border out of four controls (or more if I want it dashed) but is it possible to do what I want using just one control? 我知道我可以用四个控件制作边框(或者更多,如果我想要它破了)但是有可能只使用一个控件做我想要的吗?


rslite is right - although you don't even need to go so far as to use PInvoke like his example does - the Control.Region property is entirely sufficient. rslite是对的 - 尽管你甚至不需要像他的例子一样使用PInvoke - Control.Region属性就足够了。

You could try to make a Region with a hole inside and set the control region with SetWindowRgn. 您可以尝试创建一个内部有一个洞的区域,并使用SetWindowRgn设置控制区域。

Here is an example (I couldn't find a better one). 这是一个例子 (我找不到更好的一个)。 The idea is to create two regions and subtract the inner one from the outer one. 想法是创建两个区域并从外部区域中减去内部区域。 I think that should give you what you need. 我认为应该给你你需要的东西。

I use an overridden function for that from the class control. 我使用类控件的重写函数。

  1. The createparams property now indicates that the control can be transparent. createparams属性现在指示控件可以是透明的。

  2. InvalidateEx is necessary to invalidate the parent's region where the control is placed 必须使用InvalidateEx使放置控件的父区域无效

  3. You have to disable the automatic paint of the backcolor from the control (') 您必须从控件(')禁用背景颜色的自动绘制

     Imports System.Windows.Forms.Design Imports System.Reflection Public Class TransparentControl : Inherits Control Protected Overrides ReadOnly Property CreateParams As CreateParams Get Dim cp As CreateParams = MyBase.CreateParams() cp.ExStyle = cp.ExStyle Or 32 'WS_EX_TRANSPARENT Return cp End Get End Property Protected Sub InvalidateEx(rct As Rectangle) Me.Invalidate(rct) If IsNothing(Parent) Then Exit Sub Parent.Invalidate(New Rectangle(Me.Location, rct.Size), True) End Sub Protected Sub InvalidateEx() Me.Invalidate() If IsNothing(Parent) Then Exit Sub Parent.Invalidate(New Rectangle(Me.Location, Me.Size), True) End Sub Protected Overrides Sub OnPaintBackground(pevent As PaintEventArgs) 'MyBase.OnPaintBackground(pevent) End Sub Protected Overrides Sub OnPaint(e As PaintEventArgs) MyBase.OnPaint(e) 'draw the layout on e.Graphics End Sub End Class 

You can extend this class to make your own control. 您可以扩展此类以进行自己的控制。 After debugging the class will appear in the toolbox. 调试后,该类将出现在工具箱中。

Hope this does the trick. 希望这可以解决问题。

You could try setting the Form.TransparencyKey property. 您可以尝试设置Form.TransparencyKey属性。 Failing that, you could use DirectX to get access to the frame buffer and draw directly to it. 如果做不到这一点,您可以使用DirectX来访问帧缓冲区并直接绘制到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM