2

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

这可以做到吗?

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

4

1 回答 1

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。

于 2013-01-25T16:11:03.643 回答