WIN32 - Update Text in a Window in order to show its size in Pixels and coordinates

Joined
Oct 4, 2023
Messages
1
Reaction score
0
Hi fellows,

I am trying to write a code that creates a window with the following text:

A13.2 - EDV2 - MB3B - WS23/24
Fenstergröße:
WindowPos rechts/unten:
kein Mausklick
Tastendruck:


Next to Fenstergröße it should show the size of my windows in Pixels as "609x383 Pixels"
and next to WindowPos rechts/unten for example "545x296 Pixel".

I am trying to upade the previously declared text static char* szText[100] usind WM_MOVE and wsprintf but it's not working. Visual studios say the lines with wsprintf, namely lines 125 and 126 are wrond.

Would anyone be kind enough to help me? Here is the code:


#include <windows.h>

int windowWidth = 0, windowHeight = 0;
int upperLeftX = 0, upperLeftY = 0;
int lowerRightX = 0, lowerRightY = 0;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
char szApp[] = "Textout";
HWND hwnd;
MSG msg;
WNDCLASS wndclass;

wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szApp;

if (!RegisterClass(&wndclass))
{
MessageBox(NULL, "Fehler – Programmabbruch!", szApp, MB_ICONERROR);
return 0;
}

hwnd = CreateWindow(
szApp, TEXT(" Vinicius Passos, 769 278 "),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT, // Set initial width to default
CW_USEDEFAULT, // Set initial height to default
NULL,
NULL,
hInstance,
NULL);

if (hwnd == NULL)
{
MessageBox(NULL, "Fehler beim Erstellen des Fensters!", szApp, MB_ICONERROR);
return 0;
}

// Now that the window has been created, you can call GetWindowRect and GetClientRect.
RECT wRect, cRect;
GetWindowRect(hwnd, &wRect); // Get the window's dimensions relative to the display
GetClientRect(hwnd, &cRect); // Get the client area's dimensions relative to the window

windowWidth = wRect.right - wRect.left;
windowHeight = wRect.bottom - wRect.top;

int clientWidth = cRect.right - cRect.left;
int clientHeight = cRect.bottom - cRect.top;

ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);

while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

HDC hdc;
PAINTSTRUCT ps;
TEXTMETRIC tm;
RECT rect;
static HFONT hfont;
static int cxChar, cyChar;
static char* szText[100] = { TEXT("A13.2 - EDV2 - MB3B - WS23/24"),
TEXT("Fenstergröße:"),
TEXT("WindowPos rechts/unten:"),
TEXT("kein Mausklick"),
TEXT("Tastendruck: ") };

static int deltaX, deltaY;

switch (message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
hfont = GetStockObject(ANSI_FIXED_FONT);
GetTextMetrics(hdc, &tm);
cyChar = tm.tmHeight + tm.tmExternalLeading;
cxChar = tm.tmAveCharWidth;

ReleaseDC(hwnd, hdc);
return 0;

case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
SelectObject(hdc, hfont);
GetClientRect(hwnd, &rect);
SetTextAlign(hdc, TA_CENTER | TA_BASELINE);
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc, rect.right / 2, rect.bottom / 2 - cyChar, szText[0], strlen(szText[0]));
TextOut(hdc, rect.right / 2, rect.bottom / 2, szText[1], strlen(szText[1]));
TextOut(hdc, rect.right / 2, rect.bottom / 2 + cyChar, szText[2], strlen(szText[2]));
TextOut(hdc, rect.right / 2, rect.bottom / 2 + 2 * cyChar, szText[3], strlen(szText[3]));
TextOut(hdc, rect.right / 2, rect.bottom / 2 + 3 * cyChar, szText[4], strlen(szText[4]));

EndPaint(hwnd, &ps);
return 0;

case WM_MOVE:
// Update the positions when the window is moved
upperLeftX = LOWORD(lParam);
upperLeftY = HIWORD(lParam);
lowerRightX = upperLeftX + windowWidth; // ?
lowerRightY = upperLeftY + windowHeight; // ?

// Update the text with the new positions
wsprintf(szText[1], "Fenstergröße: %d x %d Pixel", windowWidth, windowHeight);
wsprintf(szText[2], "WindowPos rechts/unten: %d x %d Pixel", lowerRightX, lowerRightY);

// Redraw the window
InvalidateRect(hwnd, NULL, TRUE);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}



Thank you all and kind regards,

Vini
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top