1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | Public Class API_SendMessage Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As IntPtr, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr Private Declare Ansi Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long 'Public Const WM_CHAR = &H46 Public Const WM_KEYDOWN = &H100 Public Const WM_KEYUP = &H101 Public Const WM_UP = &H101 Public Const WM_CHAR = &H102 Public Const VK_A = &H41 Const VK_NUMLOCK = &H90 ' Num Lock Const KEYEVENTF_KEYDOWN = &H0 ' KeyDown Const KEYEVENTF_KEYUP = &H2 ' KeyUp Const VK_F1 = &H70 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click TextBox1.Text = "a" Dim hwnd As Integer = FindWindow("notepad", vbNullString) Dim x As Integer = FindWindowEx(hwnd, 0, "Edit", vbNullString) ' PostMessage(hwnd, WM_KEYDOWN, VK_A, MakeKeyLparam(VK_A, WM_KEYDOWN)) '按下A鍵 ' PostMessage(hwnd, WM_CHAR, Asc("A"), MakeKeyLparam(VK_A, WM_KEYDOWN)) '輸入字元A ' PostMessage(hwnd, WM_UP, VK_A, MakeKeyLparam(VK_A, WM_UP)) '釋放A鍵 PostMessage(x, WM_KEYDOWN, VK_A, 0) '按下A鍵 End Sub Function MakeKeyLparam(ByVal VirtualKey As Long, ByVal flag As Long) As Long Dim s As String Dim Firstbyte As String 'lparam參數的24-31位 If flag = WM_KEYDOWN Then '如果是按下鍵 Firstbyte = "00" Else Firstbyte = "C0" '如果是釋放鍵 End If Dim Scancode As Long '獲得鍵的掃描碼 Scancode = MapVirtualKey(VirtualKey, 0) Dim Secondbyte As String 'lparam參數的16-23位元,即虛擬鍵掃描碼 Secondbyte = Strings.Right("00" & Hex(Scancode), 2) s = Firstbyte & Secondbyte & "0001" '0001為lparam參數的0-15位,即發送次數和其它擴展資訊 MakeKeyLparam = Val("&H" & s) End Function End Class |
2020年5月19日 星期二
VB.net PostMessage 使用方式 包含宣告 與子視窗 [PostMessage control childForm]
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言