Click here to Skip to main content
14,775,325 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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

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,

Updated 29-Jul-15 10:06am
Sergey Alexandrovich Kryukov 29-Jul-15 14:48pm
Why using "WinAPI" (P/Invoke) at all? What do you want to achieve?
[no name] 29-Jul-15 15:43pm
Did you prototype PostMessage and that's the reason it won't compile?

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.

Richard Deeming 29-Jul-15 15:26pm
As far as I can see, the OP is looking to automate an external application, not simulate an event within their own application. Your answer would be correct for the latter case, but won't help in the former case.
Sergey Alexandrovich Kryukov 29-Jul-15 16:05pm
Maybe you are right; I was rather answering to the title of the question, not quite understanding the body of it. I have no idea why writing anything Windows-specific; it's always bad, compromises platform compatibility.
Thank you.
ukjohnct 29-Jul-15 15:40pm
Richard, correct.

Currently users spend a lot of time navigating the system preforming the same tasks multiple times.

The goal is for business automation that would involved :

1, navigating app (as if user were clicking menu options)
2, selecting items from lists
3, prefilling fields (eg text boxes)
4, sending click events to buttons (e.g Save)

I have managed to click a button in the app but I need to understand how to navigate menus.

This needs to do done without changing the source smalltalk application.

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


% = 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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900