控制外部Qt应用程序 - 打开文件

时间:2013-02-12 19:08:17

标签: c# qt ui-automation

我需要能够控制外部Qt应用程序,以便我可以在应用程序中打开文件。

我尝试使用Process获取Window Handle,然后使用GetMenu,GetSubMenu和GetMenuItemID通过PInvoke获取使用SendMessage的所有参数和外部应用程序中打开菜单上的“Click”

如果我使用Notepad作为外部应用程序进行尝试,但这与使用Qt编写的实际应用程序无关。

我确实得到了Window句柄,但GetMenu返回0。

我有这段代码

[DllImport("user32.dll")]
private static extern IntPtr GetMenu(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern IntPtr GetSubMenu(IntPtr hMenu, int nPos);

[DllImport("user32.dll")]
private static extern uint GetMenuItemID(IntPtr hMenu, int nPos);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);


private void OpenButton_Click(object sender, EventArgs e)
{
    OpenDocument("notepad", "test.doc");
}

public void OpenDocument(string windowTitle, string document)
{
    IntPtr hWnd = GetWindow(windowTitle);
    IntPtr hMenu = GetMenu(hWnd);
    IntPtr hSubMenu = GetSubMenu(hMenu, 0);    // File menu
    uint menuItemId = GetMenuItemID(hSubMenu, 2);  // Open

    IntPtr ptr = SendMessage(hWnd, (uint)WM.COMMAND, (IntPtr)menuItemId, IntPtr.Zero);
}

private static IntPtr GetWindow(string windowTitle)
{
    IntPtr hWnd = IntPtr.Zero;
    Process[] processes = Process.GetProcesses();
    foreach (Process p in processes)
    {
        if (p.MainWindowTitle.IndexOf(windowTitle, StringComparison.InvariantCultureIgnoreCase) > -1)
        {
            hWnd = p.MainWindowHandle;
            break;
        }
    }

    return hWnd;
}

如何从Qt应用程序中获取菜单和子菜单以及menuitemid的句柄?

// Anders

0 个答案:

没有答案
相关问题