Chapter 3.GDI/DirectDraw Internal Data Structures
說明,在這里決定跳過第二章,實在是因為里面涉及的內容太理論,對我而言又太艱深
3.1 HANDLES AND OBJECT-ORIRNTED PROGRAMMING
In normal object-oriented programming practice,information hiding is achieved by declaring certain members as private or protected,so the client side code can't access them directly.But the compiler still needs to know perfectly well all members,their types,names,and orders in a class.At least,the compiler needs to know the exact size of an instance of an object for memory allocation.This can cause lots of problems for the modular development of programs,Every time a data member or member function is changed,the whole program needs to be recompiled.Programs complied with older versions of class definition would not work with newer version.To solve this problem,there is the abstract bass class.The abstract bass class,which uses virtual functions to define the interface the clien-side program can see while completely hiding away the implementation,improves information hiding and the modularity of programs even further...For hiding the implementation away from the client side of a class, normally a special function is provided to create an instance of a derived class,including memory allocation; another special function is provided to destroy an instance,including freeing its memory.
Objects in the Win32 API can be seen as being implemented using abstract base class with no data members. The data representation of an object is completely hidden from the user application...the perfect information hiding provided by the Win32 API greatly improves the portability of programs. GDI normally provides several functions to create an instance of an object and several functions to destroy them.
To illustrate our comparison between object-roiented programming and the Win32 API,let's try to provide some minimum pseudo-implementation of GDI using C++.
//gdi.h #include<windows.h> class _GdiObj { public:virtual int GetObjectType(void) = 0;virtual int GetObject(int cbBuffer, void * pBuffer) =0;virtual bool DeleteObject(void) = 0;virtual bool UnrealizeObject(void) = 0; };class _Pen:public _GdiObj { public:virtual int GetObjectType(void){return OBJ_PEN;}virtual int GetObject(int cbBuffer,void *pBuff)=0;virtual bool DeleteObject(void)=0;virtual bool UnrealizeObject(void){return true;} };_Pen * _CreatePen(int fnPenStyle, int nWidth, COLORREF crColor); //gdi.cpp#define STRICT #define WIN32_LEAN_AND_MEAN #include <windows.h> #include "gdi.h" class _RealPen : public _Pen {LOGPEN m_LogPen; public:_RealPen(int fnPenStyle, int nWidth, COLORREF crColor){m_LogPen.lopnStyle = fnPenStyle;m_LogPen.lopnWidth.x = nWidth;m_LogPen.lopnWidth.y = 0;m_LogPen.lopnColor = crColor;}int GetObject(int cbBuffer, void * pBuffer){if ( pBuffer==NULL )return sizeof(LOGPEN);else if ( cbBuffer>=sizeof(m_LogPen) ){memcpy(pBuffer, & m_LogPen, sizeof(m_LogPen));return sizeof(LOGPEN);}else{SetLastError(ERROR_INVALID_PARAMETER);return 0;}}bool DeleteObject(void){if ( this ){delete this;return true;}elsereturn false;} }; _Pen * _CreatePen(int fnPenStyle, int nWidth, COLORREF crColor) {return new _RealPen(fnPenStyle, nWidth, crColor); } //test.cpp #include "gdi.h"void Test(void) {_Pen * pPen = _CreatePen(PS_SOLID, 1, RGB(0, 0, 0xFF));////pPen->DeleteObject(); } int WINAPI WinMain(HINSTANCE hInsatcne,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd) {return 0; }這個程序定義_GdiObj這樣一個抽象基類,緊接著派生出_Pen類,同為抽象基類,在_Pen類的子類_RealPen中才將純虛函數一一實現。另外從這個程序中也可以看出,創建畫筆對象調用的函數,其實是填充一個LOGPEN結構類型數據的幾個字段
轉載于:https://www.cnblogs.com/lanf/p/5094385.html
總結
以上是生活随笔為你收集整理的Chapter 3.GDI/DirectDraw Internal Data Structures的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 变量传值
- 下一篇: 全球知名大学课件下载地址汇总