The GetWindowLong function (the best explanation of the data is definitely worth a look, this function is often used in the sub-class technique)

Source: Internet
Author: User

Today I was looking at a code that wrote GetWindowLong (hwnd, 0), and I was a little puzzled, leaning, surfing the internet

Search function prototype description, dead or alive can not find the 2nd parameter to 0 is what, MD, very annoyed, recently finally in a

Foreign sites found, I posted in this article, the site is

Http://www.codeguru.com/forum/archive/index.php/t-243778.html

Gather all the usage and program code for this function first

Hope that later people see me this article can be moved ha:

function function Description: Use this function to get the information of the specified window

Function Prototypes:
LONG GetWindowLong (HWND hwnd,int nIndex)

Parameters:
HWnd: Specifies the handle of the window
NIndex: Types of information to be obtained
Value function

The nindex values are as follows:
Gwl_exstyle to get extended window style
Gwl_style Get window Style
Gwl_wndproc gets the address of the window callback function, or the handle. The CallWindowProc function must be used to invoke the
Gwl_hinstance getting a handle to an application running instance
Gwl_hwndparent get the handle of the parent window
GWL_ID get the identifier of the window
Gwl_userdata gets the 32-bit value associated with the window (each window has a 32-bit application that is intentionally left to create the window)
's value)

The following values can be used when the HWND identifies a dialog box
Value Action
Dwl_dlgproc gets the address of the dialog box callback function, or handle. The CallWindowProc function must be used to invoke the
Dwl_msgresult get the return value of the message processing in the dialog box callback function
Dwl_user get additional application-private information, such as some handles and pointers, etc.

return value:
When successful, returns a 32-bit value for a request
On failure, return 0, you can use GetLastError to get the error message

Attention:

/*
* Window field offsets for GetWindowLong ()
*/
#define GWL_WNDPROC (-4)
#define Gwl_hinstance (-6)
#define Gwl_hwndparent (-8)
#define GWL_STYLE (-16)
#define Gwl_exstyle (-20)
#define GWL_USERDATA (-21)
#define GWL_ID (-12)

/*
* Get/setwindowword/long offsets for use with wc_dialog windows
*/
#define DWL_MSGRESULT 0
#define DWL_DLGPROC 4
#define Dwl_user 8

Example 1:
Long Nstyle =:: GetWindowLong (HWnd, Gwl_style); HWND is a handle to an edit box
if (Nstyle & Es_password)
{
AfxMessageBox ("This is a password field");
}

Example 2:

LONG GetWindowLong (
HWND hwnd,//Handle of window
int NIndex//offset of value to retrieve
);
The second argument is 0, which is to specify gw_hwndfirst!!!
If you want to customize the data with your application, you need to specify a second parameter of Gwl_userdata (-21)

、、、、、、、、、、、、、、、、、、

The SetWindowLong function, which changes the special properties of the specified window. Here is its declaration: SetWindowLong (hwnd, 0, 1^getwindowlong (hwnd, 0));
Little Brother Beginner API, not very understanding of this function, see MSDN also not clear, especially as parameters of the 1^getwindowlong (hwnd, 0), this "^" is what meaning, hope Master pointing Maze

Declare Function setwindowlong Lib "user32" Alias "Setwindowlonga" (ByVal hwnd as Long, ByVal NIndex As Long,byval Dwnewlong as long) as long

The first parameter represents the window to be processed by the subclass, the second parameter should be GWL_WNDPROC (-4), and the third parameter is the address of the new window function

This function will be called at any time when the window takes focus, an event occurs, or in other cases (such as when other processes change certain parameters of the system).

If an error occurs, the SetWindowLong function returns 0, otherwise the address of the original window function is returned. This address is especially important and you should keep it in a variable or somewhere else. When you're not dealing with certain messages (in fact, you might only be dealing with less than 1% of messages, The other will be processed by the original window function, which is required to call the original window function.
////////////////////////////////////////////////////////////////////////////////////////////
Questions from others:::::
^ is XOR.
Because GetWindowLong returns 0 after a failure, the meaning of this statement is that it is added to this property when the window does not have this property. Popular Point says, is get first, not get, again set.
In addition, what is the attribute of index = = 0 set? The second floor should be said to be Gwl_wndproc (-4), based on what? 1 floor YXWMXKQ (cool to move the party) reply at 2002-12-12 21:06:32 score 0 My purpose is: When creating subclasses: CreateWindow (..., (hinstance) GetWindowLong (hwnd,gwl_ HINSTANCE), NULL);
I just want to make sure that SetWindowLong (hwnd,0,0) is not the extra window memory in the long position 0, and SetWindowLong (hwnd,0,1) is set to 1, if not, how to setup Setwindo The Wlong function makes the long value in the extra window memory 0 or 1, and of course the transformations of 0 and 1 I do not know if the 1^getwindowlong (hwnd,0) is correct. 3x!!
Top

2 Floor sans (long wind half-day) reply at 2002-12-13 09:56:03 score 50 If you want to manipulate user data, you first set the size of the extended data in the Cbwndextra of the window wndclass, and then RegisterClass, After using GetWindowLong (hwnd, Gwl_userdata + 0), GetWindowLong (hwnd, Gwl_userdata + 1), and so on, use Setwindo Wlong (hwnd, Gwl_userdata + 0, LData), SetWindowLong (hwnd, Gwl_userdata + 1, lData), etc. write.

(go) GetWindowLong function (the best explanation of the data, definitely worth a look, this function is often used in sub-class techniques)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.