透明的对象 [英] Transparrent an Object
本文介绍了透明的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好。
我的表格中有两个对象(Visual Studio)
Panel1
ListView
Panel1在ListView之上。
但是当我将Panel1的背景设置为Transparency时,它只显示了表格的背景图片。
我需要在这个区域看到我的ListView,而不是表格的背景。
任何帮助请??? ??? div class =h2_lin>解决方案
透明不像你在WinForms中那样工作。您准确描述了应该发生的行为。原因是因为当控件放在屏幕上时,它拥有它在屏幕上占据的像素的唯一所有权。只有一个控件将自身绘制成这些像素。 Transparent告诉控件采用其父容器的背景属性,即您的情况下的表单。
更好的解决方案是切换到不存在此限制的WPF。在WinForms中,你必须自己做一些自定义所有者的控件绘制。
参见: WinForms:如何创建一个对其他控件透明的控件
编辑:当我最初发布时,我刚看了MS文章附带的代码。看了之后,我意识到它有一些实现错误(请参阅MS示例)。但是,即使编码错误是固定的,所使用的技术也是错误的。它不会绘制模糊控件的非客户区域。
这是一个简单的透明面板控件。请注意,在设计器中,面板不会透明。其原因在于,由于使用的方法,如果面板或其内容透明,则很难编辑面板或其内容。有可能编写一个控件设计器,以便在允许透明的同时进行简单的编辑,但我现在没有时间去研究这个,我也不喜欢写作设计师。 :)
此控件通过限制控件可以绘制的允许区域来获得透明效果。如果你没有画它,你可以看到它。
公共类TransparentPanel
继承面板
受保护的覆盖Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
SetPaintableRegion()
MyBase.OnPaint(e)
End Sub
Private Sub SetPaintableRegion()
'将paintable区域设置为整个控制区域
Dim PaintableRegion As Region = New Region(New Rectangle(0,0,Me.Width,Me.Height))
'如果在设计中,不要做透明
如果不是Me.DesignMode那么
'构建一个代表客户区的矩形
Dim clientrect As Rectangle = Me.ClientRectangle
'需要相对于Control.Location设置clientrect的位置
Dim offset As Point = ClientOriginRelativeToLocation(Me)
clientrect.X = offset.X
clientrect .Y = offset.Y
'从PaintableRegion中减去clientrect
PaintableRegion.Xor(clientrect)
'现在需要添加子控件的区域
For each c As Control In Me.Controls
Dim r As Rectangle = c.Bounds
rX + = offset.X
rY + = offset.Y
PaintableRegion.Union(r)
Next
End If'不是Me.DesignMode
如果Me.Region IsNot Nothing那么
Me.Region.Dispose()
Else
Me.Region = PaintableRegion
End If
End Sub'SetPaintableRegion
公共共享函数ClientOriginRelativeToLocation(ByVal c As Control)As Point
Dim clientorigin As Point = c.PointToScreen(Point.Empty)
Dim parentorigin As Point = Point.Empty
如果c.Parent IsNot Nothing那么
parentorigin = c.Parent.PointToScreen(c.Location)
结束如果
返回新的点((CLI entorigin.X - parentorigin.X),(clientorigin.Y - parentorigin.Y))
结束函数'ClientOriginRelativeToLocation
结束类'TransparentPanel
Hi guys.
I have two objects in my form (Visual Studio)
Panel1
ListView
The Panel1 is over the ListView.
But when I make the background of Panel1 to Transparency, it just show me the background picture of form.
I need to see my ListView in this area, not background of form.
Any help please???
解决方案
Transparent doesn't work the way you think it does in WinForms. You described exactly the behavior that is supposed to happen. The reason is because when a control is put on the screen it had sole ownership of the pixels it occupies on screen. Only one control paints itself into those pixels. Transparent tells a control to take on the background properties of its parent container, the form in your case.
A better solution is to switch to WPF where this limitation doesn't exist. In WinForms you have to do some custom owner drawing of controls yourself.
see: WinForms: How to create a control transparent to other controls
Edit: When I originally posted, I had just glanced at that code that accompanies the MS article. After looking at it, I realized that it has some implementation errors (go figure for a MS example). However, even if the coding errors are fixed, the technique used is in error. It does not paint the non-client area of obscured controls.
Here is a simplistic transparent panel control. Note that while in the designer, the panel will not be transparent. The reason for this is that because of the method used, it is difficult to edit the panel or its contents if it transparent. It may be possible to write a control designer to allow for easy editing while allowing it to be transparent, but I do not have the time right now to investigate this and I also dislike writing designers. :)
This control obtains the transparent effect by limiting the allowed region into which the control can paint. If you don't paint over it, you can see it.
Public Class TransparentPanel Inherits Panel Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) SetPaintableRegion() MyBase.OnPaint(e) End Sub Private Sub SetPaintableRegion() ' intially set the paintable region as the entire control area Dim PaintableRegion As Region = New Region(New Rectangle(0, 0, Me.Width, Me.Height)) ' if in design, do not do transparent If Not Me.DesignMode Then ' build a rectangle that represents the client area Dim clientrect As Rectangle = Me.ClientRectangle ' need to set the location of clientrect relative to Control.Location Dim offset As Point = ClientOriginRelativeToLocation(Me) clientrect.X = offset.X clientrect.Y = offset.Y ' subtract clientrect from PaintableRegion PaintableRegion.Xor(clientrect) ' now need to add in areas for child controls For Each c As Control In Me.Controls Dim r As Rectangle = c.Bounds r.X += offset.X r.Y += offset.Y PaintableRegion.Union(r) Next End If 'Not Me.DesignMode If Me.Region IsNot Nothing Then Me.Region.Dispose() Else Me.Region = PaintableRegion End If End Sub 'SetPaintableRegion Public Shared Function ClientOriginRelativeToLocation(ByVal c As Control) As Point Dim clientorigin As Point = c.PointToScreen(Point.Empty) Dim parentorigin As Point = Point.Empty If c.Parent IsNot Nothing Then parentorigin = c.Parent.PointToScreen(c.Location) End If Return New Point((clientorigin.X - parentorigin.X), (clientorigin.Y - parentorigin.Y)) End Function 'ClientOriginRelativeToLocation End Class ' TransparentPanel
这篇关于透明的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文