使用c++从活动窗口获取选定文本

Acquiring selected text from active window using c++

本文关键字:文本 获取 窗口 c++ 活动 使用      更新时间:2023-10-16

我有下面的代码,我试图从活动窗口获得选定的文本,并将其打印到控制台上。

DWORD new12=0;
KEYBDINPUT* input = new KEYBDINPUT[key_count];
    if( GetGUIThreadInfo( new12, lpgui ) )
{
    target_window = lpgui->hwndFocus;
}
else
{
   // You can get more information on why the function failed by calling
   // the win32 function, GetLastError().
   std::cout<<"error1";
}
// We're sending two keys CONTROL and 'V'. Since keydown and keyup are two
// seperate messages, we multiply that number by two.
for( int i = 0; i < key_count; i++ )
{
    input[i].dwFlags = 0;
    //input[i].type = INPUT_KEYBOARD;
}
input[0].wVk = VK_CONTROL;
input[0].wScan = MapVirtualKey( VK_CONTROL, MAPVK_VK_TO_VSC );
input[1].wVk = 0x56; // Virtual key code for 'v'
input[1].wScan = MapVirtualKey( 0x56, MAPVK_VK_TO_VSC );

我有上面的c++代码,但它似乎在input[0].wScan = MapVirtualKey( VK_CONTROL, MAPVK_VK_TO_VSC );行中给出了一个错误说"error: MAPVK_VK_TO_VSC' was not declared in this scope"我想知道这里有什么问题。我不认为这个错误是由于任何声明问题而弹出的。你能帮我一下吗?谢谢你。

MAPVK_VK_TO_VSC是一个简单的#define MAPVK_VK_TO_VSC (0),甚至不是一个常数,所以它应该在预处理阶段解决。

要么你错过了包括"winuser.h"在这段代码之前(在这种情况下MapVirtualKey和VK_常量将未声明),或者你有未定义的WIN_VER某处(或定义它小于0x400)。有时很容易忘记WIN_VER必须以十六进制定义,并且使用#define WINVER 500,您的版本小于2.0