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

时间:2013-01-25 16:06:08

标签: windows vb.net

我想使用将在Windows环境中运行的Visual Basic构建应用程序。应用程序应该非常简单:两个按钮,每个按钮都会导致不同的应用程序获得焦点,在Visual Basic应用程序前面将其向前拉。

可以这样做吗?

如果使用Visual Basic无法完成,是否有另一种编程语言可以在简单的UI易于创建的情况下完成?

1 个答案:

答案 0 :(得分:2)

像这样的东西

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。