微光互联 TX800-U 扫码器无法输出中文到光标的问题( 四 )

调用点仅需稍加改造就可以了:
printf("%.*s\n", datalen, readBuffers);std::string gb2312 = utf8_to_gb2312(std::string((char *)readBuffers, datalen));copy_to_system_clipboard(gb2312);set_text_to_active_windows(gb2312);编译运行,先启动一个 notepad 应用,将光标置于其中 , 便于稍后看输出结果,然而扫码后没有任何输出 。将上面的 GetForegroundWindow 替换为 GetActiveWindow 或 GetDesktopWindows 都没有效果,更神奇的是加的许多 printf 调试日志也没有输出,这真是见了鬼了:
open dev success!开始解码:二维码长度:10浜琈D0926ready to copy data: 京MD0926copy to clipboard oksend text to active window return 0: 京MD0926二维码长度:18LFV3A23C083027701ready to copy data: LFV3A23C083027701copy to clipboard oksend text to active window return 0: LFV3A23C083027701只输出最终的一个调用结果 。一开始怀疑是 console 程序和 win32 界面程序的不同,决定新建一个新的 win32 应用试试,由于 Win32 应用的主线程要做消息循环,这里启动一个单独的线程跑扫码的逻辑:
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,                     _In_opt_ HINSTANCE hPrevInstance,                     _In_ LPWSTR    lpCmdLine,                     _In_ int       nCmdShow){    UNREFERENCED_PARAMETER(hPrevInstance);    UNREFERENCED_PARAMETER(lpCmdLine);    // TODO: Place code here.    // Initialize global strings    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);    LoadStringW(hInstance, IDC_DEMOHIDPROTOCAL, szWindowClass, MAX_LOADSTRING);    MyRegisterClass(hInstance);    // Perform application initialization:    if (!InitInstance (hInstance, nCmdShow))    {        return FALSE;    }    hThread = CreateThread(NULL, 0, qrscanner_loop, NULL, 0, NULL);    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_DEMOHIDPROTOCAL));    MSG msg;    // Main message loop:    while (GetMessage(&msg, nullptr, 0, 0))    {        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))        {            TranslateMessage(&msg);            DispatchMessage(&msg);        }    }    return (int) msg.wParam;}qrscanner_loop 就是之前 console main 那一堆东西了,为了展示信息,在默认的视图中间填充一个 edit 控件:
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){   hInst = hInstance; // Store instance handle in our global variable   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);   if (!hWnd)   {      return FALSE;   }   RECT rect = { 0 };   GetClientRect(hWnd, &rect);   hEdit = CreateWindowW(TEXT("Edit"), TEXT(""),       WS_CHILD | WS_VISIBLE | ES_LEFT | ES_MULTILINE | ES_WANTRETURN | WS_VSCROLL | ES_AUTOVSCROLL,       rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top, hWnd, (HMENU)10002, hInstance, NULL);   ShowWindow(hWnd, nCmdShow);   UpdateWindow(hWnd);   return TRUE;}

推荐阅读