Window Management Functions
I understand the window-centered nature of windows. It's strange that you can choose from a number of window functions. These functions allow the window to query its own environment and determine its position in the window family. To find your parent window, you can call getparent (hwnd). The function receives a window handle and returns the parent window handle of the window that calls the function. If this window does not have a parent window, the function returns NULL.
Enumeration window
The getwindow function is a multi-purpose function that allows a window to query its subwindows, owner, and sibling windows.
The function is prototype: hwnd getwindow (hwnd, uint ucmd );
The first parameter is the handle of the query window, and the second parameter is a constant, indicating the relationship to be queried. Constant gw_child indicates the handle of the first subwindow of the window. Getwindow returns the window in the zcoordinate order. In this case, the first subwindow is the subwindow with the highest zcoordinate. If the window does not have a subwindow, this function returns NULL. Constant gw_hwndfirst and gw_hwndlast return the first and last windows according to the zcoordinate. If the input window handle is a top-level window (a top-level window refers to a window without a parent window or a parent window is a desktop window ), the constants return the first and last top-level (topmost) Windows based on the zcoordinate. If the input window handle is a child window, getwindow returns the first and last sibling windows. Constant gw_hwndnext and gw_hwndprev return the next lower and higher window at the zcoordinate. These constants allow the window to get the next window, call getwindow with the obtained window handle to get the next window, and so on, and finally recursively traverse all the brothers. The constant gw_owner returns the handle of the window owner.
Another method to traverse the window is the enumwindows function. Its prototype is as follows:
Bool enumwindows (wndenumproc lpenumfunc, lparam );
The parameter lpenumfunc points to a callback function. enumwindows calls this callback function once for each top-level window on the desktop, and passes in the handle of each window in sequence. The value of lparam is defined by the application and passed to the enumeration function. This function is better than the getwindow loop to find the top-level window traversal mode, because this function always returns a valid window handle, while in a getwindow traversal loop, the obtained window handle may be destroyed before the next call to getwindow. However, because enumwindows only applies to top-level windows, getwindow is useful when traversing a series of subwindows.
Search window
To obtain a handle of a specified window, use the findwindow function. The prototype of the function is hwnd findwindow (lpctstr lpclassname, lptstr lpwindowname ).
This function can be used to find the window by the class name or title of the window. When the application is started, you can use it to determine whether another copy of the program is running, all the application needs to do is to use the window class name of the Main Window of the program to call findwindow. When the findwindow returns NULL, the function does not find another window using the specified window class, as a result, there is almost no other copy running.
To find the handle of a desktop window, you can use the following function: hwnd get1_topwindow (void );
Edit window structure content
The getwindowlong and setwindowlong functions allow the program to edit data in the window structure. The two function prototypes are as follows:
Long getwindowlong (hwnd, int nindex );
Long setwindowlong (hwnd, int nindex, long dwnewlong );
Recall the wndclass structure passed to the registerclass function. It has a cbwndextra field to control the number of extra bytes allocated to the structure. If you allocate extra space for the window structure during window class registration, you can use the getwindowlong and setwindowlong functions to access these bytes. In Windows CE, this item must be allocated and referenced in 4 bytes. Therefore, if the window class is filled with the cbwndextra domain with 12 during registration, the program can use the window handle to access it through the getwindowlong or setwindowlong function, you can also set the nindex parameters to 0, 4, and 8.
Getwindowlong and setwindowlong support a predefined set of index values, which are used to make the application access some basic parameters in the window. The following is a list of index values supported by Windows CE.
Gwl_style window style logo
Gwl_exstyle window extension style flag
Gwl_wndproc pointer to the Window Process
Gwl_id window ID
The 32-bit value used by the gwl_userdata Application
The dialog box window also supports the following values:
Dwl_dlgproc pointer to the dialog box window Process
Dwl_msgresult: return value when the dialog box function returns
The 32-bit value used by the dwl_user Application
Windows CE does not support gwl_hinstance and gwl_hwndparent values, which are supported in Windows 2000 and Windows XP.
Change style logo
It is useful to edit the window structure in many ways. After a window is created, you can change its default behavior and appearance by changing its style bit. For example, you can display or hide the title bar of a window by switching the ws_caption style bit. After any style icon that modifies the appearance of a window is changed, you must call setwindowpos to force the system to re-draw the non-customer area of the window.
Setwindowpos is one of the functions that are always used in windows. It allows the application to move the window, change the window size, and change the zcoordinate of the window. In the case mentioned above, it will re-draw the non-customer area of the window. Its function prototype is as follows:
Bool setwindowpos (hwnd, hwnd hwndinsertafter, int X, int y, int CX, int cy, uint uflags );
The first parameter is the handle of the window to be changed. The optional parameter hwndinsertafter allows you to set the zcoordinate of the window. This parameter is either a window handle or one of the four logos below, used to place the window to the top or bottom of the zcoordinate. The flag is as follows:
The hwnd_bottom window is under all windows on the desktop.
Hwnd_top window is at the top of all windows
The hwnd_topmost window is always placed at the top of other windows, even if the window is inactive.
The hwnd_nottopmost window is located on top of other non-top windows (nontopmost windows) but is not marked as a top window (topmost window). In this way, when another window becomes an active window, the window can be overwritten.
Optional X, Y, CX, and Cy Parameters specify the position and size of the window. The uflags parameter contains one or more flags to describe the task to be completed. These labels are as follows:
Swp_nomove does not move the window
Swp_nosize does not change the window size
Swp_nozorder does not set window zcoordinates
Swp_noactivate if zcoordinate is set, the window is not activated.
Swp_drawframe re-painting non-customer Zone
Swp_framechanged: recalculate the non-customer partition and redraw the partition.
There are two other flags, swp_showwindow and swp_hidewindow, which can show and hide windows. However, it is easier to call the showwindow function to show and hide windows. After the style bit changes, you can use setwindowpos to force the re-painting of the frame as follows:
Setwindowpos (hwnd, 0, 0, 0, 0, swp_nomove | swp_nosize | swp_nozorder | swp_framechanged );
Window subclass
Another purpose of setwindowlong is to subclass A window. Essentially, window subclass is an instance that allows an application to derive a new window class from the existing window class. A typical application of subclass is to modify the behavior of window controls, such as an edit control.
The process of subclass is actually very simple. First, create a window to provide new functions for the quilt-type window, and then create a window with the basic window class. Call getwindowlong for the window to obtain and save a pointer pointing to its initial window process, call the setwindowlong function, and set the window process of the window instance to a new window process. In this way, the new window process begins to receive messages sent to the window. Any message that is not responded by the new window process is passed to the old window process by calling callwindowproc. The following code shows the process of Window Creation and quilt classification. During the subclass Window Process, the wm_lbuttondown message is intercepted and the speaker sounds when the window receives the message.
// Prototype of subclass procedure
Lresult callback scwndproc (hwnd, uint wmsg, wparam, lparam );
// Variable that holds the pointer to the original wndproc
Wndproc lpfnoldproc = 0;
//
// Routine that subclasses the requested window.
//
Bool subclassthiswnd (hwnd hwndsc ){
If (lpfnoldproc = 0 ){
// Get and save the pointer to the original window procedure
Lpfnoldproc = (wndproc) getwindowlong (hwndsc, gwl_wndproc );
// Point to new window procedure
Return setwindowlong (hwndsc, gwl_wndproc, (DWORD) scwndproc );
}
Return false;
}
//
// Subclass procedure
//
Lresult callback scwndproc (hwnd, uint wmsg, wparam,
Lparam ){
Switch (wmsg ){
Case wm_lbuttondown:
Messagebeep (0 );
Break;
}
Return callwindowproc (lpfnoldproc, hwnd, wmsg, wparam, lparam );
}
To remove the subclass of the window, you only need to call setwindowlong and set the wndproc pointer back to the initial window process.