Set cursor position - vb.net

So i have this code, i need to be able to move the cursor to a position but i need to get the position from listbox,and move the cursor to all the values,after move was done item to be deleted
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices
Imports System.Collections.Generic
Public Class Form1
Declare Auto Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As IntPtr)
Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
Const MOUSEEVENTF_LEFTUP As Integer = &H4
Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long
Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Long
Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
Dim speed As Integer = 1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Dim c As Color
' c = Image.GetPixel()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim tempPos As Point
Dim R As Long = GetCursorPos(tempPos)
' Label1.Text = "Mouse helye:" + tempPos.ToString + " :)"
Dim bmp As New Bitmap(80, 80)
' Using bmp As New Bitmap(30, 30)
Using g As Graphics = Graphics.FromImage(bmp)
g.CopyFromScreen(Windows.Forms.Cursor.Position, _
New Point(0, 0), New Size(1, 1))
End Using
Label1.Text = bmp.GetPixel(0, 0).ToString
' Me.Invalidate()
' End Using
If Label1.Text = "Color [A=255, R=0, G=163, B=232]" Then
ListBox1.Items.Add(tempPos.X)
ListBox2.Items.Add(tempPos.Y)
Cursor.Position = New Point(Cursor.Position.X + 20 * speed, Cursor.Position.Y)
Else
Cursor.Position = New Point(Cursor.Position.X + 20 * speed, Cursor.Position.Y)
' If ListBox1.Items.Contains(tempPos.X) And Listbox2.items.contains(tempPos.Y) Then
' Else
' ListBox1.Items.Add(tempPos.X)
' ListBox2.Items.Add(tempPos.Y)
' End If
End If
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
Dim D As Boolean
Dim S As Boolean
D = GetAsyncKeyState(Keys.D)
S = GetAsyncKeyState(Keys.S)
If S = True Then
Timer1.Start()
End If
If D = True Then
Timer1.Stop()
End If
End Sub
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
End Sub
End Class

I use this code to fix the error,thank you Visual Vincent for help.
ListBox1.Items.Add(Cursor.Position.X & ";" & Cursor.Position.Y)
For Each item In ListBox1.Items
Dim s As String = item.ToString
Dim words As String() = s.Split(";")
Cursor.Position = New Point(words(0), words(1))
Next

Related

Unable to find RECT size with DwmGetWindowAttribute

