在VB 2010中将外部进程放在面板中 [英] Placing an external process inside a panel in VB 2010
问题描述
我无法弄清楚为什么外部流程没有显示在面板内部。我在form1类中有以下声明:
I haven't been able to figure out why the external process doesn't show inside the panel. I have the following declarations in ther form1 class:
Dim myProcess As Process = New Process()
Public WithEvents thb As Button = New System.Windows.Forms.Button
Public Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As System.IntPtr
然后为form1加载:
Then for the form1 load:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
WindowState = FormWindowState.Maximized
MaximizeBox = False
ShowIcon = False
Text = "TLT Opener"
thb.Location = New System.Drawing.Point(20, 20)
thb.Name = "thb"
thb.Size = New System.Drawing.Size(200, 23)
thb.TabIndex = 1
thb.Text = "Open TLT"
thb.UseVisualStyleBackColor = True
Controls.Add(Me.thb)
IsMdiContainer = True
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
myProcess.StartInfo.FileName = "C:\Program Files (x86)\Transformation LogicTree\TLT.exe"
End Sub
工作正常,form1显示Open TLT按钮,点击按钮时... 。
That works fine and the form1 shows up with the "Open TLT" button and when the button is clicked...
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles thb.Click
Dim listProc() As System.Diagnostics.Process
listProc = System.Diagnostics.Process.GetProcessesByName("TLT")
If listProc.Length > 0 Then
SetParent(myProcess.MainWindowHandle, Me.Panel1.Handle)
Exit Sub
End If
myProcess.Start()
myProcess.EnableRaisingEvents = True
AddHandler myProcess.Exited, AddressOf ProcessExited
myProcess.WaitForInputIdle()
SetParent(myProcess.MainWindowHandle, Me.Panel1.Handle)
Me.BringToFront() 'don't know is needed
End Sub
调用外部进程并正确显示但不在panel1内。
我尝试过的事情:
The external process gets called and it shows correctly but NOT inside the panel1.
What I have tried:
Tried all suggested available hints from other threads... but the only way I am able to get the external app inside the panel1 is if I click the "Open TLT" button once the external app is running. Tried to force a click to the button but programmatically it does nothing, only if done with a mouse click.
Does someone knows why is this and how to resolve it?
推荐答案
我已经能够它工作但不是优雅的方式。因此,我将分享我的所作所为。
显然问题是外部应用程序需要一些时间才能完全加载,尽管代码处理完全无需等待。因此,我包含了一个系统线程延迟并尝试了不同的时间,直到一个好的时间被删除。
这是从button2_click启动过程的代码:
I have been able to make it work but not in an elegant way. Therefore I will share what I did.
Apparently the issue is that the external app needs some time to load completely eventhough the code processes all without waiting. Therefore I included a system thread delay and tried different timings until a good one was obatined.
This is the code to start the process from button2_click:
myProcess.Start()
myProcess.EnableRaisingEvents = True
AddHandler myProcess.Exited, AddressOf ProcessExited
myProcess.WaitForInputIdle()
System.Threading.Thread.Sleep(350)
SetParent(myProcess.MainWindowHandle, Panel1.Handle)
如果系统线程休眠时间小于350,则不会将外部应用程序带入panel1。如果它大于350,则闪烁效果对用户来说是显而易见的并且不是很好。
如果外部应用程序已完全加载,应该有一种方法可以捕获而不是使用延迟,因为我不确定时间是否取决于用户的PC。
重点是问题是由于外部应用程序在将panel1设置为父级之前需要时间完全加载。我确信一个更优雅的解决方案是可能的,并且会欣赏有关如何在外部应用程序完全加载时捕获的建议。
If the system threading sleep time is less than 350 it will not bring the external app inside panel1. If it is greater than 350 then a flickering effect is noticeable to the user and not very nice.
There should be a way to capture if the external app has loaded completely instead of using the delay, because I am not sure if the timing depends on the user's PC or not.
The point is that the problem was due to the external app needing time to load completely before setting panel1 as the parent. I am sure a more elegant solution is possible and will appreciate suggestions on how to capture when the external app has loaded completely.
这篇关于在VB 2010中将外部进程放在面板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!