In the default code that Visual Studio generates when creating a Win32 project, there's a function called MyRegisterClass with the comment:
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
...
return RegisterClassEx(&wcex);
}
But this function uses RegisterClassEx that is not exists prior Windows 95. I really don't understand the comment. What they are mean?
What the main purpose to have MyRegisterClass function, and why it's so necessary for compatibility with earlier versions of Windows?
The
MyRegisterClassfunction is a helper function. It encapsulates the call to the API functionRegisterClass[Ex], since there's a lot of messy parameter setup that needs to be done in order to make the call. The complete function definition looks like this:Extracting this out into a helper function—as opposed to inlining it—keeps your code clean and more readable. As far as performance goes, the compiler will inline it automatically, if that's appropriate.
Another reason why it makes sense for this to be a helper function is historical, and that is what's described in the comments:
You see, the
Exsuffix on an API denotes "EXtended". TheRegisterClassExfunction was introduced with 32-bit Windows (API v4.0, Windows 95). Back in 16-bit Windows, there was onlyRegisterClass. 32-bit Windows supported both variants, for backwards-compatibility, but the "extension" thatRegisterClassExadded was the ability to set a small icon. If you want to have nice small icons (instead of ugly ones, generated by a low-quality stretching algorithm from the large icon), you need to callRegisterClassEx. But if you want to target 16-bit Windows, you can't callRegisterClassEx; you need to stick withRegisterClass. So, if you encapsulate the call toRegisterClass[Ex]in a helper function, you can easily modify the code, changing which API function it calls and therefore which version(s) of Windows your app can target.The comment is trying to convey this information, but doing a relatively poor job of expressing itself.
You can completely ignore this today. Nobody targets 16-bit Windows anymore. Always call
RegisterClassEx, and forget thatRegisterClasseven exists. That doesn't eliminate the advantage of encapsulating this code in a function for readability.Note, further, that the default Win32 project template bundled with Visual Studio separates the small and large icons in separate ICO files and thus assigns them separate resource IDs. This was presumably done for the same historical reasons, so you could compile for 16-bit Windows without having the small icon getting in the way, but again is completely unnecessary today. You can combine both icons into a single ICO file, and load them selectively: