2012-01-09 121 views
1

我需要在VB.NET MDI窗體中打開一些外部應用程序,例如notepad.exe,並且我還需要確保始終只有一個此運行的副本。在VB.NET MDI窗體中打開外部應用程序表單

我使用下面的代碼,但它完全沒有。它給出了錯誤的setparent不宣而FindWindow函數未聲明

Dim myProcess As Process = New Process() 
Dim MyHandle As IntPtr 
myProcess.StartInfo.FileName = "Notepad.exe" 
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal 
myProcess.Start() 
MyHandle = FindWindow(vbNullString, "C:\Windows\Notepad.exe") 
SetParent(MyHandle, Me.Handle) 
myProcess.WaitForExit() 

這是我用來驗證只有一個實例運行

If (System.Diagnostics.Process.GetProcesses.Equals("notepad.exe")) Then 
     MsgBox("Only One Instance!") 
    Else 
     Dim p As New System.Diagnostics.Process 
     p.StartInfo.FileName = "notepad.exe" 
     p.Start() 
    End If 

此代碼是開放的notepad.exe的代碼,但它不檢查以前的實例。所以每次點擊按鈕,它都會打開一個新的記事本

回答

4

必須先聲明SetParent和FindWindow,然後才能使用它們,這就是您接收錯誤的原因。您還在查找記事本的現有實例時遇到問題,因爲GetProcesses會返回一個集合,而不是單個進程。要使用您嘗試的方法,您需要遍歷整個集合以查看它是否包含匹配項或使用.Contains。在我的例子中,我沒有使用FindWindow,但如果將來需要它,我已經包含了它的聲明。示例代碼假定要使用的表單的名稱爲Form1,並且該代碼使用Button1激活。

代碼:

Imports System.Runtime.InteropServices 

    Public Class Form1 
     <DllImport("User32", CharSet:=CharSet.Auto, ExactSpelling:=True)> Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndParent As IntPtr) As IntPtr 
     End Function 

     Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer 

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
      Dim PROCHANDLE As System.IntPtr = -1 
      For Each proc In Process.GetProcesses 
       If LCase(proc.ProcessName) = "notepad" Then 
        PROCHANDLE = proc.Handle 
       End If 
      Next 
      If PROCHANDLE = -1 Then 
       PROCHANDLE = Process.Start("C:\windows\notepad.exe").Handle 
       SetParent(PROCHANDLE, Me.Handle) 
      End If 
     End Sub 
    End Class 
0

這個作品非常好,我剛剛試了一下。 這足以創建一個新的Windows窗體項目和到位的Form1的默認代碼

Public Class Form1 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 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles thb.Click myProcess.Start() myProcess.WaitForInputIdle() SetParent(myProcess.MainWindowHandle, Me.Handle) thb.Text = "Open Notepad Again" End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load WindowState = FormWindowState.Maximized MaximizeBox = False ShowIcon = False Text = "Notepad 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 Notepad" thb.UseVisualStyleBackColor = True Controls.Add(Me.thb) IsMdiContainer = True myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal myProcess.StartInfo.FileName = "notepad.exe" End Sub End Class

粘貼此代碼
相關問題