Guest User

ReinterpretCodepage.cpp

a guest
May 31st, 2011
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1. #define WIN32_LEAN_AND_MEAN
  2. #include <windows.h>
  3.  
  4. //--------------------------------------------------------------------------------
  5.  
  6. HINSTANCE   hInst;
  7.  
  8. #define     N 512
  9.  
  10. wchar_t     wstr[N];
  11. char        utf8[N*2];
  12.  
  13. HWND        ourWnd    = NULL,
  14.             foobarWnd = NULL;
  15.  
  16. //--------------------------------------------------------------------------------
  17. BOOL CALLBACK EnumWindowsProc(__in HWND hwnd, __in LPARAM lParam)
  18. {
  19.    int count = GetWindowText(hwnd, wstr, N);
  20.  
  21.    if(hwnd == ourWnd
  22.    ||   wcsstr(wstr, L"[foobar2000 v") == NULL)
  23.       return TRUE;
  24.  
  25.    foobarWnd = hwnd;
  26.    return FALSE;
  27. }
  28.  
  29. //--------------------------------------------------------------------------------
  30. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  31. {
  32.    switch (message)
  33.    {
  34.    case WM_DESTROY:
  35.       hWnd = NULL;
  36.    case WM_CLOSE:
  37.       PostQuitMessage(0);
  38.       break;
  39.  
  40.    default:
  41.       return DefWindowProc(hWnd, message, wParam, lParam);
  42.    }
  43.  
  44.    return 0;
  45. }
  46.  
  47.  
  48. //--------------------------------------------------------------------------------
  49. void MainFunction()
  50. {
  51. // Find foobar2000 window if necessary.
  52.    if(foobarWnd == NULL)
  53.       EnumWindows(&EnumWindowsProc, NULL);
  54.  
  55. //----
  56. // Get wide character string (UTF-16)
  57.    if(GetWindowText(foobarWnd, wstr, N) == 0)
  58.       EnumWindows(&EnumWindowsProc, NULL);
  59.  
  60. //----
  61. // Convert to UTF-8
  62.    WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8, 2*N, NULL, NULL);
  63.  
  64. //----
  65. // Display byte stream
  66.    SetWindowTextA(ourWnd, utf8);
  67. }
  68.  
  69.  
  70.  
  71. //--------------------------------------------------------------------------------
  72. BOOL SetupWindow()
  73. {
  74.    WNDCLASSEX wcex;
  75.  
  76.    wcex.cbSize = sizeof(WNDCLASSEX);
  77.  
  78.    wcex.style               = 0;
  79.    wcex.lpfnWndProc     = WndProc;
  80.    wcex.cbClsExtra      = 0;
  81.    wcex.cbWndExtra      = 0;
  82.    wcex.hInstance           = hInst;
  83.    wcex.hIcon               = LoadIcon(NULL, IDI_APPLICATION);
  84.    wcex.hCursor         = LoadCursor(NULL, IDC_ARROW);
  85.    wcex.hbrBackground   = (HBRUSH)(COLOR_WINDOW+1);
  86.    wcex.lpszMenuName        = NULL;
  87.    wcex.lpszClassName   = L"UTF-8 Converter";
  88.    wcex.hIconSm         = NULL;
  89.  
  90.    RegisterClassEx(&wcex);
  91.  
  92.    ourWnd = CreateWindow(L"UTF-8 Converter", L"Can't find foobar2000 window...", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInst, NULL);
  93.  
  94.    if(!ourWnd)
  95.       return FALSE;
  96.  
  97.    RECT r;
  98.    GetWindowRect    (ourWnd, &r);
  99.    MoveWindow       (ourWnd, r.left, r.top, 640, 0, FALSE);
  100.    ShowWindow       (ourWnd, SW_SHOW);
  101.    UpdateWindow (ourWnd);
  102.  
  103.    return TRUE;
  104. }
  105.  
  106.  
  107.  
  108.  
  109. //--------------------------------------------------------------------------------
  110. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  111. {
  112. // Store instance handle in our global variable
  113.    hInst = hInstance;
  114.  
  115.    MSG msg;
  116.  
  117. // application initialization
  118.    if(!SetupWindow())
  119.       return FALSE;
  120.  
  121. // Main loop
  122.    while(true)
  123.    {
  124.       if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  125.       {
  126.          if(msg.message == WM_QUIT)
  127.             break;
  128.          TranslateMessage(&msg);
  129.          DispatchMessage(&msg);
  130.       }
  131.  
  132.       MainFunction();
  133.       Sleep(10);
  134.    }
  135.  
  136.    return msg.wParam;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment