WIN32 Window , How to make it easy to implent?

Hello forum, I m new to programming in general but I stumbled upon a tutorial on how to create an actual WIN32-window instead of the usual console. I was depressed by the complexity of the code and I wonder if there is any easier way to create a window like that or a way to make a reusable object out of the code?

Ill post the code here :
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
#include <Windows.h>;

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE,PWSTR nCmdLine, int nCmdShow)
{
	const LPCSTR CLASS_NAME = "WindowClass";
	WNDCLASS wc = { };
	wc.lpfnWndProc = WindowProc;
	wc.lpszClassName = CLASS_NAME; 
	wc.hInstance = hInstance;
	RegisterClass(&wc);
	HWND hwnd = CreateWindowEx(
		0,
		CLASS_NAME,
		"My First Window",
		WS_OVERLAPPEDWINDOW,
		500,
		500,
		200,
		200,
		NULL,
		NULL,
		hInstance,
		NULL);

	if(hwnd==0) { 
		return 0;
	}
	
	ShowWindow(hwnd,nCmdShow);
	nCmdShow=1;
	MSG msg = { };
	while(GetMessage(&msg,NULL,0,0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	switch(uMsg) {
	case WM_DESTROY: {}PostQuitMessage(0); return 0;
	case WM_PAINT:
		{
			PAINTSTRUCT ps;
			HDC hdc = BeginPaint(hwnd,&ps);
			FillRect(hdc,&ps.rcPaint,(HBRUSH)(COLOR_WINDOW+5));
			EndPaint(hwnd,&ps);
		}return 0;
	case WM_CLOSE:
		{
			if(MessageBox(hwnd,"Close Window?","Close",MB_OKCANCEL)==IDOK) {
				DestroyWindow(hwnd);
			}
		}return 0;
	}
	return DefWindowProc(hwnd,uMsg,wParam,lParam);
}


EDIT : This is not pure C++ code, thanks for pointing that fact out
Last edited on
I wonder if there is any easier way to create a window like that or a way to make a reusable object out of the code?

Well, I don't think it's making things easier overall, but MattJ812 is implementing a reusable base class in this recent (current!) thread.

Visual Studio 11 on the command line
http://www.cplusplus.com/forum/windows/107437/

A bit like the one here:

The new scratch program
http://blogs.msdn.com/b/oldnewthing/archive/2005/04/22/410773.aspx

Andy
Last edited on
closed account (Dy7SLyTq)
its easy to make a class for generating win32 windows. i made one kind of like it in sdl, and theres a win32 tutorial that does it
Last edited on
You can use a multimedia library like SDL or SFML. Either way I think using the Win32 API for a window is good because it gives you more software design options.
But it isn't at all complicated! Yeah there are a lot of dumb API functions to call and structs to fill out but you can look all that stuff up!

I hope you realise you aren't expected to remember specifics of the APIs you use, just a basic idea of what you need to get the job done or where to get the right functions.

Learning an API like 'win32' isn't memorizing it, it's getting used to searching in the right places for documentation.

MSDN is a great reference, and in each section it has 'about' and 'using' pages that explain what the purpose and basic usage of each part of the API is. Obviously a tutorial or basic guide to get you started with win32 couldn't hurt at first, otherwise their library could be daunting.

Hopefully I'm teaching you to suck eggs.
Alas, unless you are using some fancy widget toolbox to do this stuff for you in only a line or two, it doesn't get any simpler. Windows API is actually one of the easier APIs out there.

Usually googling "msdn functionname" works pretty well when you know the name of the function you want to reference. Otherwise "windows api create a window", etc.

Enjoy!
Topic archived. No new replies allowed.