如何从Windows中的外部应用程序激活某些窗口?

发布于 2024-12-18 09:08:48 字数 1258 浏览 5 评论 0原文

我想向外部应用程序发送一些击键,并且工作正常,但是当我尝试将击键发送到同一外部应用程序的子窗口时,由于某种原因不起作用,所以我需要帮助。假设我们想要从记事本打印剪贴板文本,并且想要一步完成。代码看起来像这样。

        #include <windows.h>
    #include <stdio.h>
    #include <iostream.h>
    using namespace std;

    int main(int argc, char* argv[]){
        WinExec("notepad", 1);
        Sleep(1000);
        HWND handle = FindWindow("notepad",0);  // it's handling as well
        SetForegroundWindow(handle);        
        keybd_event(VK_CONTROL, 0, 0, 0);   // simulate CTRL down
        keybd_event(VkKeyScan('V'), 0, 0, 0);
        keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0); 
        Sleep(500);
        keybd_event(VK_CONTROL, 0, 0, 0);   // simulate CTRL down
        keybd_event(VkKeyScan('P'), 0, 0, 0);
        keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0); // simulate CTRL up
        Sleep(1000);
        HWND handle1 = FindWindow(0, "Print");  // it wann't find "Print" window
        SetForegroundWindow(handle1);
        keybd_event(VK_MENU, 0, 0, 0); // simulate ALT down
        keybd_event(VkKeyScan('P'), 0, 0, 0);
        keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
        return 0;
    }

但它想将 ALT+P 发送到“打印”窗口,为什么? 最终目标是制作发送到应用程序按键的小宏(在任何 Windows 子级或父级上..) 操作系统:WIN 7 64位

I want to send some keystroke to the external application, and it's work fine, but when I try to send keystroke to the child window of same external application, for some reason that doesn't work, so I need help. Let's say that we want to print clipboard text from notepad, and want to do it at one step. At code that will look like this.

        #include <windows.h>
    #include <stdio.h>
    #include <iostream.h>
    using namespace std;

    int main(int argc, char* argv[]){
        WinExec("notepad", 1);
        Sleep(1000);
        HWND handle = FindWindow("notepad",0);  // it's handling as well
        SetForegroundWindow(handle);        
        keybd_event(VK_CONTROL, 0, 0, 0);   // simulate CTRL down
        keybd_event(VkKeyScan('V'), 0, 0, 0);
        keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0); 
        Sleep(500);
        keybd_event(VK_CONTROL, 0, 0, 0);   // simulate CTRL down
        keybd_event(VkKeyScan('P'), 0, 0, 0);
        keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0); // simulate CTRL up
        Sleep(1000);
        HWND handle1 = FindWindow(0, "Print");  // it wann't find "Print" window
        SetForegroundWindow(handle1);
        keybd_event(VK_MENU, 0, 0, 0); // simulate ALT down
        keybd_event(VkKeyScan('P'), 0, 0, 0);
        keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
        return 0;
    }

But it want to send ALT+P to "Print" window, why?
Final goal is to make little macro that send to application keystorkes (on any windows child, or parent..)
OS: WIN 7 64bit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兮颜 2024-12-25 09:08:48

您可以通过简单地删除这些行来使现有代码(在某种程度上)工作:

HWND handle1 = FindWindow(0, "Print");  // it wann't find "Print" window
SetForegroundWindow(handle1);

请记住,伪造的输入将转到具有输入焦点的线程,并且当您在记事本中显示打印对话框时,该对话框将获得输入焦点。您根本不需要设置焦点,系统会为您完成此操作。

然而,您所采取的方法非常脆弱。我怀疑使用 UI Automation 之类的东西会更好地为您提供服务。

You can probably make the existing code (sort of) work by simply removing these lines:

HWND handle1 = FindWindow(0, "Print");  // it wann't find "Print" window
SetForegroundWindow(handle1);

Remember that faked input goes to the thread which has the input focus and when you show the print dialog in Notepad, that dialog will gain the input focus. You simply do not need to set the focus, the system will do that for you.

However, the approach you are taking is incredibly brittle. I suspect that you would be far better served by using something like UI Automation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文