当前位置:C++技术网 > 资讯 > GetClassInfo函数的理解及实例应用

GetClassInfo函数的理解及实例应用

更新时间:2016-01-22 21:28:06浏览次数:1+次

我们知道一个窗体建立的过程.首先注册一个wndClass的结构体.然后才是用createWindow函数来建立窗体.
现在有个问题了,我们想知道我们建立的wndClass结构体是否已经注册了,怎么办?
GetClassInfo就是用来解决这个问题的.
先看msdn中的东西.
The GetClassInfo function retrieves information about a window class. 
这个函数返回一些信息关于某个windowClass
Note   The GetClassInfo function has been superseded by the GetClassInfoEx function. You can still useGetClassInfo, however, if you do not need information about the class small icon. 

需要注意的是这个函数已经被GetClassInfoEx 取代了,如果你不需要小图标的信息 当然还是可以继续用这个函数的.

BOOL GetClassInfo(      

    HINSTANCE hInstance,     LPCTSTR lpClassName,     LPWNDCLASS lpWndClass );
 
hInstance
[in] Handle to the instance of the application that created the class. To retrieve information about classes defined by the system (such as buttons or list boxes), set this parameter to NULL.
用第二个参数建立的实例.如果是系统自定义的windowClass那么这个参数为null,delphi中就是0; 
lpClassName
[in] 
Pointer to a null-terminated string containing the class name. The name must be that of a preregistered class or a class registered by a previous call to the RegisterClass or RegisterClassExfunction.

Alternatively, this parameter can be an atom. If so, it must be a class atom created by a previous call to RegisterClass or RegisterClassEx. The atom must be in the low-order word of lpClassName; the high-order word must be zero.
windowClass结构体.

lpWndClass
[out] Pointer to a WNDCLASS structure that receives the information about the class.
返回的结构体的信息会储存在这里.delphi中这个就是一个var 参数.
之前在《单文档应用程序实现修改窗口资源》就提到过,利用getclassinfo来修改默认的窗口类函数,在这里,我们就简单的是实现下,还是基于单文档,过两天研究下基于对话框的如何修改背景
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWndEx::PreCreateWindow(cs) )
		return FALSE;
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式
	WNDCLASS wndclass;
	GetClassInfo(AfxGetInstanceHandle(),cs.lpszClass,&wndclass);
	wndclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH); 
	wndclass.lpszClassName = L"测试"; 
    RegisterClass(&wndclass);  
	//cs.lpszClass = L"测试";
	return TRUE;
}
BOOL CGetClassInfoView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式
	cs.lpszClass=L"测试";
	return CView::PreCreateWindow(cs);
}
在CMainFrame类中修改后,需要在CView中修改下窗口类的类名才能实现出来,具体的原因,我已经解释了