I'm trying to find to find the size of the cmd.exe window which is started as a child. I like to use this size to resize my form accordingly. For some reason the size returned by DwmGetWindowAttribute is always zero, so I must be doing something wrong here, but I can't find it. Any help will be greatly appreciated.
Kind regards,
Eric
Imports System.Runtime.InteropServices
Public Class Form1
Private WithEvents Tmr As New Timer With {.Interval = 100}
Private Const HWND_BOTTOM As Integer = &H1
Private WithEvents proc As New Process
Public Const DWMWA_EXTENDED_FRAME_BOUNDS As Integer = 9
<DllImport("user32.dll", EntryPoint:="SetParent")>
Private Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
End Function
<DllImport("user32.dll", EntryPoint:="SetWindowPos")>
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<DllImport("dwmapi.dll")>
Shared Function DwmGetWindowAttribute(ByVal hwnd As IntPtr, ByVal dwAttribute As Integer, ByRef pvAttribute As RECT, ByVal cbAttribute As Integer) As Integer
End Function
Public Structure RECT
Public left, top, right, bottom As Integer
End Structure
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = "My title"
proc.EnableRaisingEvents = True
proc.StartInfo.FileName = "cmd"
proc.Start()
Tmr.Start()
End Sub
Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
If SetParent(proc.MainWindowHandle, Panel1.Handle) <> IntPtr.Zero Then
Tmr.Stop()
Dim Width As Integer
Dim Hight As Integer
Dim WindowRect As New RECT
DwmGetWindowAttribute(proc.MainWindowHandle, DWMWA_EXTENDED_FRAME_BOUNDS, WindowRect, Marshal.SizeOf(WindowRect))
Width = WindowRect.right - WindowRect.left
Hight = WindowRect.bottom - WindowRect.top
MessageBox.Show("Hight: " & Hight & " Width: " & Width)
'Me.Size = New Size(Width, Hight)
SetWindowPos(proc.MainWindowHandle, New IntPtr(HWND_BOTTOM), 0, 0, Panel1.ClientSize.Width, Panel1.ClientSize.Height, 0)
End If
End Sub
Private Sub Proc_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles proc.Exited
Invoke(Sub() Close())
End Sub
End Class
After implementing all valuable additions of #Jimi, this is the code that works:
Imports System.Runtime.InteropServices
Public Class Form1
Private WithEvents proc As New Process
Public Const WM_NCLBUTTONDOWN As Long = &HA1
Public Const SW_SHOWMAXIMIZED As UInt32 = 3
Public Const WM_CLOSE = &H10
Public Const DWMWA_EXTENDED_FRAME_BOUNDS As Integer = 9
'Function to set the parent window
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
'Function to set the child window position
Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As <MarshalAs(UnmanagedType.Bool)> Boolean
'Function to allow the child to be maximized
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Boolean
'Function to retrieve the initail child size
Private Declare Function DwmGetWindowAttribute Lib "dwmapi" (ByVal hwnd As IntPtr, ByVal dwAttribute As Integer, ByRef pvAttribute As RECT, ByVal cbAttribute As Integer) As Integer
'Function to set focus to the child window
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As IntPtr) As Long
'Function used to set terminate child window
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
'Rectangle size, used later to resize the form
Public Structure RECT
Public left, top, right, bottom As Integer
End Structure
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = "My title"
proc.EnableRaisingEvents = True
proc.StartInfo.FileName = "cmd"
proc.Start()
End Sub
Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Shown
'Wait for 200 ms for the form to load
proc.WaitForExit(200)
Dim WindowRect As New RECT
Dim Width, Hight As Integer
'Retrieve the initial size of the child window
DwmGetWindowAttribute(proc.MainWindowHandle, DWMWA_EXTENDED_FRAME_BOUNDS, WindowRect, Marshal.SizeOf(WindowRect))
Width = WindowRect.right - WindowRect.left
Hight = WindowRect.bottom - WindowRect.top + 23
'Set the form size to the initial size of the child window
Me.Size = New Size(Width, Hight)
'When the child is started, move the child into the panel and maximize it
If SetParent(proc.MainWindowHandle, Panel1.Handle) <> IntPtr.Zero Then
SetWindowPos(proc.MainWindowHandle, IntPtr.Zero, 0, 0, Width, Height, 0)
ShowWindow(proc.MainWindowHandle, SW_SHOWMAXIMIZED)
End If
End Sub
'Exit form when child terminates
Private Sub Proc_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles proc.Exited
Invoke(Sub() Close())
End Sub
'Set focus on child when the form is activated
Private Sub Form1_UnFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
SetForegroundWindow(proc.MainWindowHandle)
End Sub
'Set focus on child when the parent titlebar is clicked
Protected Overrides Sub DefWndProc(ByRef m As System.Windows.Forms.Message)
If CLng(m.Msg) = WM_NCLBUTTONDOWN Then
SetForegroundWindow(proc.MainWindowHandle)
End If
MyBase.DefWndProc(m)
End Sub
'Properly terminate child when the form is closed by the user
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
SendMessage(proc.MainWindowHandle, WM_CLOSE, 0, 0)
End Sub
End Class
Kind regards,
Eric

how do I solve unbalance stack in visual basic 2012?

This is error that I got
Additional Information: A call to PInvoke function 'cobawebcamsss!cobawebcamsss.Form1::SendMessage' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
Here is the code :
Option Explicit On
Option Strict Off
Public Class Form1
Const WM_CAP As Short = &H400S
Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30
Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52
Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
Const WS_CHILD As Integer = &H40000000
Const WS_VISIBLE As Integer = &H10000000
Const SWP_NOMOVE As Short = &H2S
Const SWP_NOSIZE As Short = 1
Const SWP_NOZORDER As Short = &H4S
Const HWND_BOTTOM As Short = 1
Dim iDevice As Integer = 0
Dim hHwnd As Integer
' Declare Function
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Object) As Integer
Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
Declare Function capCreateCaptureWindowA Lib "avicap32.dll" (ByVal lpszWindowName As String, ByVal dwStyle As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Short, ByVal hWndParent As Integer, ByVal nID As Integer) As Integer
Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short, ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, ByVal cbVer As Integer) As Boolean
Private Sub LoadDeviceList()
Dim strName As String = Space(100)
Dim strVer As String = Space(100)
Dim bReturn As Boolean
Dim x As Integer = 0
Do
bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
If bReturn Then lstDevices.Items.Add(strName.Trim)
x += 1
Loop Until bReturn = False
End Sub
Private Sub OpenPreviewWindow()
Dim iHeight As Integer = picCapture.Height
Dim iWidth As Integer = picCapture.Width
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, 480, picCapture.Handle.ToInt32, 0)
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, SWP_NOMOVE Or SWP_NOZORDER)
btnSave.Enabled = True
btnStop.Enabled = True
btnStart.Enabled = False
Else
DestroyWindow(hHwnd)
btnSave.Enabled = False
End If
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim data As IDataObject
Dim bmap As Image
SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
data = Clipboard.GetDataObject()
If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
picCapture.Image = bmap
ClosePreviewWindow()
btnSave.Enabled = False
btnStop.Enabled = False
btnStart.Enabled = True
If sfdImage.ShowDialog = DialogResult.OK Then
bmap.Save(sfdImage.FileName, Imaging.ImageFormat.Bmp)
End If
End If
End Sub
Private Sub ClosePreviewWindow()
SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)
DestroyWindow(hHwnd)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadDeviceList()
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
OpenPreviewWindow()
btnStart.Enabled = False
btnStop.Enabled = True
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
ClosePreviewWindow()
btnStart.Enabled = True
btnStop.Enabled = False
End Sub
End Class
and it got highlighted here when I break the operation
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
please help, I am new in visual basic so I try to learn from others program but it didn't work, it is originally from visual basic 2010 and I copied it to visual basic 2012
If you are getting an error about PInvoke..SendMessage, try replacing the line:
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Object) As Integer
With:
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByRef RECT As IntPtr) As IntPtr
After that, worked fine on my Win7 x64.

