0

I'm using the following code to make a vb.net form click through and transparent:

Dim InitialStyle As Integer
'Dim PercentVisible As Decimal

InitialStyle = GetWindowLong(Me.Handle, -20)
'PercentVisible = 0.4

SetWindowLong(Me.Handle, -20, InitialStyle Or &H80000 Or &H20)

'SetLayeredWindowAttributes(Me.Handle, 0, 255 * PercentVisible, &H2)

Me.BackColor = System.Drawing.Color.LightBlue
Me.TransparencyKey = System.Drawing.Color.LightBlue

Me.TopMost = True

it does work on my development machine (windows 10). It does work on a VM guest windows 7 (on the same host). It does not work (the form is not click through) in a laptop with windows 7 service pack 1, it does not work in another computer with windows 7 and it does not work in a laptop with windows 10 home edition. the pc where it works has 64bit OS, the same where it does not. But also does not on some 32bit

What is the problem here? Is there a way to fix this issue? Thank you in advance.

P.S. I have commented out the SetLayeredWindowAttributesbecause it's not needed to make it transparent (even with it enabled, does not affect it).

MirrorMirror
  • 186
  • 8
  • 36
  • 70
  • Don't call `SetWindowLong()` (which, btw, should be `SetWindowLongPtr()`), override `CreateParams`. Take the code [from here](https://stackoverflow.com/a/60688580/7444103). – Jimi Mar 12 '21 at 16:26
  • @Jimi still the same, does not work on the same machines. Code: `Dim parms As CreateParams = MyBase.CreateParams parms.ExStyle = parms.ExStyle Or &H80000 Or &H20` – MirrorMirror Mar 12 '21 at 16:42
  • I don't see where you're **overriding** CreateParams. Did you copy that code I linked? Or, better, take the whole Form and the helper/wrapper methods and try that. – Jimi Mar 12 '21 at 16:50
  • @Jimi i edited my post, i'm overriding CreateParams, now it's worse: does nothing at all. – MirrorMirror Mar 12 '21 at 17:22
  • Please, take that code and test it. Remove everything from `Form.Load`, override and `OnLoad()` to only first select a transparent/semi-transparent Bitmap into that DC. If you want to add `WS_EX_TRANSPARENT` to `WS_EX_LAYERED`, do this also in the `CreateParams` override. Note that a per-pixel alpha Layered Form with the Transparent style attached, cannot be interacted with in any way. – Jimi Mar 12 '21 at 17:27
  • @Jimi got it working, updated code in the first post – MirrorMirror Mar 12 '21 at 18:06
  • 1
    I've rolled back your edit. It is not appropriate to edit the solution into the question itself. If you found a solution and want to share it, do so by writing an answer in the space provided for that purpose below. See [Can I answer my own question?](http://stackoverflow.com/help/self-answer) for more information. – Ken White Mar 12 '21 at 18:47

1 Answers1

0

the following solves my problem thanks to Jimi's comments:

Protected Overrides ReadOnly Property CreateParams As CreateParams
    Get
        Dim parms As CreateParams = MyBase.CreateParams
        If Not DesignMode Then parms.ExStyle = parms.ExStyle Or &H80000 Or &H20
        Return parms
    End Get
End Property
Private Sub frmDrawing_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim InitialStyle As Integer
    'Dim PercentVisible As Decimal

    InitialStyle = GetWindowLong(Me.Handle, -20)
    'PercentVisible = 0.4
    Dim parms As CreateParams = MyBase.CreateParams
    'parms.ExStyle = parms.ExStyle Or &H80000 Or &H20
    'SetWindowLong(Me.Handle, -20, InitialStyle Or &H80000 Or &H20)
    'MsgBox(CStr(parms.ExStyle) & ":" & CStr(InitialStyle Or &H80000 Or &H20))
    SetWindowLong(Me.Handle, -20, parms.ExStyle)

    'SetLayeredWindowAttributes(Me.Handle, 0, 255 * PercentVisible, &H2)

    Me.BackColor = System.Drawing.Color.LightBlue
    Me.TransparencyKey = System.Drawing.Color.LightBlue

    Me.TopMost = True
End Sub
MirrorMirror
  • 186
  • 8
  • 36
  • 70