Visual C++ and MFC Fundamentals Chapter 15: Fundamental Controls
14.4.1..Retrieving Control Information.........................................................
To get information about a control, you can call the GetWindowLong() function. Its
syntax is:
LONG GetWindowLong(HWND hWnd, int nIndex );
After this function executes, it returns a LONG value. If you prefer to get a pointer and if
you are working on a 64-bit environ, use the GetWindowLongPtr() function instead. Its
syntax is:
LONG_PTR GetWindowLongPtr(HWND hWnd, int nIndex);
The first argument, hWnd, is a handle to the control whose information you are seeking.
The nIndex argument specifies the type of information you are looking for. Its possible
values are:
nIndex Value
GetWindowLong() GetWindowLongPtr()
Description
GWL_EXSTYLE This is used to get information about the extended
style(s) used on the control
GWL_STYLE This is used to get information about the style(s)
used on the control
GWL_WNDPROC GWLP_WNDPROC Remember that a window procedure is a function
pointer. If such a procedure was used to handle the
message(s) for the hWnd control, use this constant
to get the address of that procedure
GWL_HINSTANCE GWLP_HINSTANCE This gives access to the handle to the current
application
GWL_HWNDPARENT GWLP_HWNDPARENT This constant can be used to get a handle to the
parent window. For example, you can use it the
get a handle to a dialog box that is hosting the
hWnd control.
GWL_ID GWLP_ID This gives you the ID of the hWnd control
GWL_USERDATA GWLP_USERDATA This gives you a 32-bit value used by the current
application
If the hWnd argument represents a dialog box, nIndex can use the following values:
nIndex Value
GetWindowLong GetWindowLongPtr
Description
DWL_DLGPROC DWLP_DLGPROC This provides the address of, or a pointer, to the
procedure of the dialog box
DWL_MSGRESULT DWLP_MSGRESULT This provides the return value of the dialog box’
procedure
DWL_USER DWLP_USER This provides additional information about the
application
After calling this function and specifying the type of information you need, it returns a
(constant) value you can use as you see fit. Here are two methods of getting a handle to
the instance of the application. The second example uses the GetWindowLong()
function:
BOOL CDialog1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();