I learning how to program win32 applications, and starting with the "Hello World" version found on the MSDN:
http://msdn.microsoft.com/en-us/library/bb384843.aspx
I have some questions based on their code:
1. They have their windows class name as
static TCHAR szWindowClass[] = _T("win32app");
Why would you make it a TCHAR instead of something like:
const char szWindowClass[] = "win32app";
I have also seen:
wchar_t szWindowClass[] = L"win32app";
This prefix L and _T is confusing to me. Why would you use one of these methods over the other? In modern c++ programming which is the 'best' one to use?
2. They have this line of code in the full code:
hInst = hInstance; // Store instance handle in our global variable
I noticed you can comment it out and it does not affect the program. In fact, they do not use hInst after this is written, so what is its purpose?
3. They have this line:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
but I have also seen the same sort of line written as WinProc instead of WndProc. I cant find much information on WinProc. What is the difference?
4. In their callback they have this code:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
TCHAR greeting[] = _T("Hello, World!");
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
TextOut(hdc, 5, 5, greeting, _tcslen(greeting));
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
I am confused as to a few things.
- The program itself just writes "Hello World", but how does it know which "case" to take.
- In this particular program there is no input you can do except close or minimize the window, so as the program is running, what is exactly the default value of "message"?
- Does clicking on the red X mean that case "WM_DESTORY" has been selected?
5. I was playing around with some of the code to change the background color and altered this line:
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
. I changed COLOR_WINDOW + 1 to COLOR_WINDOW + 240. In this case the window does not show and the call "Call to RegisterClassEx failed!" pops up. I ran the code a few times and then changed it back to + 1. When you save it and run it you get an error from VS2013 saying:
"cannot open HelloWorldApp.exe for writing", and when I check task manager I can see a couple of "HelloWorldApp.exe" sitting there. You need to end task them all to make the program compile again. So I guess the code does not properly handle a failed run apart from the message box.
- In this case how would you properly shut the program down and release the memory?
Thank you for your time.