以下代码有效,并在用户按下键盘上的 a 键时打印到 Label1:
Public Class Form1
Sub New()
InitializeComponent()
End Sub
Public Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal HWND As IntPtr, ByVal WMSG As Integer, ByVal WPARAM As Integer, ByVal LPARAM As IntPtr) As IntPtr
Private WM_KEYDOWN As Integer = &H100
Protected Overrides Sub WndProc(ByRef WindowsMessage As Message)
Select Case WindowsMessage.Msg
Case WM_KEYDOWN
Dim VirtualKeyCode As Integer = CType(WindowsMessage.WParam, Integer)
Select Case (VirtualKeyCode)
Case Keys.A
Label1.Text = "The a key was pressed"
End Select
End Select
MyBase.WndProc(WindowsMessage)
End Sub
结束类
将按钮添加(拖动)到表单后,代码停止工作。一旦按钮位于表单上,就不再发送 WM_KEYDOWN 消息。在 Case WM_KEYDOWN 处设置的断点永远不会被命中。
如果我删除按钮,代码将再次开始工作。
有什么建议么?
谢谢。