How do I change Win32 C style to C++?

I have tried following a few tutorials but they are outdated and throw errors. How would I change my standard Win32 windows create and wndproc, to OOP / OOD?

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "SuperSaver.h"

// The one and only CLimitSingleInstance object.
CLimitSingleInstance g_SingleInstanceObj(_In_ TEXT("Global\\{9DA0BEED-B4AD-3C0b-B27C-7FD612EA18E9}"));

int WINAPI WinMain(
	_In_ HINSTANCE m_hInstance,
	_In_opt_ HINSTANCE /*hPrevInstance*/,				// Not using hPrevInstance
	_In_ LPSTR /*szCmdLine*/,							// Not using szCmdLine
	_In_ int m_iCmdShow)
{
	static TCHAR m_szAppName[] = TEXT("SuperSaver");
	static TCHAR m_szAppCaption[] = TEXT("SuperSaver");

	if (g_SingleInstanceObj.
		BringPreviousInstanceToForeground(
			_In_	m_szAppName,
			_In_	m_szAppCaption))
	{
		return FALSE;
	}

	HWND
		m_hWndMain;

	MSG
		m_Msg;

	WNDCLASS
		m_WndMain;

	HICON
		m_hIcon;

	m_hIcon = (HICON)LoadImage(
		_In_opt_	m_hInstance,
		_In_		MAKEINTRESOURCE(IDI_SUPERSAVER),
		_In_		IMAGE_ICON,
		_In_		GetSystemMetrics(SM_CXICON),
		_In_		GetSystemMetrics(SM_CYICON),
		_In_		LR_SHARED);

	m_WndMain.style = 0;
	m_WndMain.lpfnWndProc = WndMainProc;
	m_WndMain.cbClsExtra = 0;
	m_WndMain.cbWndExtra = 0;
	m_WndMain.hInstance = m_hInstance;
	m_WndMain.hIcon = m_hIcon;

	m_WndMain.hCursor = LoadCursor(
		_In_opt_	nullptr,
		_In_		IDC_ARROW);

	m_WndMain.hbrBackground = (HBRUSH)GetStockObject(_In_ WHITE_BRUSH);
	m_WndMain.lpszMenuName = MAKEINTRESOURCE(IDR_MENU);
	m_WndMain.lpszClassName = m_szAppName;

	if (!RegisterClass(_In_ &m_WndMain))
	{
		MessageBoxEx(
			_In_opt_	nullptr,										// A handle to the owner window of the message box to be created. If this parameter is NULL, the message box has no owner window. 
			_In_opt_	TEXT("Error: Unable to register SuperSaver"),	// Message to be displayed
			_In_opt_	m_szAppName,									// Dialog caption (Name in title / task-bar)
			_In_		MB_ICONERROR,									// Icon displayed in Message Box
			_In_		0);												// Language (0 is system default)
		return TRUE;
	}

	m_hWndMain = CreateWindowEx(
		_In_		WS_EX_APPWINDOW,									// Windows Extended Styles (WS_EX_***)
		_In_opt_	m_szAppName,										// Window class name (Either predefined system window classes (Buttons, Edit, Static, Scrollbar, etc.) or "The class name can be any name registered with RegisterClass or RegisterClassEx, provided that the module that registers the class is also the module that creates the window.")
		_In_opt_	m_szAppCaption,										// Window caption (Name of title / task-bar)
		_In_		WS_VISIBLE | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CAPTION | WS_THICKFRAME | WS_CLIPCHILDREN,				// Window styles (WS_***)
		_In_		CW_USEDEFAULT,										// Initial x position
		_In_		CW_USEDEFAULT,										// Initial y position
		_In_		CW_USEDEFAULT,										// Initial x size
		_In_		CW_USEDEFAULT,										// Initial y size
		_In_opt_	nullptr,											// Parent window handle
		_In_opt_	nullptr,											// Window menu handle
		_In_opt_	m_hInstance,										// Program instance handle
		_In_opt_	nullptr);											// Creation parameters (NULL if no additional data is needed)

	ShowWindow(
		_In_	m_hWndMain,
		_In_	m_iCmdShow);

	UpdateWindow(_In_ m_hWndMain);

	while (GetMessage(
		_Out_		&m_Msg,
		_In_opt_	nullptr,
		_In_		0,
		_In_		0) > 0)
	{
		TranslateMessage(_In_ &m_Msg);
		DispatchMessage(_In_ &m_Msg);
	}
	return m_Msg.wParam;
}

LRESULT CALLBACK WndMainProc(
	_In_	HWND		m_hWndMain,
	_In_	UINT		m_Message,
	_In_	WPARAM		m_wParam,
	_In_	LPARAM		m_lParam)
{
	switch (_In_ m_Message) {

	case WM_CREATE:
		return 0;

	case WM_DESTROY:
		PostQuitMessage(_In_ 0);
		return 0;

	}

	return DefWindowProc(
		_In_	m_hWndMain,
		_In_	m_Message,
		_In_	m_wParam,
		_In_	m_lParam);
}
Last edited on
closed account (E0p9LyTq)
Jack Hammered wrote:
I have tried following a few tutorials but they are outdated and throw errors

What errors are you getting? What compiler/IDE are you using?

Properly written C/Win32 code should compile fine in most instances. I've run into memory management problems using C memory functions that can cause problems, depending on the compiler used.
Thank you, must have been a copy error somewhere which didn't throw an error.
Topic archived. No new replies allowed.