【问题标题】:Switch focus to a vb.net application on key press在按键时将焦点切换到 vb.net 应用程序
【发布时间】:2016-01-09 17:39:48
【问题描述】:

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

尝试了 Windows 热键,但应用已打开时无法聚焦。请帮助

【问题讨论】:

    标签: vb.net background-process hotkeys


    【解决方案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事件中启用计时器。

    【讨论】:

    • 嗨,法尔汉,谢谢。该代码第一次工作。当我按下组合键时从任务栏激活应用程序。但是一旦再次最小化,它就不起作用了。
    猜你喜欢
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多