2016-05-23 49 views
0

私のC#アプリケーションでは、フォームのウィンドウハンドルが必要です。ウィンドウの ".exe"名前を知っているので、 "GetProcessesByName"関数を試しました。私はアプリケーションのハンドルを持っていますが、子ウィンドウを取得する方法はわかりません。ハンドルp1を使用して、私フォームとその子ウィンドウハンドルのウィンドウハンドルを取得する方法#

Process[] p = System.Diagnostics.Process.GetProcessesByName("MyProcess"); 

foreach (Process p1 in p) 
{ 
    MessageBox.Show(GetWindowText(p1.MainWindowHandle)); 
} 

ガイド、私はp1の子ウィンドウのハンドルを取得する必要があります。どうやってするの?

+0

[System.Windows.Automation](https://blogs.msdn.microsoft.com/oldnewthing/20160314-00/?p=93152)を使用してください。 –

答えて

0

私は

、ソースコードについては、以下の子windows.Referのハンドルを取得するには、メインウィンドウのハンドルと「 EnumChildWindows」を取得するには、このような「 GetProcessesByName」などの機能のカップルを使用して、これを達成
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Diagnostics; 
using System.Runtime.InteropServices; 
using System.Windows.Interop; 
using Microsoft.Win32; 

[DllImport("user32.Dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
public static extern bool EnumChildWindows(IntPtr parentHandle,  Win32Callback callback, IntPtr lParam); 

Process[] p = System.Diagnostics.Process.GetProcessesByName("StikyNot"); //this is for sticky notes 
foreach (Process p1 in p) 
     {     
      IntPtr MainWindowHandle = p1.MainWindowHandle;    

      List<IntPtr> GetChildWindowsHandle=GetChildWindows(MainWindowHandle); 
     } 

public static List<IntPtr> GetChildWindows(IntPtr parent) 
    { 
     List<IntPtr> result = new List<IntPtr>(); 
     GCHandle listHandle = GCHandle.Alloc(result); 
     try 
     { 
      Win32Callback childProc = new Win32Callback(EnumWindow); 
      EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle)); 
     } 
     finally 
     { 
      if (listHandle.IsAllocated) 
       listHandle.Free(); 
     } 
     return result; 
    } 
関連する問題