2013年3月31日 星期日

vbnet SendMessage 傳送 keyboard











------------
 Imports System.Runtime.InteropServices

Public Class Form1


    _
    Private Shared Function SetActiveWindow(ByVal hWnd As IntPtr) As IntPtr
    End Function

    _
    Public Shared Function SendMessage(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    End Function


    Public Const WM_KEYDOWN = &H100
    Public Const WM_KEYUP = &H101

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
     
        Select Case e.KeyCode
            Case Keys.A
                TextBox1.Text += ChrW(e.KeyValue)
            Case Keys.B
                TextBox1.Text += ChrW(e.KeyValue)
            Case Keys.C
                TextBox1.Text += ChrW(e.KeyValue)
            Case Keys.D
                TextBox1.Text += ChrW(e.KeyValue)
            Case Keys.E
                TextBox1.Text += ChrW(e.KeyValue)
        End Select
        TextBox1.Select(TextBox1.Text.Length, TextBox1.Text.Length)
        TextBox1.Focus()
    End Sub

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

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SetActiveWindow(Me.Handle)      
        SendMessage(Me.Handle, WM_KEYDOWN, Keys.A, 0)
        SendMessage(Me.Handle, WM_KEYUP, Keys.A, 0)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        SetActiveWindow(Me.Handle)
        SendMessage(Me.Handle, WM_KEYDOWN, Keys.B, 0)
        SendMessage(Me.Handle, WM_KEYUP, Keys.B, 0)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        SetActiveWindow(Me.Handle)
        SendMessage(Me.Handle, WM_KEYDOWN, Keys.C, 0)
        SendMessage(Me.Handle, WM_KEYUP, Keys.C, 0)
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        SetActiveWindow(Me.Handle)
        SendMessage(Me.Handle, WM_KEYDOWN, Keys.D, 0)
        SendMessage(Me.Handle, WM_KEYUP, Keys.D, 0)
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        SetActiveWindow(Me.Handle)
        SendMessage(Me.Handle, WM_KEYDOWN, Keys.E, 0)
        SendMessage(Me.Handle, WM_KEYUP, Keys.E, 0)
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        TextBox1.Text = ""
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click

        Form2.Show()

    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        SetActiveWindow(Form2.Handle)
        SendMessage(Form2.Handle, WM_KEYDOWN, Keys.A, 0)
        SendMessage(Form2.Handle, WM_KEYUP, Keys.A, 0)
    End Sub
End Class

沒有留言:

張貼留言