Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- //--------------------------------------------------------------------------------
- HINSTANCE hInst;
- #define N 512
- wchar_t wstr[N];
- char utf8[N*2];
- HWND ourWnd = NULL,
- foobarWnd = NULL;
- //--------------------------------------------------------------------------------
- BOOL CALLBACK EnumWindowsProc(__in HWND hwnd, __in LPARAM lParam)
- {
- int count = GetWindowText(hwnd, wstr, N);
- if(hwnd == ourWnd
- || wcsstr(wstr, L"[foobar2000 v") == NULL)
- return TRUE;
- foobarWnd = hwnd;
- return FALSE;
- }
- //--------------------------------------------------------------------------------
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_DESTROY:
- hWnd = NULL;
- case WM_CLOSE:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- //--------------------------------------------------------------------------------
- void MainFunction()
- {
- // Find foobar2000 window if necessary.
- if(foobarWnd == NULL)
- EnumWindows(&EnumWindowsProc, NULL);
- //----
- // Get wide character string (UTF-16)
- if(GetWindowText(foobarWnd, wstr, N) == 0)
- EnumWindows(&EnumWindowsProc, NULL);
- //----
- // Convert to UTF-8
- WideCharToMultiByte(CP_UTF8, 0, wstr, -1, utf8, 2*N, NULL, NULL);
- //----
- // Display byte stream
- SetWindowTextA(ourWnd, utf8);
- }
- //--------------------------------------------------------------------------------
- BOOL SetupWindow()
- {
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = 0;
- wcex.lpfnWndProc = WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInst;
- wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = NULL;
- wcex.lpszClassName = L"UTF-8 Converter";
- wcex.hIconSm = NULL;
- RegisterClassEx(&wcex);
- ourWnd = CreateWindow(L"UTF-8 Converter", L"Can't find foobar2000 window...", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInst, NULL);
- if(!ourWnd)
- return FALSE;
- RECT r;
- GetWindowRect (ourWnd, &r);
- MoveWindow (ourWnd, r.left, r.top, 640, 0, FALSE);
- ShowWindow (ourWnd, SW_SHOW);
- UpdateWindow (ourWnd);
- return TRUE;
- }
- //--------------------------------------------------------------------------------
- int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- // Store instance handle in our global variable
- hInst = hInstance;
- MSG msg;
- // application initialization
- if(!SetupWindow())
- return FALSE;
- // Main loop
- while(true)
- {
- if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- if(msg.message == WM_QUIT)
- break;
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- MainFunction();
- Sleep(10);
- }
- return msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment