C#WinAPI的点击菜单项 [英] C# WinAPI Clicking on menu items

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

问题描述

我想点击一个叫做媒体Subtitler和任何我想做它不工作程序中的菜单项。

I'm trying to click on a menu item inside a program called Media Subtitler and whatever I'm trying to do it's not working.

首先,我试图用函数使用getMenu但它返回IntPtr.Zero。
然后,我尝试使用ALT键+使用我的菜单的第一个字母(F代表文件),但它也没做。
然后,我试图用一个简单的MOUSEDOWN和MouseUp消息,但再次,它什么也没做(我也尝试创建一个循环,在这个范围内的一切单击但有在该区域没有点击)。

First, I tried to use the function GetMenu but it returned IntPtr.Zero. Then, I tried using the ALT key + using the first letter of my menu (F stands for file) but it did nothing. Then, I tried using a simple MOUSEDOWN and MOUSEUP messages but again, it did nothing (I also tried creating a loop that clicks on everything in that range but there was no click in that area).

我清楚知道的是,我正在正确的窗口上。

What I clearly know is that I'm working on the correct window.

我在做什么错了?

如果有人想测试一下你可以下载媒体Subtitler免费的,它不重量太大。

If someone wants to test it out you can download Media Subtitler for free and it doesn't weight that much.

此外,这里的code我一直在测试:

Also, Here's the code I've been testing:

Process p = Process.Start(@"C:\Program Files\DivXLand\Media Subtitler\MediaSub.exe");
        p.WaitForInputIdle(1500);
        Thread.Sleep(3000);

        SetForegroundWindow(p.MainWindowHandle);
        ShowWindow(p.MainWindowHandle, SW_MAXIMIZE);

        IntPtr handle = p.MainWindowHandle;

        SendMessage(handle, WM_NCHITTEST, 0, MakeLParam(18, 29));

        //for (int i = 0; i < 200; i++)
        //{
        //    for (int x = 0; x < 200; x++)
        //    {
        //        SendMessage(p.MainWindowHandle, WM_LBUTTONDOWN, 0, MakeLParam(i, x));
        //        SendMessage(p.MainWindowHandle, WM_LBUTTONUP, 0, MakeLParam(i, x));
        //    }
        //}
        //IntPtr menuItems = GetMenu(p.MainWindowHandle);
        return;
        //SendMessage(p.MainWindowHandle, WM_COMMAND, 6, 0);
        SendMessage(p.MainWindowHandle, WM_KEYDOWN, VK_MENU, 0);
        SendMessage(p.MainWindowHandle, WM_KEYUP, VK_MENU, 0);
        SendMessage(p.MainWindowHandle, WM_KEYDOWN, VK_F, 0);
        SendMessage(p.MainWindowHandle, WM_KEYUP, VK_F, 0);

感谢您的帮助!

推荐答案

通过监控发送到应用程序的主窗口中的消息,我提取的菜单项目的菜单识别符。您可以发布 WM_COMMAND 消息窗口,用菜单项为的wParam 的ID:

By monitoring the messages sent to the main window of the application, I extracted the menu identifiers for the menu items. You can post WM_COMMAND message to the window, with the ID of the menu items as the wParam:

[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, Message msg, int wParam, int lParam);

PostMessage(handle, WM_COMMAND, 2, 0); // File->New subtitle

PostMessage(handle, WM_COMMAND, 3, 0); // File->New from clipboard

PostMessage(handle, WM_COMMAND, 5, 0); // File->Open text or subtitle

PostMessage(handle, WM_COMMAND, 6, 0); // File->Open video

...

我测试过的code。与媒体Subtitler,它就像一个魅力!唯一的局面,这是行不通的,是当在Windows Vista或七,你的目标程序正在运行的Administrator和C#程序是没有的。意识到这一点!

I've tested the code with Media Subtitler, and it works like a charm! The only situation that this will not work, is when on windows Vista or Seven, your target program is running as Administrator and you C# program is not. Be aware of that!

菜单ID可以通过(使用间谍++)监测 WM_COMMAND 消息很容易检查。结果
您还可以使用 SendMessage函数而不是 PostMessage的,但你的程序没有响应,直到用户关闭通过菜单打开的窗口采取行动。

The menu IDs can be easily examined by monitoring the WM_COMMAND message (using Spy++).
You can also use SendMessage instead of PostMessage, but then your program freezes until the user closes the window opened by the menu action.

您可以使用同样的方法来发送其他命令到应用程序的其他窗口。例如,单击打开视频窗口的打开按钮。

You can use the same approach to send other command to other windows of the application. For example, clicking the 'Open' button of the 'Open video' window.

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

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