API to OO |
||||
| PGUI: API to OO | ||||
|
Contents: |
Here we explain how to use object-oriented -programming in creating Windows API -programs. We use an example program that also demonstrates the implementation of class libraries (like MFC) on top of Windows API. API programmingThe central idea in Windows API -programming is the use of window classes for dispatching event messages to windows. Each window class stores a pointer to the window procedure function with signature LRESULT CALLBACK WndProc(HWND hWnd, UINT message, Where the WNDCLASS wc; When the application receives a message it dispatches the message to a window. The window includes a reference to the window class where the window procedure can be called with the message details. All of this gets done in a message loop which resides in the WinMain-function (entry point of the application): int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, See Hello, Win API -example program for further details. Making it simpleTo make Windows API programming simple and OO we aim to hide all of the detaile
about window classes. Thus we also hide the registration of window classes and
actually the whole main program ( class TestWindow: public Window
{
protected:
void OnPaint(HDC);
};
class TestApplication: public Application
{
protected:
BOOL InitApplication();
};
// the application object must be created somewhere
TestApplication testApp;
void TestWindow::OnPaint(HDC hdc)
{
::TextOut(hdc, 0, 0, "Testing...", 10);
}
// create the main window in here
BOOL TestApplication::InitApplication()
{
mainWindow = new TestWindow();
mainWindow->Create("API to OO");
return true;
}
There are two problems in making the above possible in API programming:
Window procedureThe event messages are sent to the window procedure. The window procedure has
the signature shown above. The window procedure cannot be a member function of
the We can, however, implement a generic window procedure that calls the member function of our window object. We need to store a pointer to the window object into the Win32 window structure with Window *wPtr; This can be called in the generic window procedure during the creation of the window. After that we can get the pointer by calling wPtr = (Window*) ::GetWindowLong(hWnd, 0); Now we can call a method that is almost like the window procedure: wPtr->WndProc(message, wParam, lParam); This method can be a protected member function of the base window class. It
can call more detailed message processing functions like WinMainHiding public: This application object is used in the int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInst,
LPSTR lpCmdLine, int nCmdShow)
{
if (!Application::theApp) return 0;
Application::theApp->InitApplication(hInst, lpCmdLine, nCmdShow);
return Application::theApp->RunApplication();
}
The reference to the application object gets created when one application object is constructed: Application::Application()
{
theApp = this;
mainWindow = NULL;
}
The application program can create an application object by declaring it as a global variable. See above. Example programHere is the complete example program called "APItoOO":
|
See also: |
||
| Jarkko Leponiemi, Monday, 15-Sep-2003 07:31:54 EEST | ||||