I'm in a position where I have to design a layout editor in winforms - no option. I'm doing this via adding custom controls to a Panel some of which are rendered transparent; the pertinent bits of code for that being:
Public Sub New()
SetStyle(ControlStyles.SupportsTransparentBackColor, True)
SetStyle(ControlStyles.Opaque, True)
Me.backcolor = Color.Transparent
End Sub
Protected Overrides ReadOnly Property CreateParams As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or &H20
Return cp
End Get
End Property
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
' .. various code here - not related to problem
End Sub
I think the code causing the problem is the cp.ExStyle = cp.ExStyle Or &H20
. This is the bit that makes the controls transparent (if I omit it, they get the parent background color). However, I think it is also causing the control's Mousedown
event to 'click through' the control. I'll illustrate this below. This is taken from my prototype:
The two buttons work fine (with no transparency code) if I click on the area where Button 2 overlaps button 1, the Mousedown
for Button 2 gets fired. However, the orange rectangle is behind the image of Miner Willy. If I click on the image, the Mousedown
event gets fired for the orange rectangle, not the image. If I remove the line cp.ExStyle = cp.ExStyle Or &H20
then things work as they should - with the image's Mousedown
event being fired
The difficulty is that I need the line cp.ExStyle = cp.ExStyle Or &H20
as this makes the transparency!
I'd welcome any ideas or workarounds. Thanks.