VB.Net Photo Booth Project - vb.net

So my older sister's high school graduation is coming up soon. She really enjoy's looking at pictures of her friend so I had this idea to make a photo booth style program. At this point, I have the webcam to display on a picturebox, however the click and mouseclick subroutines are not working. Does anyone know why this could be?
Private Sub picCapture_MouseClick(sender As Object, e As MouseEventArgs) Handles picCapture.MouseClick
MsgBox("hi")
End Sub
Dim iHeight As Integer = picCapture.Height
Dim iWidth As Integer = picCapture.Width
hHwnd = capCreateCaptureWindowA(DeviceID.ToString, WS_VISIBLE Or WS_CHILD, 0, 0, 1280, _
1024, picCapture.Handle.ToInt32, 0)
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, DeviceID, 0) Then
SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
'Start previewing the image from the camera
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, _
SWP_NOMOVE Or SWP_NOZORDER)
Else
' Error connecting to device close window
DestroyWindow(hHwnd)
End If
Any help is appreciated. :)

Related

VB.net Autoclick sound file play on top of each other

I've been trying for a really long time now to play an audio file that is like a click with a Timer that clicks at the same time as the mouse clicks as an autoclicker, but I haven't gotten anything to work...
Here is my code:
Private Sub Click_Tick(sender As Object, e As EventArgs) Handles Click.Tick
mouse_event(mouseclickdown, 0, 0, 0, 0)
Dim AudioFile = New MCIPlayer(Me, TextBox3.Text)
AudioFile.Play(AudioPlayMode.Background)
Thread.Sleep(20)
mouse_event(mouseclickup, 0, 0, 0, 0)
End Sub
I used MCI Player that I found from another post, that got the best result so far, but it still doesn't work like I want it to.
I am on Visual Studio 2022 "net6.0-windows" target framework.

Trying to create a simple while loop using Rbutton VB.Net

Not important:
I started learning VB.Net yesterday, I'm learning basic functions that I use in other languages, but I don't find much direct content about VB.Net
The real question:
I have no idea how i can use while RButton down loop something... ill send what i need to loop, if someone can help me...
Edit: I found another problem, i can send the keys in game chatbox, but the real function of the keys isnt working)
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
KEYB_Event(VK_KEY_Q, 0, 0, 0)
KEYB_Event(VK_KEY_Q, 0, 0, 0)
KEYB_Event(VK_KEY_Q, 0, KEYEVENTF_KEYUP, 0)
Thread.Sleep(5)
KEYB_Event(VK_KEY_W, 0, 0, 0)
KEYB_Event(VK_KEY_W, 0, 0, 0)
KEYB_Event(VK_KEY_W, 0, KEYEVENTF_KEYUP, 0)
Thread.Sleep(5)
KEYB_Event(VK_KEY_E, 0, 0, 0)
KEYB_Event(VK_KEY_E, 0, 0, 0)
KEYB_Event(VK_KEY_E, 0, KEYEVENTF_KEYUP, 0)
Thread.Sleep(5)
KEYB_Event(VK_KEY_E, 0, KEYEVENTF_KEYUP, 0)
End Sub

Mouse event wheel occurs last

