C#winform-获取外部程序子进程';s的位置和大小
经过一个月的在线搜索,我没有找到解决问题的方法。现在我觉得是时候发布这篇文章寻求帮助了 我的项目:它在任务栏中查找外部运行的软件,获取窗口的位置、名称、进程ID和标题,并将其保存在datagrid中。我在windows 7的winform中使用c# 问题:我的代码可以找到正在运行的软件进程(processID),并可以获得进程的大小和其他细节。但问题是我的代码无法找到子/子窗口的大小或任何其他细节 样本信息:我附上了样本图像和代码,正在运行,没有任何错误。但不捕获运行子窗口的其他程序。 在这些图像中,您可以看到Webex软件可以找到子窗口并在其上附加一个弹出框(仅举一个可能找到子窗口的示例)C#winform-获取外部程序子进程';s的位置和大小,c#,winforms,c#-4.0,c#-3.0,C#,Winforms,C# 4.0,C# 3.0,经过一个月的在线搜索,我没有找到解决问题的方法。现在我觉得是时候发布这篇文章寻求帮助了 我的项目:它在任务栏中查找外部运行的软件,获取窗口的位置、名称、进程ID和标题,并将其保存在datagrid中。我在windows 7的winform中使用c# 问题:我的代码可以找到正在运行的软件进程(processID),并可以获得进程的大小和其他细节。但问题是我的代码无法找到子/子窗口的大小或任何其他细节 样本信息:我附上了样本图像和代码,正在运行,没有任何错误。但不捕获运行子窗口的其他程序。 在这些图
在我的第二张图片中,您可以看到我的代码,但只能找到skype(lync)的主窗口 任何建议或指导都会帮上大忙,谢谢 导入dll 运行代码,在按下按钮时查找窗口和大小,并对w和h进行一些计算 更新:根据Abye的建议。我试着用谷歌搜索Windows。我从这个链接中找到了一个代码,我试图在代码中实现它,但我不知道如何使用它并获取子窗口的位置和大小
private delegate bool EnumWindowProc(IntPtr hwnd, IntPtr lParam);
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr lParam);
private IntPtr _MainHandle;
public Form1(IntPtr handle)
{
this._MainHandle = handle;
}
public List<IntPtr> GetAllChildHandles()
{
List<IntPtr> childHandles = new List<IntPtr>();
GCHandle gcChildhandlesList = GCHandle.Alloc(childHandles);
IntPtr pointerChildHandlesList = GCHandle.ToIntPtr(gcChildhandlesList);
try
{
EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
EnumChildWindows(this._MainHandle, childProc, pointerChildHandlesList);
}
finally
{
gcChildhandlesList.Free();
}
return childHandles;
}
private bool EnumWindow(IntPtr hWnd, IntPtr lParam)
{
GCHandle gcChildhandlesList = GCHandle.FromIntPtr(lParam);
if (gcChildhandlesList == null || gcChildhandlesList.Target == null)
{
return false;
}
List<IntPtr> childHandles = gcChildhandlesList.Target as List<IntPtr>;
childHandles.Add(hWnd);
return true;
}
private void button5_Click(object sender, EventArgs e)
{
Process[] anotherApps = Process.GetProcessesByName(textBox1.Text);
if (anotherApps.Length == 0) return;
if (anotherApps[0] != null)
{
var allChildWindows = new Form1(anotherApps[0].MainWindowHandle).GetAllChildHandles();
}
}
私有委托bool EnumWindowProc(IntPtr hwnd、IntPtr lParam);
[DllImport(“user32”)]
[返回:Marshallas(UnmanagedType.Bool)]
私有静态外部bool EnumChildWindows(IntPtr窗口、EnumWindowProc回调、IntPtr lParam);
私有IntPtr_主句柄;
公共表格1(IntPtr句柄)
{
这个。_MainHandle=handle;
}
公共列表GetAllChildHandles()
{
List childHandles=new List();
GCHandle gcChildhandlesList=GCHandle.Alloc(childHandles);
IntPtr pointer childhandleslist=GCHandle.ToIntPtr(gcChildhandlesList);
尝试
{
EnumWindowProc childProc=新的EnumWindowProc(EnumWindow);
EnumChildWindows(此.u主句柄、childProc、pointerChildHandlesList);
}
最后
{
gcChildHandleList.Free();
}
返回子句柄;
}
私有布尔枚举窗口(IntPtr hWnd、IntPtr lParam)
{
GCHandle gcChildhandlesList=GCHandle.FromIntPtr(LPRAM);
if(gcChildHandleList==null | | gcChildHandleList.Target==null)
{
返回false;
}
List childHandles=gcChildhandlesList.Target作为列表;
childHandles.Add(hWnd);
返回true;
}
私有无效按钮5_单击(对象发送者,事件参数e)
{
Process[]anotherApps=Process.getProcessByName(textBox1.Text);
如果(anotherApps.Length==0)返回;
如果(其他应用程序[0]!=null)
{
var allChildWindows=new Form1(其他应用程序[0].MainWindowHandle.GetAllChildHandles();
}
}
这是GetAllChildHandles()代码返回的值,但是如何使用它来获取子窗口的位置和大小呢
您没有显示如何枚举子窗口。@Aybe:这是我无法找到的缺少部分。EnumChildWindows?你好,艾比。我找到了EnumChildWindows的示例,但无法获取子窗口名称或位置。不知道如何在我的代码中使用它。咖啡厅[链接][链接]
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
if ((int)p.MainWindowHandle != 0)
{
if (!String.IsNullOrEmpty(p.MainWindowTitle))
{
myCurrentProcessName = p.ProcessName;
//listBox1.Items.Add(p.MainWindowTitle); // add all process to listbox
//find windows size and location
IntPtr ptr = p.MainWindowHandle;
Rect appRect = new Rect();
GetWindowRect(ptr, ref appRect);
x = appRect.Left;
y = appRect.Top;
r = appRect.Right;
b = appRect.Bottom;
//
//calculate size of the app
w = r - x;
h = b - y;
dataGridView1.Rows.Add(p.Id, p.MainWindowHandle, p.ProcessName, p.MainWindowTitle, IsIconic(wHnd), appState, x, y, w, h);
}
}
}
private delegate bool EnumWindowProc(IntPtr hwnd, IntPtr lParam);
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr lParam);
private IntPtr _MainHandle;
public Form1(IntPtr handle)
{
this._MainHandle = handle;
}
public List<IntPtr> GetAllChildHandles()
{
List<IntPtr> childHandles = new List<IntPtr>();
GCHandle gcChildhandlesList = GCHandle.Alloc(childHandles);
IntPtr pointerChildHandlesList = GCHandle.ToIntPtr(gcChildhandlesList);
try
{
EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
EnumChildWindows(this._MainHandle, childProc, pointerChildHandlesList);
}
finally
{
gcChildhandlesList.Free();
}
return childHandles;
}
private bool EnumWindow(IntPtr hWnd, IntPtr lParam)
{
GCHandle gcChildhandlesList = GCHandle.FromIntPtr(lParam);
if (gcChildhandlesList == null || gcChildhandlesList.Target == null)
{
return false;
}
List<IntPtr> childHandles = gcChildhandlesList.Target as List<IntPtr>;
childHandles.Add(hWnd);
return true;
}
private void button5_Click(object sender, EventArgs e)
{
Process[] anotherApps = Process.GetProcessesByName(textBox1.Text);
if (anotherApps.Length == 0) return;
if (anotherApps[0] != null)
{
var allChildWindows = new Form1(anotherApps[0].MainWindowHandle).GetAllChildHandles();
}
}