2

您好,我正在尝试在 VB.NET 中使用 RegisterHeyKeys,但是我让它与 2 个热键一起工作,我尝试在第三个中添加它,它给出了太多的参数。这可能是一件非常简单的事情,我也是一个小块,所以放轻松。哈哈。任何帮助将不胜感激。

这是到目前为止的代码:

Public Const MOD_CONTROL As Integer = &H11
Public Const MOD_SHIFT As Integer = &H10
Public Const WM_HOTKEY As Integer = &H312

<DllImport("User32.dll")> _
Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, _
ByVal id As Integer, ByVal fsModifiers As Integer, _
ByVal vk As Integer) As Integer
End Function

<DllImport("User32.dll")> _
Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, _
                    ByVal id As Integer) As Integer
End Function

Private Sub Form1_Load(ByVal sender As System.Object, _
                    ByVal e As System.EventArgs) Handles MyBase.Load
    RegisterHotKey(Me.Handle, 100, MOD_CONTROL, MOD_SHIFT, Keys.D2)
    RegisterHotKey(Me.Handle, 200, MOD_CONTROL, MOD_SHIFT, Keys.D3)
    RegisterHotKey(Me.Handle, 300, MOD_CONTROL, MOD_SHIFT, Keys.D4)
End Sub
4

1 回答 1

6

我看到的问题是你添加了两个修饰符MOD_CONTROLMOD_SHIFT并用逗号分隔它们,说你有五个参数到函数,即使它只需要四个。尝试像这样将您的修饰符组合在一起。您还应该使用文档验证您的修饰键,它们似乎不正确。

Private Sub Form1_Load(ByVal sender As System.Object, _
                    ByVal e As System.EventArgs) Handles MyBase.Load
    RegisterHotKey(Me.Handle, 100, MOD_CONTROL Or MOD_SHIFT, Keys.D2)
    RegisterHotKey(Me.Handle, 200, MOD_CONTROL Or MOD_SHIFT, Keys.D3)
    RegisterHotKey(Me.Handle, 300, MOD_CONTROL Or MOD_SHIFT, Keys.D4)
End Sub

从它声明的文档中(强调我的):

fsModifiers [in]
类型:UINT

为了生成 WM_HOTKEY 消息,必须与 uVirtKey 参数指定的键组合按下的键。fsModifiers 参数可以是以下值的组合

   Value                        Meaning

MOD_ALT 0x0001         Either ALT key must be held down.

MOD_CONTROL 0x0002     Either CTRL key must be held down.

MOD_NOREPEAT 0x4000   Changes the hotkey behavior so that the keyboard auto-repeat does not yield multiple hotkey notifications. 
                      Windows Vista and Windows XP/2000:  This flag is not supported.
MOD_SHIFT 0x0004      Either SHIFT key must be held down.

MOD_WIN 0x0008        Either WINDOWS key was held down. These keys are labeled with the Windows logo. Keyboard shortcuts
                      that involve the WINDOWS key are reserved for use by the operating system

这是您的程序的一个工作示例。

Public Const MOD_CONTROL As Integer = &H2
Public Const MOD_SHIFT As Integer = &H4
Public Const WM_HOTKEY As Integer = &H312

<DllImport("User32.dll")> _
Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, _
                                      ByVal id As Integer, ByVal fsModifiers As Integer, _
                                      ByVal vk As Integer) As Integer
End Function

<DllImport("User32.dll")> _
Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, _
                ByVal id As Integer) As Integer
End Function

Private Sub Form1_Load(ByVal sender As System.Object, _
                    ByVal e As System.EventArgs) Handles MyBase.Load
    RegisterHotKey(Me.Handle, 100, MOD_CONTROL Or MOD_SHIFT, Keys.D2)
    RegisterHotKey(Me.Handle, 200, MOD_CONTROL Or MOD_SHIFT, Keys.D3)
    RegisterHotKey(Me.Handle, 300, MOD_CONTROL Or MOD_SHIFT, Keys.D4)
End Sub

Protected Overrides Sub DefWndProc(ByRef m As System.Windows.Forms.Message)
    MyBase.DefWndProc(m)
    If m.Msg = WM_HOTKEY Then
        Select Case CType(m.WParam, Integer)
            Case 100
                NotifyIcon1.Text = "Hello"
                NotifyIcon1.ShowBalloonTip(2000, "", NotifyIcon1.Text, ToolTipIcon.Info)
            Case 200
                NotifyIcon1.Text = "World"
                NotifyIcon1.ShowBalloonTip(2000, "", NotifyIcon1.Text, ToolTipIcon.Info)
            Case 300
                NotifyIcon1.Visible = False
                If Not Visible Then Visible = True
        End Select
    End If
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Me.Hide()
    NotifyIcon1.Icon = Me.Icon
    NotifyIcon1.Visible = True
End Sub
于 2013-01-05T04:13:04.923 回答