How can i programmatically click a external Application Using VB.net

I have created a test software to test our product, but in some cases other customer provide there API/Tool and etc... to test there specific parameter.
I want is to click the button on other the application (Automatically Click).
Basically we want it so the operator will not click the wrong button even there have a work instruction.
Try this:
Private Const BM_CLICK = &HF5
Private Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As IntPtr
Private Declare Auto Function FindWindowEx Lib "user32.dll" (ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim hWndMsgBox, hWndButton As Long
hWndMsgBox = FindWindow("#32770", "About")
If hWndMsgBox Then hWndButton = FindWindowEx(hWndMsgBox, 0&, "Button", "OK")
If hWndButton Then SendMessage(hWndButton, BM_CLICK, 0&, 0&)
End Sub
'------------------------------FORM-----------------------------------------------
Public Class Form1
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
LeftClick()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If CommandType.Text = "Start" Then
Timer1.Enabled = True
CommandType.Text = "Stop"
Else
Timer1.Enabled = False
CommandType.Text = "Start"
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MoveMouse(300, 75)
LeftClick()
LeftClick()
End Sub
End Class
'------------------------------MODULE----------------------------------------------
Module Module1
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
Public Declare Function SetCursorPos Lib "user32" (ByVal x As Integer, ByVal y As Integer) As Integer
Public Declare Function GetCursorPos Lib "user32" (ByVal lpPoint As POINTAPI) As Integer
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20
Public Const MOUSEEVENTF_MIDDLEUP = &H40
Public Const MOUSEEVENTF_RIGHTDOWN = &H8
Public Const MOUSEEVENTF_RIGHTUP = &H10
Public Const MOUSEEVENTF_MOVE = &H1
Public Structure POINTAPI
Dim x As Integer
Dim y As Integer
End Structure
Public Function GetX() As Integer
: Dim n As POINTAPI
GetCursorPos(n)
GetX = n.x
End Function
Public Function GetY() As Integer
Dim n As POINTAPI
GetCursorPos(n)
GetY = n.y
End Function
Public Sub LeftClick()
LeftDown()
LeftUp()
End Sub
Public Sub LeftDown()
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
End Sub
Public Sub LeftUp()
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End Sub
Public Sub MiddleClick()
MiddleDown()
MiddleUp()
End Sub
Public Sub MiddleDown()
mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0)
End Sub
Public Sub MiddleUp()
mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)
End Sub
Public Sub RightClick()
RightDown()
RightUp()
End Sub
Public Sub RightDown()
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
End Sub
Public Sub RightUp()
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
End Sub
Public Sub MoveMouse(ByVal xMove As Integer, ByVal yMove As Integer)
mouse_event(MOUSEEVENTF_MOVE, xMove, yMove, 0, 0)
End Sub
Public Sub SetMousePos(ByVal xPos As Integer, ByVal yPos As Integer)
SetCursorPos(xPos, yPos)
End Sub
End Module
This will allow you to Left click, Right click and move cursor base on X and Y Axis.

VB.net Click through form

