Simple window basics

Although this is a beginner question, I didn't know whether to put it here in the beginner forum, so i just put it here. So I just made a simple window using an online tutorial. I have a few questions about the code.
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
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wc;
	HWND hwnd;
	MSG msg;

	wc.cbSize        = sizeof(WNDCLASSEX);
	wc.style         = 0;
	wc.lpfnWndProc   = WndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = hInstance;
	wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = g_szClassName;
	wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }


In this section of code, the tutorials say that you have to add the bit about RegisterClassEx to register it. First of all why do you have to register this class and second of all isn't this checking if it hasn't been registered? Shouldn't there be some code that actually registers it? I'll post the rest of my questions when I'm sure I can't figure them out...
Yes and yes.

Every window displayed must have a "class". The word "class" is a pretty generic name (hence much confusion about it), but what it really does is define a structure that is used as a template to create windows.

In other words, in order to create a window, Windows needs to know certain things about that window: its owner, its default color, icon, cursor, window procedure, etc.

It is called a "class" because just as in C++, one or more instances of a window may be created from the same class (type/definition/etc).

RegisterClassEx() tells the OS about your window class. It complains if there was a problem doing that. (If there was, there is something catastrophically wrong with the system, and you would need to reboot anyway...)

Hope this helps.
So is it registered automatically? and the code that I pasted about !RegisterClassEx is only used if theres a problem?
No and yes (kind of). RegisterClassEx() is what does the registering. The function result just tells you whether or not it succeeded. So you check the result in case something horrible happened.
Last edited on
Ok, see my C++ isn't amazing yet so I don't see when RegisterClassEx is called...

"if (!RegisterClassEx(&wc))" I can see that that checks if the function failed, but does it call it too? Sorry if thats a nooby question...
Oh, I understand your confusion.

Yes, every time you have functionname( ... ) you are calling the function. Since a function returns a value, you can place a function call in an expression, like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

// Here is my function. It returns a value of type 'int'.
int add_two( int a, int b )
  {
  return a + b;
  }

int main()
  {
  // Here is my expression. I am multiplying an int with an int.
  int c = add_two( 3, 4 ) * 6;
  // The first int is obtained by calling a function. (It is the function's result. In this case, the value 7.)
  // The second int is a literal constant -- the value 6.
  // I place the product (of my multiplication expression) in the int variable named 'c'. (That is, 'c = 42'.)
  }


It might be worth your time to read through the C++ tutorials on this site. You can find them from the upper-right box on the main page. I'm sure a quick read will get you up to speed.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.