How do you write in to a window?

closed account (E3vDSL3A)
Like once you declare the window prototype and the WinMain and all that other stuff that's like 60 lines long, how exactly do you write/add things to the window itself?

I really want to know how to do that because it's a step closer to game programming! :)
You're right about WM_PAINT. Use TextOut.

guestgulkan, why so rude?
closed account (E3vDSL3A)
But how exactly should I use it and where should I use it?
TextOut() is a WinApi function. Search for it on msdn. I'm usually nicer and explain things in more detail, but just think of it as 'tough love'. Plus I don't understand why folks waste computer cycles on games. Never played one myself. Coding is enough of a game for me.
How to print text to a Win32 Window:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <Windows.h>
#define ErrorMessageBox(a,b) MessageBox(a,b,"Error:",MB_ICONWARNING);

bool SetUpWindowClass (char*, int, int, int);
LRESULT CALLBACK WindowProcedure (HWND, unsigned int, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpsCmdLine, int iCmdShow) {
	if (!SetUpWindowClass ("1", 255, 255, 255)) {
		ErrorMessageBox (NULL, "Window class \"1\" failed");
		return 0;
	}
	HWND hWnd = CreateWindow ("1", "Hello World - Win32 API", WS_OVERLAPPEDWINDOW, 315, 115, 700, 480, NULL, NULL, hInstance, NULL);
	if (!hWnd) {
		ErrorMessageBox (NULL, "Window handle = NULL");
		return 0;
	}
	ShowWindow (hWnd, SW_SHOW);
	MSG uMsg;
	while (GetMessage (&uMsg, NULL, 0, 0) > 0) {
		TranslateMessage (&uMsg);
		DispatchMessage (&uMsg);
	}
	return 0;
}

bool SetUpWindowClass (char *cpTitle, int iR, int iG, int iB) {
	WNDCLASSEX WindowClass;
	WindowClass.cbClsExtra = 0;
	WindowClass.cbWndExtra = 0;
	WindowClass.cbSize = sizeof (WNDCLASSEX);
	WindowClass.style = 0;
	WindowClass.lpszClassName = cpTitle;
	WindowClass.lpszMenuName = NULL;
	WindowClass.lpfnWndProc = WindowProcedure;
	WindowClass.hInstance = GetModuleHandle (NULL);
	WindowClass.hCursor = LoadCursor (NULL, IDC_ARROW);
	WindowClass.hbrBackground = CreateSolidBrush (RGB (iR, iG, iB));
	WindowClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
	WindowClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
	if (RegisterClassEx (&WindowClass)) return true;
	else return false;
}

LRESULT CALLBACK WindowProcedure (HWND hWnd, unsigned int uiMsg, WPARAM wParam, LPARAM lParam) {
	switch (uiMsg) {
		case WM_CLOSE:
			DestroyWindow (hWnd);
			break;
		case WM_DESTROY:
			PostQuitMessage (0);
			break;
		case WM_PAINT: {
				PAINTSTRUCT ps;
				HDC hDC = BeginPaint (hWnd, &ps);
				char *cpaText [] = {
					"Hello World!",
					"This is a hello world application made in the Win32 API",
					"This example was made by some random dude, aka -LeetGamer-"
				};
				int iY = 5;
				for (int iLoopCounter = 0; cpaText [iLoopCounter] != '\0'; iLoopCounter++, iY += 20) {
					TextOut (hDC, 5, iY, cpaText [iLoopCounter], strlen (cpaText [iLoopCounter]));
				}
				EndPaint (hWnd, &ps);
			}
			break;
	}
	return DefWindowProc (hWnd, uiMsg, wParam, lParam);
}


If you have any questions let me know.
Last edited on
kiana wrote:
guestgulkan, why so rude?

Because this person, who is the same as that other person, who is the same as spoonlicker
is an annoying prat.
closed account (E3vDSL3A)
@some random dude

DAMN! THANKS SO MUCH!!!!!! I'VE BEEN DYING TO FIND OUT HOW TO WRITE IN TO A WINDOW AND YOU'RE THE FIRST PERSON TO MAKE IT CODE CLEAR!!!!


:)

I give you full credits whenever I use your guidance to help me write on to a window. :)

Many thanks to you!

The code compiler perfectly and fast, no errors, and I was impressed!

@guestgulkan

Who the hell is spoonlicker?
lol no problem, I remember when I was trying to find out how to do this, annoying because it took a few hours on google to find the answer, and everyone was like "google it", so I was happy to help.

If you have any questions let me know.
closed account (E3vDSL3A)
Okay, I have a question.

How can you remember all the code required to do this?

I mean not that I don't fully understand it(which I don't, not fully)but say even if I do how would I manage to remember every thing in like 60 lines of code?

It's hard to do that for me, and that's why I can't write code in Windows API.

Like for example this part:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
WNDCLASSEX WindowClass;
	WindowClass.cbClsExtra = 0;
	WindowClass.cbWndExtra = 0;
	WindowClass.cbSize = sizeof (WNDCLASSEX);
	WindowClass.style = 0;
	WindowClass.lpszClassName = cpTitle;
	WindowClass.lpszMenuName = NULL;
	WindowClass.lpfnWndProc = WindowProcedure;
	WindowClass.hInstance = GetModuleHandle (NULL);
	WindowClass.hCursor = LoadCursor (NULL, IDC_ARROW);
	WindowClass.hbrBackground = CreateSolidBrush (RGB (iR, iG, iB));
	WindowClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
	WindowClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
	if (RegisterClassEx (&WindowClass)) return true;
	else return false;


^^I couldn't remember that right now if my life depended on it, even knowing 100% of Windows API.^^
Last edited on
If you knew 100% of the Windows API then you would be able to remember the members of the window class structure lol. Anyway I suggest flash cards, or just typing it out again and again and again (that is how I remembered)
Last edited on
Topic archived. No new replies allowed.