none
vb.net 2010 做全局键盘钩子的实例... RRS feed

  • 问题

  • 如题.我想在自己的程序最小化后按下某个键,让他恢复窗口...可是在网上搜不到相关的代码....难道VB.NET已经没落了还是我太弱了.............

    求实例,精简

    另,我的程序是基于.framework 3.5的...程序是vs2010

    2012年9月11日 1:50

答案

  • 你说的这种情况可以不做钩子,注册个热键就好了, 我把我做的sample传到这了:http://sdrv.ms/OfhMGY 

    其中第一个project里的SetHotKeys form就是我说的form,你可以看看里面的代码。

    算了,我这边也贴一份吧:

    Imports System.Runtime.InteropServices
    
    Public Class SetHotKeys
        <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
    
        Public Shared HotKey(0 To 7) As String
        Public Const WM_HOTKEY As Integer = &H312
    
        Private Sub SetHotKeys_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            UnregisterHotKey(Me.Handle, 100)
            UnregisterHotKey(Me.Handle, 200)
            UnregisterHotKey(Me.Handle, 300)
            UnregisterHotKey(Me.Handle, 400)
        End Sub
    
        Private Sub SetHotKeys_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            HotKey(0) = Asc("Z") 'Z
            HotKey(1) = Asc("Y") 'Y
            HotKey(2) = Asc("X")  'X
            RegisterHotKey(Me.Handle, 100, 0, HotKey(0))
            RegisterHotKey(Me.Handle, 200, 0, HotKey(1))
            RegisterHotKey(Me.Handle, 300, 0, HotKey(2)) 'this work
            RegisterHotKey(Me.Handle, 400, 0, Keys.A) 'this also works
        End Sub
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg = WM_HOTKEY Then
                Dim id As IntPtr = m.WParam
                Select Case (id.ToString)
                    Case "100"
                        Console.WriteLine("100")
                    Case "200"
                        Console.WriteLine("200")
                    Case "300"
                        Console.WriteLine("300")
                    Case "400"
                        Me.WindowState = FormWindowState.Normal
                        Console.WriteLine("400")
                End Select
            End If
            MyBase.WndProc(m)
        End Sub
    End Class

    希望有用。


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2012年9月12日 9:01
    版主

全部回复

  • 求达人解答.............真心求

    2012年9月11日 4:30
  • 你说的这种情况可以不做钩子,注册个热键就好了, 我把我做的sample传到这了:http://sdrv.ms/OfhMGY 

    其中第一个project里的SetHotKeys form就是我说的form,你可以看看里面的代码。

    算了,我这边也贴一份吧:

    Imports System.Runtime.InteropServices
    
    Public Class SetHotKeys
        <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
    
        Public Shared HotKey(0 To 7) As String
        Public Const WM_HOTKEY As Integer = &H312
    
        Private Sub SetHotKeys_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            UnregisterHotKey(Me.Handle, 100)
            UnregisterHotKey(Me.Handle, 200)
            UnregisterHotKey(Me.Handle, 300)
            UnregisterHotKey(Me.Handle, 400)
        End Sub
    
        Private Sub SetHotKeys_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            HotKey(0) = Asc("Z") 'Z
            HotKey(1) = Asc("Y") 'Y
            HotKey(2) = Asc("X")  'X
            RegisterHotKey(Me.Handle, 100, 0, HotKey(0))
            RegisterHotKey(Me.Handle, 200, 0, HotKey(1))
            RegisterHotKey(Me.Handle, 300, 0, HotKey(2)) 'this work
            RegisterHotKey(Me.Handle, 400, 0, Keys.A) 'this also works
        End Sub
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg = WM_HOTKEY Then
                Dim id As IntPtr = m.WParam
                Select Case (id.ToString)
                    Case "100"
                        Console.WriteLine("100")
                    Case "200"
                        Console.WriteLine("200")
                    Case "300"
                        Console.WriteLine("300")
                    Case "400"
                        Me.WindowState = FormWindowState.Normal
                        Console.WriteLine("400")
                End Select
            End If
            MyBase.WndProc(m)
        End Sub
    End Class

    希望有用。


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2012年9月12日 9:01
    版主
  • 我在别的地方找到了这个方法,和你说的是一样的...谢谢您.

    2012年9月18日 0:55