First Window, constant loop?

Hi, this is my first attempt at opening a window using Windows API, I copied it out of my book, but it's hand typed, so there's probably something I missed and I just can't find it.
My problem is that the window isn't being opened/painted on my screen, but my debugger keeps running like it's caught in a loop (the loop part is what this should do until the exit button is pressed). Can someone please take a look at this and tell me where I went wrong?

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
#include <windows.h>
#include <iostream>
using namespace std;

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int CmdShow)
{
	WNDCLASSEX WindowClass;
	static LPCTSTR szAppName = L"OFWin";
	HWND hWnd;
	MSG msg;
	WindowClass.cbSize = sizeof(WNDCLASSEX);

	WindowClass.style = CS_HREDRAW | CS_VREDRAW;
	WindowClass.lpfnWndProc=WindowProc;
	WindowClass.cbClsExtra = 0;
	WindowClass.cbWndExtra = 0;
	WindowClass.hInstance = hInstance;
	WindowClass.hIcon = LoadIcon(0, IDI_APPLICATION);
	WindowClass.hCursor = LoadCursor(0, IDC_ARROW);
	WindowClass.hbrBackground = static_cast<HBRUSH>(GetStockObject(GRAY_BRUSH));
	WindowClass.lpszMenuName = 0;
	WindowClass.lpszClassName = szAppName;
	WindowClass.hIcon = 0;
	RegisterClassEx(&WindowClass);

	//Now We Create the Window.
	hWnd = CreateWindow(szAppName, L"A BASIC WINDOW The Hard Way", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 
		CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,0,0,hInstance, 0);
	ShowWindow(hWnd, CmdShow);
	UpdateWindow(hWnd);

	//Message Loop
	while(GetMessage(&msg,0,0,0)==TRUE)
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return static_cast<int>(msg.wParam);
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC hDC;
	PAINTSTRUCT PainSt;
	RECT aRect;
	switch(message)
	{
	case WM_PAINT:
		hDC=BeginPaint(hWnd, &PainSt);
		GetClientRect(hWnd, &aRect);
		SetBkMode(hDC, TRANSPARENT);

		DrawText(hDC, L" HELLO WORLD!", -1, &aRect, DT_SINGLELINE | DT_CENTER|DT_VCENTER);
		EndPaint(hWnd, &PainSt);	
		return 0;

	case WM_DESTROY:

		PostQuitMessage(0);
		return 0;

	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
}

I tried placing cout<<"got this far"; on the first line under my winmain() but no console opened, does this mean that the program didn't even open winmain?
Last edited on
Windows GUI applications does not work that way. To get a console you need to use AllocConsole() first and only then you can use iostreams:
1
2
3
4
5
AllocConsole();
freopen("CONIN$", "r",stdin);
freopen("CONOUT$", "w",stdout);
freopen("CONOUT$", "w",stderr);
std::cout << "blarg" << std::endl;


Before exiting don't forget to call FreeConsole() API.
Thanks Modoran, I'll have to remember that.
I just want to post this link here in case anyone reads this post and wants an example for a working WINAPI program with "Hello world" as a starting template. The tutorial supplied there was just a bit confusing and didn't explain things well, but the final example does work correctly with visual C++ 2012.
http://msdn.microsoft.com/en-us/library/vstudio/bb384843%28v=vs.100%29

Using that and my book Horton's "Beginning Visual C++ 2010" at the same time helped out.
Last edited on
Topic archived. No new replies allowed.