Skip to content

Instantly share code, notes, and snippets.

@MSDN-WhiteKnight
Created July 25, 2019 07:02
Show Gist options
  • Save MSDN-WhiteKnight/ee7b70d7705bd1c0994fe6118a8d3ca5 to your computer and use it in GitHub Desktop.
Save MSDN-WhiteKnight/ee7b70d7705bd1c0994fe6118a8d3ca5 to your computer and use it in GitHub Desktop.
GetWindowText example
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tchar.h>
LPCWSTR szWindowClass = L"MYWINDOW";
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
const int size = 24;
BOOL res;
switch (message)
{
case WM_CREATE:
wchar_t name[size];
memset(name, 0, size);
res = GetWindowTextW(hWnd, name, size);
if (res == FALSE) wprintf(L"GetWindowTextW Error!\n");
wprintf(L"%ls / %d\n", name, GetWindowTextLengthW(hWnd)); //Test Player / 11
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
LineTo(hdc, 50, 50);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(nullptr, IDI_APPLICATION);
return RegisterClassExW(&wcex);
}
int main(int argc, char **argv)
{
HINSTANCE hInstance = GetModuleHandle(NULL);
ATOM atom = MyRegisterClass(hInstance);
HWND MainWindow = CreateWindowExW(
WS_EX_TOPMOST,
szWindowClass,
L"Test Player",
WS_POPUPWINDOW | SW_HIDE,
0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
nullptr, nullptr, hInstance, nullptr);
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment