简体   繁体   English

如何从C#中的应用程序处理在后台运行的应用程序的菜单项

[英]How to get handle for menu items of an application running in the back ground from an application in C#

I am working on a application, which would be the face of lot of other tools running in background. 我正在开发一个应用程序,这将是许多其他工具在后台运行的表面。 I am facing an issue. 我正面临一个问题。 During the launch of a background application ,it needs to load a specific file(file-->load--> file name). 在启动后台应用程序期间,它需要加载特定文件(文件 - >加载 - >文件名)。

Let the front end application be Fapp and the background application be Bapp . 让前端应用程序为Fapp ,后台应用程序为Bapp Is it possible for Fapp to get the handle of Bapp's menu item and trigger the load function. Fapp是否可以获取Bapp's菜单项的句柄并触发加载功能。 I am able to get the handle for buttons but not able to do the same for menu items. 我能够获得按钮的句柄,但不能对菜单项执行相同的操作。

Now we are achieving this using AutoIt, I am trying to achieve this in C# itself. 现在我们使用AutoIt实现这一点,我试图在C#中实现这一点。

After you have obtained the handle of the window that you want to invoke its menus, then you may use 获得要调用其菜单的窗口的句柄后,您可以使用

  • HMENU GetMenu(HWND) windows api to get menu HMENU GetMenu(HWND) windows api获取菜单
  • HMENU GetSubMenu(HMENU, int) to get to the file menu and again to open menu. HMENU GetSubMenu(HMENU, int)进入文件菜单并再次打开菜单。
  • BOOL GetMenuItemInfo( ... ) to get info about menu BOOL GetMenuItemInfo( ... )获取有关菜单的信息
  • and you can use PostMessage((IntPtr)hWnd, WM_COMMAND, 0, ID_MENU_ITEM); 你可以使用PostMessage((IntPtr)hWnd, WM_COMMAND, 0, ID_MENU_ITEM); ( related post ) to perform a click on that item. 相关帖子 )点击该项目。

all these apis are what AutoIt calls (I think). 所有这些apis都是AutoIt所称的(我认为)。 This solution works if your Bapp is a normal windows application with a normal windows menu, not a fancy WPF app, or a ribbon. 如果您的Bapp是普通的Windows应用程序,具有正常的Windows菜单,而不是花哨的WPF应用程序或功能区,则此解决方案有效。 If this is the case, then what you see as menu probably is not a menu (technically anyway) 如果是这种情况,那么你看到的菜单可能不是菜单(技术上无论如何)

Are you sure this is the right way to get two applications talking to each other? 您确定这是让两个应用程序相互通信的正确方法吗?

If you don't have source code for BApp, and it also doesn't have an API that you can use, then pretending to be an interactive user could be the only way to interact with it. 如果您没有BApp的源代码,并且它也没有可以使用的API,那么伪装成交互式用户可能是与其进行交互的唯一方式。 Just be aware that it is fraught with issues, consider what will happen when 请注意,它充满了问题,考虑一下会发生什么

  • BApp isn't already running BApp尚未运行
  • BApp has a modal dialog open BApp打开了一个模态对话框
  • BApp is in the middle of an operation (or hanging) and its menu is disabled BApp处于操作(或挂起)的中间,其菜单被禁用
  • BApp is updated to a new version and its UI changes BApp已更新为新版本,其UI更改
  • An interactive user changes focus, in the middle of an operation. 交互式用户在操作过程中更改焦点。

An alternative to this would be to do the same thing that you do when you are unit testing an application with a UI. 替代方法是执行与使用UI对应用程序进行单元测试时所执行的操作相同的操作。 This is because you are doing the same thing, automating an application by making calls that execute its functions, in this case to test the results are as expected. 这是因为您正在做同样的事情,通过执行执行其功能的调用来自动化应用程序,在这种情况下,测试结果是按预期的。 Since this is a WPF post lets assume that you are writing an application with MVVM, and the best way (to avoid brittleness when we are change the UI) is to ignore the UI (View) and call the layer that sits underneath ie the VM (ViewModel). 由于这是WPF帖子,我们假设您正在使用MVVM编写应用程序,并且最好的方法(为了避免在更改UI时出现漏洞)是忽略UI(View)并调用位于其下的层,即VM (视图模型)。

In fact its quite easy just to add a self-hosted WCF connection inside your BApp application so that it can be called externally. 实际上,只需在BApp应用程序中添加自托管WCF连接就可以很容易地在外部调用它。

        this._host = new ServiceHost(service);
        this._host.AddServiceEndpoint(typeof(IContract), new NetTcpBinding(), address);
        this._host.Open();

This would then enable you to get the two talking totally independently. 这将使您能够完全独立地进行两个谈话。

If your Bapp is able to somehow invoke Win32 API - then this can be achieved by sending a custom WM_USER message to your Fapp - using SendMessage() . 如果您的Bapp能够以某种方式调用Win32 API - 那么这可以通过使用SendMessage()向您的Fapp发送自定义WM_USER消息来实现。 In your Fapp you handle this message and take appropriate action. 在您的Fapp中,您处理此消息并采取适当的措施。

I don't think getting handle to a control and invoking its handler is the right way. 我不认为获取控件的句柄并调用其处理程序是正确的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在c#控制台应用程序菜单中添加/删除项目 - How To Add/Remove Items in c# Console Application Menu 使用C#by Handle获取Excel应用程序的实例 - Get instance of Excel application with C# by Handle 如何在 C# 中显示列表中的项目(控制台应用程序) - How to display items from List in C# (console application) 如何将文本从非托管应用程序转换为ac#应用程序? - How to get text from unmanaged application to a c# application? 如何使用C#Windows Service获取正在运行的应用程序 - How to Get Running Application with C# Windows Service C#如何获取正在运行远程应用程序的虚拟IP - C# how to get the virtual ip on which a a remote application is running 如何处理C#控制台应用程序终止? - How to handle a C# console application terminating? 如何从C#中正在运行的进程中获取在应用程序中打开的文件的路径 - How to get the path of the file which is opened in an application from the running process in c# C#:如何从WPF应用程序的多个运行实例中获取最近使用的实例? - C# : How to get most recently used instance from multiple running instances of my WPF application? 如何处理 C# 中的菜单项以传递值 - How to handle menu items in C# for passing values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM