How to encapsulate a WinAPI application into a C++ class
- by Semen Semenych
There is a simple WinAPI application. All it does currently is this:
register a window class
register a tray icon with a menu
create a value in the registry in order to autostart
and finally, it checks if it's unique using a mutex
As I'm used to writing code mainly in C++, and no MFC is allowed, I'm forced to encapsulate this into C++ classes somehow. So far I've come up with such a design:
there is a class that represents the application
it keeps all the wndclass, hinstance, etc variables, where the hinstance is passed as a constructor parameter as well as the icmdshow and others (see WinMain prototype)
it has functions for registering the window class, tray icon, reigstry information
it encapsulates the message loop in a function
In WinMain, the following is done:
Application app(hInstance, szCmdLIne, iCmdShow);
return app.exec();
and the constructor does the following:
registerClass();
registerTray();
registerAutostart();
So far so good. Now the question is : how do I create the window procedure (must be static, as it's a c-style pointer to a function) AND keep track of what the application object is, that is, keep a pointer to an Application around.
The main question is : is this how it's usually done? Am I complicating things too much? Is it fine to pass hInstance as a parameter to the Application constructor? And where's the WndProc?
Maybe WndProc should be outside of class and the Application pointer be global? Then WndProc invokes Application methods in response to various events.