【发布时间】:2016-01-09 17:39:48
【问题描述】:
我有一个 vb.net 应用程序,它将被最小化到任务栏或通知区域。当用户按下一个键时(即使用户正在使用任何其他应用程序),我有什么方法可以最大化/专注于该应用程序。
尝试了 Windows 热键,但应用已打开时无法聚焦。请帮助
【问题讨论】:
标签: vb.net background-process hotkeys
我有一个 vb.net 应用程序,它将被最小化到任务栏或通知区域。当用户按下一个键时(即使用户正在使用任何其他应用程序),我有什么方法可以最大化/专注于该应用程序。
尝试了 Windows 热键,但应用已打开时无法聚焦。请帮助
【问题讨论】:
标签: vb.net background-process hotkeys
您需要全局热键。首先,将这些函数添加到您的应用程序中。
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事件中启用计时器。
【讨论】: