2013年4月1日 星期一

vbnet postmessage 組合鍵


Public Class Form1
    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 Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
         
            Case e.Alt And Keys.A
                TextBox1.Text = e.KeyValue
                MessageBox.Show("1")
            Case Keys.A
                TextBox1.Text = e.KeyValue
                MessageBox.Show("2")
        End Select
    End Sub

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

    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


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Dim hwnd As Integer = FindWindow("notepad", vbNullString)
        'Dim x As Integer = FindWindowEx(hwnd, 0, "Edit", vbNullString)
        'PostMessage(Me.Handle, WM_KEYDOWN, VK_A, 0)  '按下A键


        ' 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(Me.Handle, WM_KEYDOWN, VK_A, 0)  '按下A键

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        PostMessage(Me.Handle, WM_KEYDOWN, Keys.Alt + Keys.A, 0)  '按下Alt+A键
     
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Form2.Show()

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        PostMessage(Form2.Handle, WM_KEYDOWN, Keys.Alt + Keys.A, 0)  '按下Alt+A键
    End Sub
End Class

沒有留言:

張貼留言