I would like to ask why, in developing Windows GUI's using the API, is it necessary to register a window class? What is the concept of it?
I have already read the first 3 chapters of Programming Windows by Charles Petzold, but I still wonder what's the purpose of explicitly registering a class. Why would I want to do it explicitly? Why isn't it done in the background for instance in the CreateWindow() (or CreateWindowEx()) function? I mean, why isn't the code that RegisterClass() executes inside CreateWindow(), or why doesn't CreateWindow() call the RegisterClass() itself?
I have also been reading the documentation on MSDN and I know that the RegisterClass() function associates a window procedure with a window class, by filling a WNDCLASS structure. I know that this is the function that handles the messages from the OS, however why is it necessary to register that function (the WinProc one) to a class inside a separate function from CreateWindow()?
I can understand the reasons to exist the CreateWindow() function, and why it doesn't automatically shows the window created. This implies I also understand the purpose of the ShowWindow() function.
I'm sure that there must be good reasons for this behavior, to let the programmer register a class when he wants, I'm just failing to see those reasons, and that's why I am asking you guys to shed light on the subject.
Please keep in mind that I am very new to GUI development with the Windows API. I have done some GUI's in MATLAB, which being different from the Windows API, still allowed me to understand some of the Windows philosophy, specifically the purpose of callback functions. I don't know if this info is useful, but if you need to make some analogies please be my guest.
Since you've tagged your question with C++ I'll give you a C++ analogy...
RegisterClassis basically you defining a class and including it in your program (much like a#includein C++). TheWNDPROCis your handler for anything that happens within the window if and when an instance is created.CreateWindowis conceptually the same as you doing anewin C++. You're asking Windows to create a new window, and you've got to tell it the type of window. Windows includes a set of predefined windows, such as Button or Edit, but if you want to create an instance of your own window then that's fine, you just need to tell it the "class" you'd like to create. You've already registered this class by callingRegisterClass, so Windows can now go straight to the definition and create an instance of your window.