GetClassInfo函数解析

我们知道一个窗体建立的过程.首先注册一个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 参数.

返回值 boolean;

例子会在综合后面几个API后一起给出来.

BOOL CMyTest::RegisterWindowClass(HINSTANCE hInstance)   
{   
      LPCSTR className = "CMyWin1";//"CMyWin"控件?的名字   
       WNDCLASS windowclass;         
    
       if(hInstance)   
              hInstance = AfxGetInstanceHandle();   
          
       if (!(::GetClassInfo(hInstance, className, &windowclass)))   
       {                
              windowclass.style = CS_DBLCLKS;   
              windowclass.lpfnWndProc = ::DefWindowProc;   
              windowclass.cbClsExtra = windowclass.cbWndExtra = 0;   
              windowclass.hInstance = hInstance;   
              windowclass.hIcon = NULL;   
              windowclass.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);   
              windowclass.hbrBackground= (HBRUSH)(COLOR_BTNFACE+1);
              windowclass.lpszMenuName = NULL;   
              windowclass.lpszClassName = className;   
    
              if (!AfxRegisterClass(&windowclass))   
              {   
                     AfxThrowResourceException();   
                     return FALSE;   
              }   
       }   
    
       return TRUE;   
} 

注册窗口:

CMyTest::CMyTest()
{
	RegisterWindowClass();   
}

  

posted @ 2014-01-14 17:26  陳さん様  阅读(2522)  评论(0编辑  收藏  举报