作为一个窗体,除了具有窗体类信息外还有其自身的一些信息如风格,扩展风格,窗体处理函数外还包括一个用户信息, 我们可以通过信息空间将我们要公共信息置入其中实现信息共享. 接下来我们看看它的功能.
(一) 函数声明
LONG_PTR GetWindowLongPtr(HWND hWnd, int nIndex );
获取指定窗体特定标识信息, 此函数支持32位和64位
LONG GetWindowLong(HWND hWnd, int nIndex );
功能同GetWindowLongPtr一致, 但其仅支持32位
nIndex 值可参考
- GWL_EXSTYLE
- Retrieves the extended window styles. For more information, see CreateWindowEx.
- GWL_STYLE
- Retrieves the window styles.
- GWL_WNDPROC
- Retrieves 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.
- GWL_HINSTANCE
- Retrieves a handle to the application instance.
- GWL_HWNDPARENT
- Retrieves a handle to the parent window, if any.
- GWL_ID
- Retrieves the identifier of the window.
- GWL_USERDATA
- Retrieves the user data associated with the window. This data is intended for use by the application that created the window. Its value is initially zero.
- The following values are also available when the hWnd parameter identifies a dialog box.
- DWL_DLGPROC
- Retrieves the address of the dialog box procedure, or a handle representing the address of the dialog box procedure.
- You must use the CallWindowProc function to call the dialog box procedure.
- DWL_MSGRESULT
- Retrieves the return value of a message processed in the dialog box procedure.
- DWL_USER
- Retrieves extra information private to the application, such as handles or pointers.
Code1:
1. 找到NotePad窗体句柄
2. 将100 置入NotePad主窗体UserData中
HWND hWndNotePad = FindWindowEx(NULL, NULL, _T("Notepad"), NULL);
SetWindowLongPtr(hWndNotePad, GWLP_USERDATA, 100);
Code2:
1. 获取NotePad窗体句柄
2. 获取NotePad窗体信息并显示
HWND hWndNotePad = FindWindowEx(NULL, NULL, _T("Notepad"), NULL);
if (NULL != hWndNotePad && IsWindow(hWndNotePad))
{
int nIndexList[] = { GWL_EXSTYLE, GWL_STYLE, GWLP_WNDPROC, GWLP_HINSTANCE, GWLP_HWNDPARENT,
GWLP_ID, GWLP_USERDATA, DWLP_DLGPROC, DWLP_MSGRESULT, DWLP_USER };
int* nValueList = new int[sizeof(nIndexList) / sizeof(*nIndexList)];
const TCHAR* szInfo[] = { _T("Extend Style:\t 0x%06X\n"),
_T("Style:\t 0x%06X\n"),
_T("WndProc: 0x%06X\n"),
_T("hInstance: 0x%06X\n"),
_T("hParent:\t 0x%06X\n"),
_T("ID: 0x%06X\n"),
_T("UserData: 0x%06X\n"),
_T("DlgProc:\t 0x%06X\n"),
_T("MsgResult: 0x%06X\n"),
_T("User: 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] = GetWindowLongPtr(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;
}
我们可以看到从Demo0028置入100UserData信息
此函数我们使用比较多, 通常与SetWindowLongPtr配合使用于,用于设置用户信息、读取窗体风格,以及读取同一进程窗体过程函数以便恢复.
(二) 特别说明
1. 如果用于替换窗体过程函数需要同一个进程中(见后序章节)