问题描述
我正在尝试使用其句柄(即System.IntPtr
值)访问特定窗口:
I'm trying to access a specific window using its handle (that is System.IntPtr
value):
// Getting the process of Visual Studio program
var process = Process.GetProcessesByName("devenv")[0];
// Showing the handle we've got, we've no problem
MessageBox.Show(this, process.MainWindowHandle.ToString());
// Attempting to get the main window object by its handle
var wnd = NativeWindow.FromHandle(process.MainWindowHandle);
// always fails
if (wnd == null)
MessageBox.Show("Failed");
else
MessageBox.Show(wnd.ToString(), "Yeeeeees!!");
我也试图访问为此目的而创建的另一个演示.net winforms应用程序的主窗口(即,我运行演示应用程序,并尝试通过该应用程序访问其主窗口),并且也失败了,尽管该演示和此应用程序均为.NET应用程序.但是,成功了:
I have tried also to access another demo .net winforms application's main window, that I have made for this purpose, (i.e. I run the demo application, and attempted to access its main window by this application), and failed, too, although both the demo and this application are .NET applications. However, this successes:
var process2 = Process.GetCurrentProcess();
MessageBox.Show(this, process2.MainWindowHandle.ToString());
var wnd2 = NativeWindow.FromHandle(process2.MainWindowHandle);
if (wnd2 == null)
MessageBox.Show("Failed");
else
MessageBox.Show(wnd2.ToString(), "Yes");
我认为这是可行的,因为它是从同一应用程序中调用的.那么,如何通过其句柄访问另一个程序的窗口对象?我以为可以使用C\C++
并通过使用头文件<windows.h>
然后使用P \ invoke来工作.
I think this works because it is invoked from the same application. So, how can I access some another program's window object by its handle?I thought it can work using C\C++
by using header file <windows.h>
and then using a P\invoke.
如果不能,是否还有另一种访问窗口的方法(即,而不是使用句柄)?
If I can't, is there another way to access a window (i.e. rather than using handles)?
===================编辑
===================EDIT
我想处理整个窗口对象及其自己的控件
I want to deal with the entire window object and its own controls
推荐答案
然后,如Raymond所建议的那样,为什么不尝试使用Automation?添加引用UIAutomationClient
和UIAutomationTypes
Then, as Raymond suggested, why don't you try with Automation? Add a console project with references to UIAutomationClient
and UIAutomationTypes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;
using System.Diagnostics;
using System.Threading;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
var pInfo = new ProcessStartInfo("notepad");
var p = Process.Start(pInfo);
p.WaitForInputIdle();
AutomationElement installerEditorForm = AutomationElement.FromHandle(p.MainWindowHandle);
// menus
AutomationElementCollection menuBars = installerEditorForm.FindAll(TreeScope.Children, new PropertyCondition(
AutomationElement.ControlTypeProperty, ControlType.MenuBar));
var mainMenuItem = menuBars[0];
AutomationElementCollection menus = mainMenuItem.FindAll(TreeScope.Children, new PropertyCondition(
AutomationElement.ControlTypeProperty, ControlType.MenuItem));
var fileMenuItem = menus[0];
ExpandCollapsePattern fileMenuItemOpenPattern = (ExpandCollapsePattern)fileMenuItem.GetCurrentPattern(
ExpandCollapsePattern.Pattern);
fileMenuItemOpenPattern.Expand();
AutomationElement fileMenuItemNew = fileMenuItem.FindFirst(TreeScope.Children,
new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.MenuItem),
new PropertyCondition(AutomationElement.NameProperty, "New")));
Console.Read();
}
}
}
这篇关于如何访问一个窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!