I'm trying to make a program for my late night star gazing, I need my laptop screen to be only red, so I want to make a program that acts as a red filter. It would cover the whole screen and be transparent + red. The user can click through it, it would be just like putting a piece of transparent red plastic in-front of the screen.
So far I have a form that sizes itself to what ever your screen size is, and moves itself to the upper left corner. It is slightly transparent and red.
I need to make all clicks on the form pass through, as I will eventually make the form transparent and red, but I don't want the user to be able to interact with it.
Program is called "Red_Filter"
Public Class Form1
Dim Screens As Array
Dim TotalWidth As Integer
Dim TotalHeight As Integer
Dim Heights As List(Of Integer) = New List(Of Integer)
'Load / Close
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Screens = Screen.AllScreens
For I As Integer = 0 To UBound(Screens)
TotalWidth += Screens(I).Bounds.Width
Heights.Add(Screens(I).Bounds.Height)
Next
TotalHeight = Heights.Max()
Me.Width = TotalWidth
Me.Height = TotalWidth
Me.Location = New Point(0, 0)
Me.BackColor = Color.Red
Me.Opacity = 0.5
Me.TopMost = True
'Make it click through
SetWindowLong(Me.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT)
End Sub
'Click Through Functionality
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Const GWL_EXSTYLE = -20
Const WS_EX_TRANSPARENT = &H20
End Class
This is what I have so far, the part after "'Click Through Functionality" I found online, however, it gives me this error:
A call to PInvoke function 'Red Filter!Red_Filter.Form1::SetWindowLong' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
I do not know how the code I found online works, but the error happens in the last line of the form's load event.
Does anyone know how to make a form click-through?
I ripped code from this codeproject post: http://www.codeproject.com/Articles/12877/Transparent-Click-Through-Forms
Here's a complex version with all the comment-ey goodness:
Imports System.Runtime.InteropServices
Public Class Form1
Private InitialStyle As Integer
Dim PercentVisible As Decimal
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Grab the Extended Style information
' for this window and store it.
InitialStyle = GetWindowLong(Me.Handle, GWL.ExStyle)
PercentVisible = 0.8
' Set this window to Transparent
' (to the mouse that is!)
' This creates a new Extended Style
' for our window, which takes effect
' immediately upon being set, that
' combines the initial style of our window
' (saved in Form.Load) and adds the ability
' to be Transparent to the mouse.
' Both Layered and Transparent must be
' turned on for this to work AND have
' the window render properly!
SetWindowLong(Me.Handle, GWL.ExStyle, InitialStyle Or WS_EX.Layered Or WS_EX.Transparent)
' Don't forget to set the Alpha
' for the window or else you won't be able
' to see the window! Possible values
' are 0 (visibly transparent)
' to 255 (visibly opaque). I'll set
' it to 70% visible here for show.
' The second parameter is 0, because
' we're not using a ColorKey!
SetLayeredWindowAttributes(Me.Handle, 0, 255 * PercentVisible, LWA.Alpha)
' Just for giggles, set this window
' to stay on top of all others so we
' can see what's happening.
Me.TopMost = True
Me.BackColor = Color.Red
End Sub
Public Enum GWL As Integer
ExStyle = -20
End Enum
Public Enum WS_EX As Integer
Transparent = &H20
Layered = &H80000
End Enum
Public Enum LWA As Integer
ColorKey = &H1
Alpha = &H2
End Enum
<DllImport("user32.dll", EntryPoint:="GetWindowLong")> _
Public Shared Function GetWindowLong( _
ByVal hWnd As IntPtr, _
ByVal nIndex As GWL _
) As Integer
End Function
<DllImport("user32.dll", EntryPoint:="SetWindowLong")> _
Public Shared Function SetWindowLong( _
ByVal hWnd As IntPtr, _
ByVal nIndex As GWL, _
ByVal dwNewLong As WS_EX _
) As Integer
End Function
<DllImport("user32.dll", _
EntryPoint:="SetLayeredWindowAttributes")> _
Public Shared Function SetLayeredWindowAttributes( _
ByVal hWnd As IntPtr, _
ByVal crKey As Integer, _
ByVal alpha As Byte, _
ByVal dwFlags As LWA _
) As Boolean
End Function
End Class
And here's the simplified version I'm using that makes more sense to me:
Imports System.Runtime.InteropServices
Public Class Form1
Private InitialStyle As Integer
Dim PercentVisible As Decimal
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
InitialStyle = GetWindowLong(Me.Handle, -20)
PercentVisible = 0.8
SetWindowLong(Me.Handle, -20, InitialStyle Or &H80000 Or &H20)
SetLayeredWindowAttributes(Me.Handle, 0, 255 * PercentVisible, &H2)
Me.BackColor = Color.Red
Me.TopMost = True
End Sub
<DllImport("user32.dll", EntryPoint:="GetWindowLong")> Public Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
End Function
<DllImport("user32.dll", EntryPoint:="SetWindowLong")> Public Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
End Function
<DllImport("user32.dll", EntryPoint:="SetLayeredWindowAttributes")> Public Shared Function SetLayeredWindowAttributes(ByVal hWnd As IntPtr, ByVal crKey As Integer, ByVal alpha As Byte, ByVal dwFlags As Integer) As Boolean
End Function
End Class
Try this:
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Const WS_EX_TRANSPARENT As Integer = &H20 'Check if it can
Dim params As CreateParams = MyBase.CreateParams
params.ExStyle = params.ExStyle Or WS_EX_TRANSPARENT
Return params 'return
End Get
End Property
Using VS 2012, I created a simple program without your Screens object, like this:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BackColor = Color.Red
Me.Opacity = 0.5
Me.TopMost = True
SetWindowLong(Me.Handle.ToInt64(), GWL_EXSTYLE, WS_EX_TRANSPARENT)
End Sub
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Const GWL_EXSTYLE = -20
Const WS_EX_TRANSPARENT = &H20
End Class
Note the call to SetWindowLong:
SetWindowLong(Me.Handle.ToInt64(), GWL_EXSTYLE, WS_EX_TRANSPARENT)
Here's a screen shot of the program running:
EDITED TO ADD
Note that you can get this same effect without calling SetWindowLong.
Add this after Class:
Private Const WS_EX_TRANSPARENT As Integer = &H20
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
cp.ExStyle = cp.ExStyle Or WS_EX_TRANSPARENT
Return cp
End Get
End Property
Then run it. It should work. Also it dose not work on non-transparent forms from what i know.
A little variant for a ghost form (You can see through it and click through it!), with the most recent Declare syntax. Thanx to Postman for the useful post.
Imports System.Runtime.InteropServices
Public Class GhostForm
Private InitialStyle As Integer
Private PercentVisible As Decimal
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.BackColor = Color.Green
Me.TopMost = True
End Sub
End Class

