I am trying to create a screen capture tool. I have so far got a simple program that takes a screen show when a button is clicked on a form.
I would like to know how to get my code to run when you click the mouse anywhere on the screen and not just on the button or form.
Thank you for your help
My code so far
Private Sub btnCapture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapture.Click
Dim ScreenShot As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim ScreenGrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(ScreenGrab)
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), ScreenShot)
ScreenGrab.Save("C:\snap.jpg")
End Sub
I can recommend you to use tools like Usersnap to your project, instead of re-inventing the wheel.
Try the following code:
Imports System.Runtime.InteropServices
Public Class Form1
Dim result As Integer
<DllImport("user32.dll")> _
Public Shared Function GetAsyncKeyState(ByVal vKey As Int32) As Short
End Function
Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
Private Const MOUSEEVENTF_ABSOLUTE = &H8000
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4
Private Const MOUSEEVENTF_MOVE = &H1
Private Const MOUSEEVENTF_MIDDLEDOWN = &H20
Private Const MOUSEEVENTF_MIDDLEUP = &H40
Private Const MOUSEEVENTF_RIGHTDOWN = &H8
Private Const MOUSEEVENTF_RIGHTUP = &H10
Private Structure MSLLHOOKSTRUCT
Public pt As Point
Public mouseData As Int32
Public flags As Int32
Public time As Int32
Public extra As IntPtr
End Structure
Private _mouseHook As IntPtr
Private Const WH_MOUSE_LL As Int32 = 14
Private Delegate Function CallBack(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
<MarshalAs(UnmanagedType.FunctionPtr)> Private _mouseProc As CallBack
Private Declare Function SetWindowsHookExW Lib "user32.dll" (ByVal idHook As Int32, ByVal HookProc As CallBack, ByVal hInstance As IntPtr, ByVal wParam As Int32) As IntPtr
Private Declare Function UnhookWindowsHookEx Lib "user32.dll" (ByVal hook As IntPtr) As Boolean
Private Declare Function CallNextHookEx Lib "user32.dll" (ByVal idHook As Int32, ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
Private Declare Function GetCurrentThreadId Lib "kernel32.dll" () As Integer
Private Declare Function GetModuleHandleW Lib "kernel32.dll" (ByVal fakezero As IntPtr) As IntPtr
Public Function InstallHook() As Boolean
If _mouseHook = IntPtr.Zero Then
_mouseProc = New CallBack(AddressOf MouseHookProc)
_mouseHook = SetWindowsHookExW(WH_MOUSE_LL, _mouseProc, GetModuleHandleW(IntPtr.Zero), 0)
End If
Return _mouseHook <> IntPtr.Zero
End Function
Public Sub RemoveHook()
If _mouseHook = IntPtr.Zero Then Return
UnhookWindowsHookEx(_mouseHook)
_mouseHook = IntPtr.Zero
End Sub
Private Function MouseHookProc(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
Try
For i = 1 To 255
result = 0
result = GetAsyncKeyState(i)
Me.Text = result
If result = -32767 Then
Dim ScreenShot As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim ScreenGrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(ScreenGrab)
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), ScreenShot)
ScreenGrab.Save("C:\snap.jpg")
End If
Next i
Catch ex As Exception
End Try
End Function
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
RemoveHook()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
InstallHook()
End Sub
End Class
Take Screen Shot on Asp Dotnet
Client-side
asp:Button runat="server" ID="Button1" OnClick="Button1_Click" />
Server-side
using System.Drawing;
using System.Drawing.Imaging;
using System.Timers;
protected void Button1_Click(object sender, EventArgs e)
{
Capture("E:/ScreenShot.bmp");//path to Save Captured files
}
public static void Capture(string CapturedFilePath)
{
Bitmap bitmap = new Bitmap
(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap as System.Drawing.Image);
graphics.CopyFromScreen(25, 25, 25, 25, bitmap.Size);
bitmap.Save(CapturedFilePath, ImageFormat.Bmp);
}
Related
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
I have this code but I don't know why it doesn't work, can anyone help me? I need to send ctrl + f to another application in the background
Imports System.Runtime.InteropServices
Public Class Form2
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Const WM_CHAR = &H102
Private Const WM_SYSKEYDOWN = &H104
Private Const WM_SYSKEYUP = &H105
Private Const VK_SHIFT = &H10
Private Const VK_CONTROL = &H11
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2
Private Const KEYEVENTF_KEYDOWN = &H0
Private Const VK_F = &H46
Private Const WM_SETTEXT = &HC
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
End Function
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
Public Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Integer, ByVal wMapType As Integer) As Byte
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim hPadre As IntPtr = FindWindowEx(IntPtr.Zero, hPadre, "Notepad", "Sin título: Bloc de notas")
Dim hCarpetas As IntPtr = FindWindowEx(hPadre, hCarpetas, "Edit", vbNullString)
keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), KEYEVENTF_KEYDOWN, 0)
PostMessage(hPadre, WM_KEYDOWN, VK_F, 0)
keybd_event(VK_CONTROL, MapVirtualKey(VK_CONTROL, 0), KEYEVENTF_KEYUP, 0)
End Sub
I also have this example, but it doesn't work :(
Imports System.Runtime.InteropServices
Public Class Form1
Private Const VK_CONTROL As Integer = 17
Private Const KEYEVENTF_KEYUP As Integer = &H2
Private Const INPUT_MOUSE As Integer = 0
Private Const INPUT_KEYBOARD As Integer = 1
Private Const INPUT_HARDWARE As Integer = 2
Private Const WM_KEYDOWN As Integer = &H100
Private Structure MOUSEINPUT
Public dx As Integer
Public dy As Integer
Public mouseData As Integer
Public dwFlags As Integer
Public time As Integer
Public dwExtraInfo As IntPtr
End Structure
Private Structure KEYBDINPUT
Public wVk As Integer
Public wScan As Integer
Public dwFlags As Integer
Public time As Integer
Public dwExtraInfo As IntPtr
End Structure
Private Structure HARDWAREINPUT
Public uMsg As Integer
Public wParamL As Integer
Public wParamH As Integer
End Structure
<StructLayout(LayoutKind.Explicit)>
Private Structure INPUT
<FieldOffset(0)>
Public type As Integer
<FieldOffset(4)>
Public mi As MOUSEINPUT
<FieldOffset(4)>
Public ki As KEYBDINPUT
<FieldOffset(4)>
Public hi As HARDWAREINPUT
End Structure
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
End Function ' Dany
Private Declare Function SendInput Lib "user32" (ByVal nInputs As Integer, ByVal pInputs() As INPUT, ByVal cbSize As Integer) As Integer
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As IntPtr, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Integer, ByVal bScan As Byte, ByVal dwFlags As IntPtr, ByVal dwExtraInfo As IntPtr)
Private Sub wait(ByVal interval As Integer)
Dim sw As New Stopwatch
sw.Start()
Do While sw.ElapsedMilliseconds < interval
' Allows UI to remain responsive
Application.DoEvents()
Loop
sw.Stop()
End Sub
Private Sub SendKey(ByVal bKey As Integer)
Dim GInput(0) As INPUT
' press the key
GInput(0).type = INPUT_KEYBOARD
GInput(0).ki.wVk = bKey
GInput(0).ki.dwFlags = 0
SendInput(2, GInput, Marshal.SizeOf(GetType(INPUT)))
End Sub
Sub Press_Control()
wait(2000)
Dim b As Integer
b = VK_CONTROL
SendKey(b)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim hPadre As IntPtr = FindWindowEx(IntPtr.Zero, hPadre, "Notepad", "Sin título: Bloc de notas")
Dim hCarpetas As IntPtr = FindWindowEx(hPadre, hCarpetas, "Edit", vbNullString)
Press_Control()
SendMessage(hPadre, WM_KEYDOWN, 70, 0)
wait(1000)
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)
End Sub
I have a code that append menu in the system menu(right click form title bar). How will i add click event in the two menus that i appended? this is the code i use:
Public Class AppendFormSysMenu
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Private Shared Function AppendMenu(ByVal hMenu As IntPtr, ByVal uFlags As Int32, ByVal uIDNewItem As IntPtr, ByVal lpNewItem As String) As Boolean
End Function
Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As IntPtr, ByVal bRevert As Boolean) As IntPtr
Private SYSMENU_SAVE_FORM_ID As Integer = &H1
Private SYSMENU_RESTORE_FORM_ID As Integer = &H2
<Flags()>
Public Enum MenuFlags As Integer
MF_BYPOSITION = 1024
MF_REMOVE = 4096
MF_SEPARATOR = 2048
MF_STRING = 0
End Enum
Public Sub insertSeparator(frm As Form)
Dim hMenu = GetSystemMenu(frm.Handle, False)
AppendMenu(hMenu, MenuFlags.MF_SEPARATOR, 0, Nothing)
End Sub
Public Sub insertSaveMenu(frm As Form, ByVal strMenuItem As String)
Dim hMenu = GetSystemMenu(frm.Handle, False)
AppendMenu(hMenu, MenuFlags.MF_STRING, SYSMENU_SAVE_FORM_ID, strMenuItem)
End Sub
Public Sub insertRestoreMenu(frm As Form, ByVal strMenuItem As String)
Dim hMenu = GetSystemMenu(frm.Handle, False)
AppendMenu(hMenu, MenuFlags.MF_STRING, SYSMENU_RESTORE_FORM_ID, strMenuItem)
End Sub
End Class
This is the code i use in the form:
Dim AppendFormMenu As New AppendFormSysMenu
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AppendFormMenu.insertSeparator(Me)
AppendFormMenu.insertSaveMenu(Me, "Save Form Position")
AppendFormMenu.insertRestoreMenu(Me, "Restore Form Position")
End Sub
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.
my test:
Public Class Form1
Public Const WM_SYSCOMMAND As Int32 = &H112
Public Const MF_BYPOSITION As Int32 = &H400
Public Const MYMENU1 As Int32 = 1000
Public Const MYMENU2 As Int32 = 1001
Dim hSysMenu As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Public Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" _
(ByVal hMenu As IntPtr, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As String) As Boolean
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
MyBase.WndProc(m)
If (m.Msg = WM_SYSCOMMAND) Then
Select Case m.WParam.ToInt32
Case MYMENU1
MsgBox("1")
Case MYMENU2
MsgBox("2")
End Select
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
hSysMenu = GetSystemMenu(Me.Handle, False)
InsertMenu(hSysMenu, 5, MF_BYPOSITION, MYMENU1, "My Menu 1")
InsertMenu(hSysMenu, 6, MF_BYPOSITION, MYMENU2, "My Menu 2")
End Sub
End Class
and i get an error:
PInvokeStackImbalance was detected
Message: A call to PInvoke function 'WindowsApplication1!WindowsApplication1.Form1::GetSystemMenu' 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.
Replace Long with Integer in your declarations.