2012-01-09 52 views
1

VB.NET MDIフォーム内のnotepad.exeなどのいくつかの外部アプリケーションを開く必要があります。また、常にこの実行中のものが1つしかないことを確認する必要があります。VB.NET MDIフォーム内の外部アプリケーションを開く

私は以下のコードを使用しましたが、まったく何もしません。それは私が唯一のインスタンスはこのコードはメモ帳を開いている

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 

を実行していることを確認するために使用されるコードがあるが、エラー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() 

この

を宣言されていない与え、それ以前のインスタンスをチェックしていません。したがって、ボタンをクリックするたびに新しいメモ帳が開かれます

答えて

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

を、このコードを貼り付けています
関連する問題