|
|
|
A window property is any data assigned to a window. A window property is usually a handle of the window-specific data, but it may be any value. Each window property is identified by a string name. There are several functions that enable applications to use window properties. This overview discusses the following topics:
Advantages of Using Window Properties
Window properties are typically used to associate data with a subclassed window or a window in a multiple-document interface (MDI) application. In either case, it is not convenient to use the extra bytes specified in the CreateWindow function or class structure for the following two reasons:
- An application might not know how many extra bytes are available or how the space is being used. By using window properties, the application can associate data with a window without accessing the extra bytes.
- An application must access the extra bytes by using offsets. However, window properties are accessed by their string identifiers, not by offsets.
- For more information about subclassing, see Window Procedure Subclassing. For more information about MDI windows, see Multiple Document Interface.
Assigning Window Properties
The SetProp function assigns a window property and its string identifier to a window. The GetProp function retrieves the window property identified by the specified string. The RemoveProp function destroys the association between a window and a window property but does not destroy the data itself. To destroy the data itself, use the appropriate function to free the handle that is returned by RemoveProp.
Enumerating Window Properties
The EnumProps and EnumPropsEx functions enumerate all of a window's properties by using an application-defined callback function. For more information about the callback function, see PropEnumProc.
EnumPropsEx includes an extra parameter for application-defined data used by the callback function. For more information about the callback function, see PropEnumProcEx.
Step-By-Step Example - Create a standard exe project - Add one module to the project - Add the following code in form1
Form1.frm |
Click here to copy the following block | Private Sub Form_Load() Me.AutoRedraw = True
Randomize
SetProp Me.hWnd, "value1", 12 SetProp Me.hWnd, "value2", 23 SetProp Me.hWnd, "value3", Rnd * 100
EnumPropsEx Me.hWnd, AddressOf PropEnumProcEx, 1
RemoveProp Me.hWnd, "value1" RemoveProp Me.hWnd, "value2" RemoveProp Me.hWnd, "value3" End Sub |
- Add the following code in Module1
Module1.bas |
Click here to copy the following block | Public Declare Function EnumProps Lib "user32.dll" Alias "EnumPropsA" ( _ ByVal hWnd As Long, _ ByVal lpEnumFunc As Long) As Long
Public Declare Function EnumPropsEx Lib "user32.dll" Alias "EnumPropsExA" ( _ ByVal hWnd As Long, _ ByVal lpEnumFunc As Long, _ ByVal lParam As Long) As Long
Public Declare Function GetAtomName Lib "kernel32.dll" Alias "GetAtomNameA" ( _ ByVal nAtom As Integer, _ ByVal lpBuffer As String, _ ByVal nSize As Long) As Long
Public Declare Function GetProp Lib "user32.dll" Alias "GetPropA" ( _ ByVal hWnd As Long, _ ByVal lpString As String) As Long
Public Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" ( _ ByVal lpString1 As String, _ ByVal lpString2 As Long) As Long
Public Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" ( _ ByVal lpString As String) As Long
Public Declare Function RemoveProp Lib "user32.dll" Alias "RemovePropA" ( _ ByVal hWnd As Long, _ ByVal lpString As String) As Long
Public Declare Function SetProp Lib "user32.dll" Alias "SetPropA" ( _ ByVal hWnd As Long, _ ByVal lpString As String, _ ByVal hData As Long) As Long
Function PropEnumProcEx(ByVal hWnd As Long, ByVal lpszString As Long, ByVal hData As Long, ByVal dwData As Long) As Boolean Dim sPropName As String
If (lpszString And &HFFFF0000) = 0 Then sPropName = Space$(255) sPropName = Left$(sPropName, _ GetAtomName(lpszString, sPropName, Len(sPropName))) Else sPropName = Space$(lstrlen(lpszString)) lstrcpy sPropName, lpszString End If
If dwData = 0 Then Debug.Print "Property Name: " & Replace(sPropName, Chr(0), vbNullString) & " [Value: " & hData & "]" ElseIf dwData = 1 Then MsgBox "Property Name: " & Replace(sPropName, Chr(0), vbNullString) & " [Value: " & hData & "]" ElseIf dwData = 2 Then Form1.Print "Property Name: " & Replace(sPropName, Chr(0), vbNullString) & " [Value: " & hData & "]" End If
PropEnumProcEx = True End Function
Function PropEnumProc(ByVal hWnd As Long, ByVal lpszString As Long, ByVal hData As Long) As Boolean Dim sPropName As String
If (lpszString And &HFFFF0000) = 0 Then sPropName = Space$(255) sPropName = Left$(sPropName, _ GetAtomName(lpszString, sPropName, Len(sPropName))) Else sPropName = Space$(lstrlen(lpszString)) lstrcpy sPropName, lpszString End If
PropEnumProc = True
Form1.Print "Property Name: " & Replace(sPropName, Chr(0), vbNullString) & " [Value: " & hData & "]" End Function |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|