显示另一个程序的窗口

时间:2015-04-23 18:37:12

标签: vb.net winforms user-interface

我在Visual Basic 2010中创建了一个GUI,用于启动另一个程序,但其他程序在启动时隐藏在GUI窗口后面。我已经检索了其他程序的进程ID,以便以后杀死它,但是我不知道如何将ID转换成我可以用来推进窗口的东西。

另一个解决方案是将我的GUI发送到后面,但这也不起作用。我认为这是因为其他程序。在主窗口启动之前出现需要交互的启动画面。我的程序在启动屏幕关闭之前不会被发送回来,从而无法实现目的。

2 个答案:

答案 0 :(得分:2)

你有没有看过AppActivate。它应该专注于你想要的应用程序,只要它运行。

答案 1 :(得分:1)

有几种方法可以做到这一点。您可以使用Timmy建议的AppActivate,也可以使用SetForegroundWindow函数使用pinvoke:

Imports System.Runtime.InteropServices

Public Class Form1
    Dim oProcess As New Process()

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Add buttons to the form

        Dim cmd As New Button
        cmd.Name = "cmdAppActivate"
        cmd.Text = "App Activate"
        cmd.Location = New Point(0, 0)
        cmd.Size = New Size(90, 25)
        AddHandler cmd.Click, AddressOf cmdAppActivate_Click
        Me.Controls.Add(cmd)

        cmd = New Button
        cmd.Name = "cmdSetForegroundWindow"
        cmd.Text = "Set Foreground Window"
        cmd.Location = New Point(0, 30)
        cmd.Size = New Size(130, 25)
        AddHandler cmd.Click, AddressOf cmdSetForegroundWindow_Click
        Me.Controls.Add(cmd)

        ' Open notepad

        oProcess.StartInfo = New ProcessStartInfo("notepad.exe")
        oProcess.Start()
    End Sub

    Private Sub cmdAppActivate_Click(sender As Object, e As EventArgs)
        AppActivate(oProcess.Id)    ' use appactivate to bring the notepad window to the front
    End Sub

    Private Sub cmdSetForegroundWindow_Click(sender As Object, e As EventArgs)
        SetForegroundWindow(oProcess.MainWindowHandle)  ' Use pinvoke (SetForegroundWindow) to bring the notepad window to the front
    End Sub
End Class

Module Module1
    <DllImport("user32.dll")> _
    Public Function SetForegroundWindow(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function
End Module