C++ draw text on screen over a window and follow window when it moves
- User
C++ draw text on screen over a window and follow window when it moves
- Assistant
To draw text on the screen over a window and make it follow the window when it moves, you can make use of the Win32 API functions to handle window messages and drawing.
Here's a simple example that demonstrates how to achieve this:
#include <windows.h> #include <string> #define ID_TIMER 1 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // Register the window class const char *CLASS_NAME = "MyWindowClass"; WNDCLASS wc = {}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = CLASS_NAME; RegisterClass(&wc); // Create the window HWND hwnd = CreateWindowEx( 0, // Optional window styles CLASS_NAME, // Window class "C++ Draw Text on Screen", // Window text WS_OVERLAPPEDWINDOW, // Window style // Size and position CW_USEDEFAULT, CW_USEDEFAULT, 500, 400, NULL, // Parent window NULL, // Menu hInstance, // Instance handle NULL // Additional application data ); if (hwnd == NULL) { return 0; } // Create a timer to continuously redraw the text SetTimer(hwnd, ID_TIMER, 10, NULL); // Display the window ShowWindow(hwnd, nCmdShow); // Run the message loop MSG msg = {}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static std::string text = "Hello, world!"; static RECT windowRect; static RECT textRect; switch (msg) { case WM_CREATE: // Initialize the window and text rectangles GetClientRect(hwnd, &windowRect); textRect.left = windowRect.left + 10; // Adjust the coordinates to your desired position textRect.top = windowRect.top + 10; textRect.right = textRect.left + 100; // Adjust the size according to your needs textRect.bottom = textRect.top + 20; break; case WM_TIMER: // Redraw the text on each timer tick InvalidateRect(hwnd, &textRect, FALSE); break; case WM_PAINT: { HDC hdc; PAINTSTRUCT ps; hdc = BeginPaint(hwnd, &ps); // Draw the text on the screen DrawText(hdc, text.c_str(), -1, &textRect, DT_LEFT | DT_NOCLIP); EndPaint(hwnd, &ps); } break; case WM_SIZE: // Update the window and text rectangles on window size change GetClientRect(hwnd, &windowRect); textRect.left = windowRect.left + 10; // Adjust the coordinates to your desired position textRect.top = windowRect.top + 10; textRect.right = textRect.left + 100; // Adjust the size according to your needs textRect.bottom = textRect.top + 20; break; case WM_DESTROY: // Cleanup and exit the application KillTimer(hwnd, ID_TIMER); PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; }In this example, the
WM_CREATEmessage initializes the window and text rectangles, and theWM_TIMERmessage triggers the redraw of the text every 10 milliseconds. TheWM_PAINTmessage is responsible for drawing the text on the screen using theDrawTextfunction. TheWM_SIZEmessage updates the rectangles when the window is resized, and theWM_DESTROYmessage cleans up and exits the application.You can tweak the position and size of the text by adjusting the values of the
textRectrectangle according to your needs. Note that in this example, the text is drawn directly on the client area of the window, so it won't be visible if the window is minimized or overlaps with other windows.