VB2008中怎么为窗体设置热键? RRS feed

  • 问题

  • VB2008中怎么为窗体设置热键?比如我按下Home键时,form1窗体显示出来,当我再按下Home键时,form1窗体关闭或者隐藏掉。下次再次按下Home键的时候窗体又出现!谢谢!
    2011年9月10日 9:17


  • Hi,


    Imports System.Runtime.InteropServices
    Public Class Form1
       '--- Windows API declarations
       Private Const WM_HOTKEY As Integer = &H312
       Private Const MOD_ALT As Integer = &H1
       Private Const MOD_CONTROL As Integer = &H2
       Private Const MOD_SHIFT As Integer = &H4
       Private Declare Function RegisterHotKey Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifier As Integer, ByVal vk As Integer) As Integer
       Private Declare Function UnregisterHotKey Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean
       Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hWnd As IntPtr) As Boolean
      Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
         RegisterHotKey(Me.Handle, 0, MOD_CONTROL + MOD_SHIFT, Keys.U)
       End Sub
       Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
         UnregisterHotKey(Me.Handle, 0)
       End Sub
       Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
         If (m.Msg = WM_HOTKEY AndAlso m.WParam = CType(0, IntPtr)) Then
           Me.Visible = True
           If Me.WindowState = FormWindowState.Minimized Then Me.WindowState = FormWindowState.Normal
         End If
       End Sub
     End Class
    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年9月12日 5:55