vb.net 在表单内启动应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13651815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:29:02  来源:igfitidea点击:

vb.net Launch application inside a form

vb.netwinformsprocesswindows-8visual-studio-2012

提问by Devil's Advocate

I want to run an app inside a panel or something within my applicaiton. It's an emulator front end. You browse through games, then when you select one it launches the emulator. I found the following code and adapted it to my project

我想在面板或我的应用程序中运行一个应用程序。这是一个模拟器前端。您浏览游戏,然后当您选择一个时,它会启动模拟器。我找到了以下代码并将其改编为我的项目

Public Class Form1
    Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
    Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Private Const WM_SYSCOMMAND As Integer = 274
    Private Const SC_MAXIMIZE As Integer = 61488
    Dim proc As Process

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        proc = Process.Start("C:\WINDOWS\notepad.exe")
        proc.WaitForInputIdle()

        SetParent(proc.MainWindowHandle, Panel1.Handle)
        SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    End Sub
End Class

If I try it with notepad, or even zsnesw.exe it works okay, but if I try to pass some parameters to zsnesw it kind of freaks out and I have to reboot my computer (I can't switch applications or even open the task manager).

如果我用记事本甚至 zsnesw.exe 尝试它,它可以正常工作,但是如果我尝试将一些参数传递给 zsnesw 它有点吓人,我必须重新启动我的计算机(我无法切换应用程序,甚至无法打开任务经理)。

Also, even when it does work, the start menu pops up like I have switched to another app. This is kind of what I was trying to avoid in the first place as my app is full screen.

此外,即使它确实有效,开始菜单也会弹出,就像我切换到另一个应用程序一样。这是我首先试图避免的,因为我的应用程序是全屏的。

采纳答案by Devil's Advocate

I got it working!

我让它工作了!

        Dim proc As Process
        proc = Process.Start(emuPath + "zsnesw", "-m """ + selGame.romPath + """")
        proc.WaitForInputIdle()
        SetParent(proc.MainWindowHandle, Me.Panel1.Handle)
        SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
        Me.BringToFront()

Problem 1:I was passing the arguments incorrectly. I was trying to use Process.StartInfo.Arguments. Didn't work for some reason. Using a comma in Process.Start works fine.

问题 1:我错误地传递了参数。我试图使用 Process.StartInfo.Arguments。由于某种原因没有工作。在 Process.Start 中使用逗号工作正常。

Problem 2:I added Me.BringToFront() to hide the start menu again.

问题 2:我添加了 Me.BringToFront() 来再次隐藏开始菜单。

回答by user3783084

Use Thread.Sleep ;)

使用 Thread.Sleep ;)

'Run Calc application inside Panel2 control

'在 Panel2 控件中运行 Calc 应用程序

Dim proc As Process
proc = Process.Start("Calc.exe")
proc.WaitForInputIdle()
Thread.Sleep(1000)
SetParent(proc.MainWindowHandle, Me.Panel2.Handle)
Thread.Sleep(1000)
SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)