有没有一种方法可以通过编程方式在windows中为应用程序提供焦点?

有没有一种方法可以通过编程方式在windows中为应用程序提供焦点?,windows,vb.net,Windows,Vb.net,我想使用Visual Basic构建一个将在Windows环境中运行的应用程序。应用程序应该非常简单:两个按钮,每个按钮都会使不同的应用程序获得焦点,将其向前拉到Visual Basic应用程序前面 这能做到吗 如果不能用Visual Basic完成,是否有另一种编程语言可以在创建简单UI相当容易的情况下完成 01 ' Used to get access to Win API calling attributes 02 Imports System.Runtime.InteropServi

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

这能做到吗

如果不能用Visual Basic完成,是否有另一种编程语言可以在创建简单UI相当容易的情况下完成

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
01'用于访问Win API调用属性
02导入System.Runtime.InteropServices
03
04公开课表格1
05'这是user32.dll中的2个函数(1个用于查找应用程序,1个用于将其设置为具有焦点的前台)
06       _
07私有共享函数FindWindow(_
08 ByVal lpClassName作为字符串_
09 ByVal lpWindowName(作为字符串)作为IntPtr
10端功能
11
12       _
13私有共享函数SetForegroundWindow(ByVal hWnd作为IntPtr)的长度
14端功能
15
16'在这里,我们按类名和标题查找记事本
17 Dim lpszParentClass为String=“记事本”
18 Dim lpszParentWindow为String=“无标题-记事本”
19
20 Dim ParenthWnd作为新IntPtr(0)
21
22
23私有子按钮1\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮1。单击
24'找到窗口并获取指向它的指针(VB.NET中的IntPtr)
25 ParenthWnd=FindWindow(lpszParentClass,lpszParentWindow)
26
27如果ParenthWnd.等于(IntPtr.零),则
28 Debug.WriteLine(“记事本未运行!”)
29其他
30'找到了,所以我们在调试窗口中找到了它
31'然后将其设置为前景
32 Debug.WriteLine(“记事本窗口:&ParenthWnd.ToString())
33 SetForegroundWindow(ParenthWnd)
34如果结束
35端接头
36末班
在这里输入代码


不要使用AppActivite,谷歌可能也会告诉你这一点。

使用WinSight、Spy++或Windowse或类似工具查找程序句柄。或者使用正确的参数/设置自行启动程序。非常感谢。我的问题是我对VB太陌生了(比如,这是我第一次使用它),以至于我没有想到搜索VB而不是Visual Basic.:)这肯定是我需要的!