something strange is happening in my application and I can't figure out what it is. I am trying to simulate Mouse click and scroll programmatically.
I can successfully perform Mouse click and scroll programmatically, but the problem is that the mouse wheel occurs last.
This is the code:
<DllImport("user32.dll", EntryPoint:="mouse_event")>
Private Shared Sub mouse_event(ByVal dwFlags As UInteger, ByVal dx As
Integer, ByVal dy As Integer, ByVal dwData As Integer, ByVal dwExtraInfo
As UInteger)
End Sub
Flags:
Const MOUSEEVENTF_LEFTDOWN As UInteger = &H2 '0x0002'
Const MOUSEEVENTF_LEFTUP As UInteger = &H4 '0x0004'
Const MOUSEEVENTF_WHEEL As UInteger = &H800
Const MOUSEEVENTF_XDOWN As UInteger = &H80
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
scroll_to_end()
Thread.Sleep(300)
click_perf()
MessageBox.Show("S")
End Sub
Private Sub scroll_to_end()
'Focus panel1'
c_point = New Point(Panel1.Location.X + 1, Panel1.Location.Y + 1)
Cursor.Position = Me.PointToScreen(c_point)
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
'Wait'
Thread.Sleep(150)
'Scroll to the end of the page'
Dim scroll_down As Integer = -(10 * 120) 'Perform 10 scrolls down'
mouse_event(MOUSEEVENTF_WHEEL, 0, 0, scroll_down, 0)
'This is the fix:'
Application.DoEvents()
End Sub
Private Sub click_perf()
'Move mouse diagonally 200px'
c_point.X += 200
c_point.Y += 200
Cursor.Position = Me.PointToScreen(c_point)
'Perform click'
mouse_event(MOUSEEVENTF_LEFTDOWN, c_point.X, c_point.Y, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End Sub
When I click button3, I want to focus panel1 then scroll to the end and move mouse 200px diagonally downwards and then simulate click.
What actually happens: Mouse moves to panel1, simulates mouse button down and mouse button up. Then, it moves the mouse diagonally 200px and the messagebox shows up. The scroll didn't even occur and now when I close the messagebox panel1 probably loses the focus and doesn't scroll.
If I remove messagebox.show(""): Mouse moves to panel1 and focuses it just like above. It scrolls to the end, but it performs click before it was scrolled down.
EDIT:
To fix this issue we need to put Application.DoEvents() After WHEEL MOUSE EVENT.
Put this after each UI operation to force it to happen in order
Application.DoEvents()
UI events are handled in a message loop, which looks at a queue of messages and handles them. There is no guarantee that they will be handled in the order they are raised if they are raised around the same time in your code because your code is faster than the loop. Application.DoEvents() will halt your code execution and force the loop to handle all pending messages, thus making them happen in order.
It's generally considered poor practice to use this method, however, on account of its widespread misuse. I think your case for it is a good one and maybe the only I've ever seen.
I'd be interested to see if someone can provide a preferred alternative...

Audio Recorder - Select Recording Device - VB.Net

I'm trying to create an audio recorder using VB.Net.
This is how I'm doing it:
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
mciSendString("open new Type waveaudio Alias recsound", "", 0, 0)
mciSendString("set recsound samplespersec 11025 channels 2 bitspersample 16 alignment 4 bytespersec 44100", vbNullString, 0, 0)
mciSendString("record capture", vbNullString, 0, 0)
Label1.Text = "Recording..."
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
mciSendString("save recsound c:\recsound.wav", "", 0, 0)
mciSendString("close recsound", "", 0, 0)
Label1.Text = "Stopped."
End Sub
It works fine, the only problem is that I have two mics, one of them is built-in and the other one is connected via USB. The second one has a way better recording quality but this application always records from the built-in mic.
I've searched all over the internet and I can't find a way to select the recording device. The only thing I was able to find was:
Dim DeviceId As Integer = 2
mciSendString("set recsound input " & DeviceId.ToString())
I've tried different values to no avail.
I have also tried the following code that successfully listed all the properties of all the recording devices found in my computer but I couldn't find anything that could help:
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim objSearcher As New System.Management.ManagementObjectSearcher("SELECT * FROM Win32_SoundDevice")
Dim objCollection As System.Management.ManagementObjectCollection = objSearcher.Get()
Me.TextBox1.Text = ""
For Each obj As ManagementObject In objCollection
Me.TextBox1.Text &= "---------------------------------" & vbCrLf
For Each myProperty As PropertyData In obj.Properties
Me.TextBox1.Text &= myProperty.Name & " - " & myProperty.Value & vbCrLf
Next
Next
End Sub
Any suggestions will be appreciated.
I realize that you asked this question almost two years ago and so you may never see this answer. Perhaps you have notifications turned on and this may help you.
I have switched to Naudio (available through Nuget) for my recording applications and have had much better luck with it. I can specify the recording device as follows:
waveIn = New WaveIn
waveIn.DeviceNumber = 0 '0 - Stereo Mix, 1 - Microphone, 2 - Line In
Figure out what device number your USB microphone is and use that. Do a search on "Naudio" and you will find example code.

How to call public sub using button in VB.Net

In VB.Net, how can I call thePublic Sub using a button click?
Here's what I have so far:
Public Sub SetThreshold(ByVal e As PaintEventArgs)
' Open an Image file, and draw it to the screen.
Dim Image As New Bitmap(PictureBox1.Image)
PictureBox1.Image = Image
e.Graphics.DrawImage(Image, 20, 20)
' Create an ImageAttributes object, and set its color threshold.
Dim imageAttr As New ImageAttributes
imageAttr.SetThreshold(0.7F)
' Draw the image with the colors bifurcated.
Dim rect As New Rectangle(300, 20, 200, 200)
e.Graphics.DrawImage(Image, rect, 0, 0, 200, 200, GraphicsUnit.Pixel, imageAttr)
End Sub