按键切换到vb.net应用程序

时间:2015-10-12 09:36:03

标签: vb.net background-process hotkeys

我有一个vb.net应用程序,它将被最小化到任务栏或通知区域。当用户按下某个键时,有没有办法让我最大化/专注于该应用程序(即使用户正在使用任何其他应用程序)。

尝试了Windows热键但在应用程序已经打开时不会将其集中。请帮助

1 个答案:

答案 0 :(得分:1)

您需要全局热键。首先,将这些功能添加到您的应用程序中。

Private Declare Function GetKeyPress Lib "user32" Alias "GetAsyncKeyState" (ByVal key As Integer) As Integer
<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True)> _
Private Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function ShowWindow(hWnd As IntPtr, nCmdShow As Integer) As Boolean
End Function
<DllImport("user32.dll")> _
Private Shared Function SetForegroundWindow(hWnd As IntPtr) As Integer
End Function

接下来,添加Timer并在其Timer.Tick事件中,使用以下函数:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    If GetKeyPress(Keys.LControlKey) And GetKeyPress(Keys.A) Then
        Dim Handle As IntPtr = Process.GetProcessById(2916).MainWindowHandle
        ShowWindow(Handle, 9)
        SetForegroundWindow(Handle)
    End If
End Sub

Timer间隔设置为150以避免重复按键,并确保在Form Load事件中启用计时器。