MFC中小笔记(四)

时间:2023-03-09 19:54:44
MFC中小笔记(四)

12、编译透明化界面是出现  WS_EX_LAYERED  AC_SRC_ALPHA ULW_ALPHA ULW_OPAQUE  undeclared identifier ,搜索发现SDK版本过低。

MFC中小笔记(四)

方法1:搜索SDK,下载合适的SDK(sdk地址)。但是高版本的SDK开发的程序,在低版本上运行时就得需要相当版本的运行库(如果在VS2008上开发的程序,调用了只有.net 3.5之后才有的函数库或者引用头文件,在XP没有装.net 3.5的系统上,是无法正常打开的)。

方法2:自己定义其中的宏,在开发过程中是没关系的。

/*定义高版本的SDK中的宏*/

#define WS_EX_LAYERED 0x00080000
#define AC_SRC_ALPHA 0x01
#define ULW_ALPHA 0x00000002
#define ULW_COLORKEY 0x00000001

13、UpdateLayeredWindow  : undeclared identifier

原因:SDK版本过低。如果在低版本中调用API的UpdateLayeredWindow  ,通过调用动态链接库。 官方解释 连接

/* 通过动态链接库调用 UpdateLayerWindow */

    HINSTANCE hInst = LoadLibrary("User32.DLL");
if(hInst)
{
typedef BOOL(WINAPI *UpdateLayeredWindow)(HWND,HDC,POINT *,SIZE *,HDC,POINT *,COLORREF,BLENDFUNCTION *,DWORD);
UpdateLayeredWindow myfunc= NULL; //取得UpdateLayeredWindow函数指针
myfunc=(UpdateLayeredWindow)GetProcAddress(hInst, "UpdateLayeredWindow"); if(myfunc)
myfunc(m_hWnd, pDC->m_hDC, &point, &size, MemDC, &pointSrc, , &blend, ULW_ALPHA : ULW_COLORKEY);
FreeLibrary(hInst);
}

关于最后一个参数的解释说明:

/* dwFlags  的解释说明 */
dwFlags [in]
Type: DWORD
This parameter can be one of the following values.
Value Meaning
//----------------------------------------------------------------
ULW_ALPHA Use pblend as the blend function. If the display mode is
0x00000002 colors or less, the effect of this value is the same as the effect of
ULW_OPAQUE.
//----------------------------------------------------------------
ULW_COLORKEY Use crKey as the transparency color.
0x00000001 //----------------------------------------------------------------
ULW_OPAQUE Draw an opaque layered window.
0x0000000 If hdcSrc is NULL, dwFlags should be zero.

14、在编写含有皮肤、图片的程序,尽量减少将资源导入到 Resource.h 中,那样会增大程序的体积。最好的方式是采用 动态加载的方式。

15、AlphaBlend  is not a member of CDC  采用系统的API 进行替换。

16、ON_WM_NCHITTEST    消息的返回值,在不同的SDK下不同。

错误再现:error C2440: 'type cast' : cannot convert

      from  'long (__thiscall CMyWnd::*)(class CPoint)'

      to  'unsigned int (__thiscall CWnd::*)(class CPoint)'
    Pointers to members have different representations; cannot cast between them

afx_msg UINT OnNcHitTest(CPoint point);  //VC6.0

afx_msg LRESULT OnNcHitTest(CPoint point);//VS2008

17、错误再现

MyBase.obj : error LNK2001: unresolved external symbol "public: virtual struct CRuntimeClass * __thiscall CMyBase::GetRuntimeClass(void)const " (?GetRuntimeClass@CDlgBase@@UBEPAUCRuntimeClass@@XZ)
MyDlg.obj : error LNK2001: unresolved external symbol "public: virtual struct CRuntimeClass * __thiscall CMyBase::GetRuntimeClass(void)const " (?GetRuntimeClass@CDlgBase@@UBEPAUCRuntimeClass@@XZ)

原因:在类定义中,DECLARE_DYNAMIC(CMyBase)声明了这样一个定义宏,旨在确定运行时对象属于哪一个类而定义的宏。

DEClARE_DYNCREATE/IMPLEMENT_DYNCREATE 是为了“动态创建"类的实例而定义的宏。new可以用来创建对象,但不是动态的。

/*下面的做法是通不过的:*/
char szClassName[];
cin >> szClassName;
CObject* pOb=new szClassName; //通不过 这里就要用到DEClARE_DYNCREATE/IMPLEMENT_DYNCREATE定义的功能了。

但是在使用过程中,如果不进行解释说明 IMPLEMENT_DYNAMIC(CMyBase, CBase) 会出现GetRuntimeClass 错误。出现此错误,可以查看 DECLARE_DYNAMIC 定义。\

MSDN解释(点击 连接)。

/*  DECLARE_DYNAMIC 定义 */

#define _DECLARE_DYNAMIC(class_name) \
public: \
static AFX_DATA CRuntimeClass class##class_name; \
virtual CRuntimeClass* GetRuntimeClass() const; \ #endif // not serializable, but dynamically constructable
#define DECLARE_DYNCREATE(class_name) \
DECLARE_DYNAMIC(class_name) \
static CObject* PASCAL CreateObject();

DECLARE_DYNAMIC 定义