且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有没有办法以编程方式将焦点放在 Windows 中的应用程序上?

更新时间:2022-05-23 22:03:26

类似这样的事情

01  ' Used to get access to Win API calling attributes
02  Imports System.Runtime.InteropServices
03   
04  Public Class Form1
05      ' This is 2 functions from user32.dll (1 for finding the application and 1 to set it to foreground with focus)
06      <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
07      Private Shared Function FindWindow( _
08       ByVal lpClassName As String, _
09       ByVal lpWindowName As String) As IntPtr
10      End Function
11   
12      <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
13      Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Long
14      End Function
15   
16      ' Here we are looking for notepad by class name and caption
17      Dim lpszParentClass As String = "Notepad"
18      Dim lpszParentWindow As String = "Untitled - Notepad"
19   
20      Dim ParenthWnd As New IntPtr(0)
21   
22   
23      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
24          ' Find the window and get a pointer to it (IntPtr in VB.NET)
25          ParenthWnd = FindWindow(lpszParentClass, lpszParentWindow)
26   
27          If ParenthWnd.Equals(IntPtr.Zero) Then
28              Debug.WriteLine("Notepad Not Running!")
29          Else
30              ' Found it, so echo that we found it to debug window
31              ' Then set it to foreground
32              Debug.WriteLine("Notepad Window: " & ParenthWnd.ToString())
33              SetForegroundWindow(ParenthWnd)
34          End If
35      End Sub
36  End Class
enter code here

https://www.google.nl/search?client=opera&q=focus+program+VB

不要使用 Google 可能也会告诉您的 AppActivite.

Do not use AppActivite which Google might also tell you about.