Webcam light still on ever after disconnecting the drivers vb.net 2008

I want to capture image from webcam and I found an excellent code for that, the only problem is even after disconnecting webcam drivers webcam light still on. So it seems webcam still in use. How to adjust it?
SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, DeviceID, 0)
DestroyWindow(hHwnd)
Webcam Class
Public Class Form1
Const WM_CAP As Short = &H400S
Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30
Public Const WM_CAP_GET_STATUS As Integer = WM_CAP + 54
Public Const WM_CAP_DLG_VIDEOFORMAT As Integer = WM_CAP + 41
Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52
Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
Const WS_CHILD As Integer = &H40000000
Const WS_VISIBLE As Integer = &H10000000
Const SWP_NOMOVE As Short = &H2S
Const SWP_NOSIZE As Short = 1
Const SWP_NOZORDER As Short = &H4S
Const HWND_BOTTOM As Short = 1
Private DeviceID As Integer = 0 ' Current device ID
Private hHwnd As Integer ' Handle to preview window
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
ByRef lParam As CAPSTATUS) As Boolean
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Boolean, _
ByRef lParam As Integer) As Boolean
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
ByRef lParam As Integer) As Boolean
Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Integer, _
ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, _
ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
Structure POINTAPI
Dim x As Integer
Dim y As Integer
End Structure
Private Sub LoadDeviceList()
Dim strName As String = Space(100)
Dim strVer As String = Space(100)
Dim bReturn As Boolean
Dim x As Short = 0
'
' Load name of all avialable devices into the lstDevices
'
Do
'
' Get Driver name and version
'
bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
'
' If there was a device add device name to the list
'
If bReturn Then lstDevices.Items.Add(strName.Trim)
x += CType(1, Short)
Loop Until bReturn = False
End Sub
Private Sub OpenPreviewWindow()
' Connect to device
'
SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, DeviceID, 0)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadDeviceList()
End Sub
Private Sub ClosePreviewWindow()
'
' Disconnect from device
'
SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, DeviceID, 0)
DestroyWindow(hHwnd)
End Sub
Start And Stop The Camera
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
DeviceID = lstDevices.SelectedIndex
OpenPreviewWindow()
Dim bReturn As Boolean
Dim s As CAPSTATUS
bReturn = SendMessage(hHwnd, WM_CAP_GET_STATUS, Marshal.SizeOf(s), s)
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
ClosePreviewWindow()
SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, DeviceID, 0)
End Sub
End Class