以编程方式单击c#中的菜单项 [英] Programmatically clicking a menu item in c#

查看:283
本文介绍了以编程方式单击c#中的菜单项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在运行的旧Smalltalk应用程序,我想让我的c#程序访问其菜单并单击一个项目。该应用程序有4个子菜单,此时只能看到它调用任何一个都会很好。



我一直在圈子里搜索这个但是没有让它工作。在我的代码中点击一个菜单项我需要什么?



我看到提到SendMessage的一个例子,我看到另一个有PostMessage的例子?



到目前为止,我有...



I have an old Smalltalk application that is running and I want to have my c# program access its menu and click on an item. The app has 4 submenus and at this point just being able to see it call any one would be good.

I have been going around in circles googling this but have not got it working. What do I need in my code to click on a menu item?

One example I saw mentioned SendMessage and I see another with PostMessage?

So far I have...

private const uint WM_COMMAND = 0x111;

            IntPtr theWindow = (IntPtr)0;
            theWindow = FindWindow((string)null, "My Smalltalk App");


              IntPtr hMainMenu = GetMenu(theWindow);
              IntPtr hMenu = GetSubMenu(hMainMenu, 0);
             uint MenuID = GetMenuItemID(hMenu, 1);  // for future use

             PostMessage(hMenu, WM_COMMAND, 2, 0);   //  Does not compile



抱歉,我对Win API的使用经验不多,而且我正在努力获得一个概念验证工作。



有人可以给我一些可以点击的代码菜单项?



提前致谢,



John。


Sorry but I have little experience of Win API usage and I am trying to get a proof of concept working.

Could someone be kind enough to give me some code that would click a menu item?

Thanks in advance,

John.

推荐答案

请参阅我对该问题的评论。到目前为止,我认为没有任何理由去做你正在做的事情。



但即使不了解你在做什么,也可以做一个有用的答案,因为这个与一个非常常见的误解相关:调用事件而不是仅处理事件的想法。



如果您使用纯.NET和.NET事件,则不会能够直接调用.NET FCL中定义的任何事件,因为除了声明事件成员的类型的方法的代码之外,.NET不允许从任何地方调用事件;这是一个重要的傻瓜式功能。 (当然,某些类型可以暴露一些显式的方法,例如使用WPF RaiseEvent ;将其视为某些类型实现的后门。)问题是:你不要真的需要它。



正确的方法很简单:通过说以编程方式点击一个菜单项你从来没有真正意味着点击。 (只有一个用户可以用鼠标点击某些东西;没有程序化点击,它只能是一些仿真。)你的意思是实现与点击相同的精确效果。如果您有这样的效果,则意味着您已将某个处理程序添加到某个事件实例的调用列表中。如果这样做,你可以调用一些函数,只有一个函数来自这个处理程序,并且这个函数可以单独声明。您所需要的只是在其他地方调用此功能。是的,就这么简单。



-SA
Please see my comment to the question. So far, I don't see any reason for doing what you are doing.

But a useful answer can be done even without understanding what you are doing, because this is related to one pretty common misconception: the idea to invoke an event instead of just handling it.

If you used pure .NET and .NET events, you would not be able to directly invoke any events defined in .NET FCL, because .NET does not allow event invocation from anywhere except the code of the method of the type where the event member is declared; this is an important fool-proof feature. (Of course, some types can expose some methods doing it explicitly, such as with WPF RaiseEvent; consider it as some backdoor implemented by some types.) The thing is: you don't really need it.

The right approach is simple: by saying "programmatically clicking a menu item" you never really mean a click. (Only a use can click something with a mouse; there are no "programmatic click", it can only be some emulation.) You really mean to achieve the same exact effect as with click. If you have such effect, it means that you have an handler added to an invocation list of some event instance. If you do so, you can call some function, only one function from this handler, and this function can be declared separately. All you need is to call this function elsewhere. Yes, as simple as that.

—SA


为了演示我得到了一个使用Sendkeys工作的解决方案。



For the sake of teh demo I got a solution working using Sendkeys.

SendKeys.SendWait("%A");
SendKeys.SendWait("p");





%= ALT,所以ALT A会带我到一个子菜单,然后选择我需要的菜单。



我在那里仍然必须有一个更好的解决方案,但这个让我通过演示。



感谢大家的评论。



% = ALT, so ALT A would take me to a sub menu and p selects the menu that I needed.

I beleive there still must be a better solution but this one got me through the demo.

thanks to everyone for their comments.


这篇关于以编程方式单击c#中的菜单项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