我正在尝试打开现有的可执行应用程序(PTC CREO VIEWER)并将其嵌入到我的应用程序中。我正在使用此代码:
#Region "EMBADED PROGRAM"
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
Declare Auto Function GetWindowLong Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
Declare Auto Function SetWindowLong Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
Declare Auto Function SetWindowPos Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
Private Const WM_SYSCOMMAND As Integer = 274
Private Const SC_MAXIMIZE As Integer = 61488
Private Const SC_MINIMIZE As Integer = 61472 ' &HF020
Private appProc As Process
Private winProc As Process
Public Sub LoadApp(filename As String, arguments As String)
Try
appProc = New Process()
appProc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
appProc.StartInfo.FileName = filename
If arguments <> "" Then
appProc.StartInfo.Arguments = arguments
End If
Dim isStartOK As Boolean = appProc.Start()
Dim processName As String = appProc.ProcessName
appProc.Refresh()
Dim isWaitOk As Boolean = appProc.WaitForInputIdle(5000)
appProc.Refresh()
If Not isWaitOk Then
MsgBox("WaitForInputIdle Not ok")
Return
End If
Thread.Sleep(500)
Dim processes As Process() = System.Diagnostics.Process.GetProcessesByName(processName) ' IO.Path.GetFileNameWithoutExtension(program))
Console.WriteLine(processes.Length)
Thread.Sleep(1000)
For Each p As Process In processes
If p.MainWindowHandle <> 0 Then
winProc = p
Exit For
End If
Next
If (winProc.MainWindowHandle = 0) Then
MsgBox("winProc.MainWindowHandle = 0")
Return
End If
Thread.Sleep(500)
SetParent(winProc.MainWindowHandle, Panel1.Handle)
Thread.Sleep(100)
SendMessage(winProc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
Me.BringToFront()
'MakeExternalWindowBorderless(winProc.MainWindowHandle)
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
Public Sub CloseApp()
Try
If Not IsNothing(winProc) Then
winProc.CloseMainWindow()
winProc.Close()
End If
If Not IsNothing(appProc) Then
appProc.CloseMainWindow()
appProc.Close()
End If
Catch ex As Exception
End Try
End Sub
#End Region
我可以在我的应用程序面板中运行notepad.exe或mspaint.exe等应用程序,但这对Creo Viewer或Chrome也不起作用。
Dim isWaitOk As Boolean = appProc.WaitForInputIdle(5000)
总是返回false(使用creo viewer),我试图设置更多超时但它仍然无效。我还在appProc.Refresh()
循环中尝试while
但没有尝试。我没有得到嵌入式应用的MainWindowHandle
。
有人知道在Windows窗体应用程序(VB.NET)中打开PTC Creo Viewer Express的其他方法吗?