0

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:

enter image description here

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.

Jimi
  • 29,621
  • 8
  • 43
  • 61
stigzler
  • 793
  • 2
  • 12
  • 29
  • 1
    Use a Label instead of a Panel. Add `SetStyle(ControlStyles.Opaque Or ControlStyles.ResizeRedraw, true) SetStyle(ControlStyles.OptimizedDoubleBuffer, false)` in the Constructor and `AutoSize = false` in `OnLayout()`. Setting `WS_EX_TRANSPARENT` in CreateParams is necessary and doesn't cause side-effects. The Control's surface is *solid*. -- Not sure what the Buttons you're mentioning have to do with this. – Jimi Aug 07 '21 at 13:10
  • Thanks for the reply, Jimi. It's a little ambiguous, though. Few questions: 1. The two `SetStyle` calls - do these go in the custom controls where they are at present? 2. 'AutoSize=False' - where do I put this - in the custom controls or in the main form that holds the (now) label that holds the custom controls. 3. Is "Setting WS_EX_TRANSPARENT" referring to my code: `cp.ExStyle = cp.ExStyle Or &H20`?? I have replaced the panel with the label; replaced the SetStyles in the custom controls with yours and Set the Label Autosize to false. This didn't solve the issues. Could you show some code? – stigzler Aug 07 '21 at 14:25
  • 1
    1. As mentioned, `SetStyle()` goes in the Constructor (`Sub New()` in VB.Net) 2. As mentioned, `Autosize = false` goes in the `OnLayout` method override of your Custom Control. 3. [WS_EX_TRANSPARENT](https://docs.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles) `= &H20` -- You don't need to set or use `Color.Transparent` as the background color, because your Control is already **completely transparent**. You have to paint everything in the `OnPaint()` override. You can draw with opacity, setting the Alpha channel of any Color (e.g., `Color.FromArgb(128, Color.Black)`). – Jimi Aug 07 '21 at 14:44
  • 1
    *Completely transparent* doesn't mean that the Control is not *solid*, because it is: you cannot *click through*. -- If you have issues with `Click` events of your Controls, it's not because of this. If you place your Custom Control over your buttons, the Button.Click event is not raised. Test the Custom Control just drawing the entire surface, in OnPaint(), using a semi-transparent Color. Since now you should have a functional transparent Control, you should be able to overlay any other Control, no matter what the Parent is. – Jimi Aug 07 '21 at 14:51
  • If, for some reason, the issue persists, post enough code that can reproduce the behavior described, including the *various code here - not related to problem* . – Jimi Aug 07 '21 at 15:06
  • BTW, you can get this Custom Control, quite similar to what I described: [Panel overlay not drawn over Labels](https://stackoverflow.com/a/68076648/7444103) -- In C#; if you need a conversion to VB.Net, let me know. – Jimi Aug 08 '21 at 01:55

0 Answers0