Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it necessary to RegisterClass in Windows API programming?

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.

like image 915
Scout Avatar asked Jul 02 '14 14:07

Scout


People also ask

Why is it advisable to use the app window API?

Using the Windows API, you can develop applications that run successfully on all versions of Windows while taking advantage of the features and capabilities unique to each version.

What is the function of Windows API?

The Windows API (application programming interface) allows user-written programs to interact with Windows, for example to display things on screen and get input from mouse and keyboard. All Windows programs except console programs must interact with the Windows API regardless of the language.

How to understand Windows API?

The Windows API: What Is It? Simply, the Windows API is a set of standard libraries developers can use to interact with the Windows Operating System. These libraries are made up of functions, which are exposed (or exported) in various dynamic-link libraries (DLLs) throughout the Windows Operating System.

Which function we use to register Windows classes in window?

Your application can register a window class by using either RegisterClassA or RegisterClassW. New applications should typically use RegisterClassW.


1 Answers

Since you've tagged your question with C++ I'll give you a C++ analogy...

RegisterClass is basically you defining a class and including it in your program (much like a #include in C++). The WNDPROC is your handler for anything that happens within the window if and when an instance is created.

CreateWindow is conceptually the same as you doing a new in 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 calling RegisterClass, so Windows can now go straight to the definition and create an instance of your window.

like image 55
Sean Avatar answered Nov 14 '22 22:11

Sean