在上一节中我们有测试过使用GetClassInfoEx读取不到窗体类信息,我们这一节将学习通过GetClassLongPtr获取非窗体所在的进程类信息
(一) 函数声明
ULONG_PTR GetClassLongPtr(HWND hWnd, int nIndex );
获取指定窗体(可进程也可进程外)特定类信息项值(数据源:窗体类分配的WNDCLASSEX参数值),可支持32、64位的OS
DWORD GetClassLong(HWND hWnd, int nIndex);
功能同GetClassLongPtr, 仅支持32位的OS
nIndex 值参考:
GCW_ATOM Retrieves an ATOM value that uniquely identifies the window class. This is the same atom that the RegisterClassEx function returns. GCL_CBCLSEXTRARetrieves the size, in bytes, of the extra memory associated with the class. GCL_CBWNDEXTRARetrieves the size, in bytes, of the extra window memory associated with each window in the class. For information on how to access this memory, see GetWindowLongPtr. GCLP_HBRBACKGROUNDRetrieves a handle to the background brush associated with the class. GCLP_HCURSORRetrieves a handle to the cursor associated with the class. GCLP_HICONRetrieves a handle to the icon associated with the class. GCLP_HICONSMRetrieves a handle to the small icon associated with the class. GCLP_HMODULERetrieves a handle to the module that registered the class. GCLP_MENUNAMERetrieves the pointer to the menu name string. The string identifies the menu resource associated with the class. GCL_STYLERetrieves the window-class style bits. GCLP_WNDPROCRetrieves the address of the window procedure, or a handle representing the address of the window procedure.You must use the CallWindowProc function to call the window procedure.Code: 以下代码演示
1. 获取NotePad窗体句柄
2. 获取NotePad 类信息通过(GetClassLongPtr)
HWND hWndNotePad = FindWindowEx ( NULL , NULL , _T ( "Notepad" ), NULL );
if ( NULL != hWndNotePad && IsWindow ( hWndNotePad ))
{
int nIndexList [] = { GCW_ATOM , GCL_CBCLSEXTRA , GCL_CBWNDEXTRA , GCLP_HBRBACKGROUND , GCLP_HCURSOR ,
GCLP_HICON , GCLP_HICONSM , GCLP_HMODULE , GCLP_MENUNAME , GCL_STYLE , GCLP_WNDPROC };
int * nValueList = new int [ sizeof ( nIndexList ) / sizeof (* nIndexList )];
const TCHAR * szInfo [] = { _T ( "Atom:\t\t 0x%06X\n" ),
_T ( "class Extra:\t 0x%06X\n" ),
_T ( "Wnd Extra: 0x%06X\n" ),
_T ( "hBackground: 0x%06X\n" ),
_T ( "hCursor:\t 0x%06X\n" ),
_T ( "hIcon: 0x%06X\n" ),
_T ( "hIconSM: 0x%06X\n" ),
_T ( "hModule: 0x%06X\n" ),
_T ( "MenuName: 0x%06X\n" ),
_T ( "Style: 0x%06X\n" ),
_T ( "WndProc: 0x%06X\n" )};
assert (( sizeof ( nIndexList ) / sizeof (* nIndexList )) == ( sizeof ( szInfo ) / sizeof (* szInfo )));
TCHAR szClassInfo [1024] = {0};
TCHAR szTemp [256];
for ( int ii = 0; ii < sizeof ( nIndexList ) / sizeof (* nIndexList ); ii ++)
{
nValueList [ ii ] = GetClassLongPtr ( hWndNotePad , nIndexList [ ii ]);
TCHAR szTemp [256];
_stprintf_s ( szTemp , szInfo [ ii ], nValueList [ ii ]);
_tcscat ( szClassInfo , szTemp );
}
SetWindowText ( GetDlgItem ( hWnd , ID_LABINFO ), szClassInfo );
OutputDebugString ( szClassInfo );
delete [] nValueList ;
}
我们读取后的值与SPY++ 中类信息对比
疑问: 1. MenuName 不一致;
2. 根据hCursor句柄值如何得到对应的资源ID == IDC_IBEAM
3. 根据hBackground句柄值如何得到对应的资源ID == COLOR_WINDOW
(二) 特别说明
此函数返回的数据没有WNDCLASSEX项多
对于以上疑问需努力学习!