微光互聯 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;}

推薦閱讀