0

With the code below, I can click-through but I have to click on the form at the same time because I want a transparent form to click behind but I have to put a song when I click every time.

This is the code

Imports System.Runtime.InteropServices

Public Class GhostForm

    Private InitialStyle As Integer
    Private PercentVisible As Decimal

    Public Property WS_EX_TRANSPARENT As Integer
    Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As System.IntPtr, ByVal nIndex As Integer) As Integer
    Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As System.IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
    Declare Function SetLayeredWindowAttributes Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal crKey As Integer, ByVal alpha As Byte, ByVal dwFlags As Integer) As Boolean

    Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

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

        SetWindowLong(Me.Handle, -20, InitialStyle Or &H80000 Or &H20)
        SetLayeredWindowAttributes(Me.Handle, 0, 255 * PercentVisible, &H2)

        Me.TopMost = True

    End Sub

    Public Sub ClickHandler(sender As Object, e As MouseEventArgs) Handles Me.MouseClick, Label1.MouseClick, Button1.MouseClick
        Label1.Text = String.Format("Clicked ""{0}"" with the {1} mouse button.", sender.name, e.Button.ToString.ToLower)
        Select Case e.Button
            Case MouseButtons.Left
                Label1.Text = "Left"
            Case MouseButtons.Right
                Label1.Text = "Right"
            Case MouseButtons.Middle
                Label1.Text = "Middle"
            Case Else
                Label1.Text = "Some other button"
        End Select
    End Sub

    Private Sub GhostForm_Click(sender As Object, e As EventArgs) Handles Me.Click
        Console.Beep()
        Dim sp As New System.Media.SoundPlayer("C:\WINDOWS\MEDIA\Ding.wav")
        sp.Play()
        sp.Dispose()
    End Sub

End Class

How can I have a song when a click-through the form?

So I rephrase my question. I have touch screens and the supported drivers don't give me the "beep" option so I want to develop a small app that will allow to hear a beep every time the customer touches the touch screen. to do this I have to create a transparent form that will allow me to click on what is behind the form. What this code does perfectly, however, it does not support my click function because the click passes through it. so the question is: How can I get audio output when I click through the form?

  • Why SHOUT at us? please edit your title. – Solar Mike Mar 16 '22 at 19:10
  • done, sorry bruh! ^^' – P-a Gagnon Mar 16 '22 at 19:14
  • 3
    Hello and welcome to SO! There's no question. Please update your post so we can further help. – Trevor Mar 16 '22 at 19:41
  • its done Trevor, sorry again. – P-a Gagnon Mar 16 '22 at 19:44
  • Note that when you set the `Opacity` Property of a Form, the .Net Framework [already calls SetLayeredWindowAttributes()](https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Form.cs,6450) -- To set `WS_EX_TRANSPARENT`, you usually override `CreateParams`. -- On *logical side*, it's not clear what effect you want to achieve and what doesn't come out as expected. – Jimi Mar 16 '22 at 19:49
  • So I rephrase my question. I have touch screens and the supported drivers don't give me the "beep" option so I want to develop a small app that will allow to hear a beep every time the customer touches the touch screen. to do this I have to create a transparent form that will allow me to click on what is behind the form. What this code does perfectly, however, it does not support my click function because the click passes through it. so the question is: How can I get audio output when I click through the form? – P-a Gagnon Mar 16 '22 at 19:58
  • You mean [something like this](https://stackoverflow.com/a/62433353/7444103)? – Jimi Mar 16 '22 at 20:16
  • Not exactly because we see in his example that it allows him to have an aura around the cursor and pick color in his click but there is no interaction with what is behind. thanks for help. its really appreciated. – P-a Gagnon Mar 16 '22 at 20:26
  • Hmm, right, that doesn't work as intended anymore. Maybe you can make [this other work](https://stackoverflow.com/a/60688580/7444103) (per-pixel-alpha), but you'd probably prefer a DirectX overlay, e.g. [GameOverlay.Net](https://github.com/michel-pi/GameOverlay.Net) – Jimi Mar 16 '22 at 21:00
  • 2
    Another option is to [hook low level Mouse input events](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowshookexw), or use [SetWinEventHook()](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwineventhook) (it depends on what events you want to generate a sound for) and ditch the overlay. You're outside WinForms *territory*, though; if you need to handle gestures, testing the whole thing can take a long time. – Jimi Mar 16 '22 at 21:17

0 Answers0