The following is an article from csdn, hoping to find some useful things.
Interface Technology Overview
In the "hook file open/save dialog box", I first studied the knowledge of the interface library. The interface library is generally encoded by C/C ++, a medium-and low-level language, this is because the implementation technology of the interface library in Windows is mainly used to directly control Windows messages and call windows APIs. This is the advantage of this medium and low-level language. Regardless of the interface library, the most fundamental principle is to obtain or intercept some messages in the window, process these messages as needed, and draw the interface you need.
According to the usage of the interface library in windows, there are two types:
1. Use the library by deriving and inheriting the classes in the interface library. This type of interface library currently accounts for the vast majority. This type of interface library can control the display style of controls and windows of the same type. This type of interface library is represented by guitoolkit and profuis.
2. interface library used by DLL through link header file. These interface libraries are generally commercialized interface libraries. This type of interface library generally displays the same style for controls and windows of the same type. Typical examples of this type of interface library are skin ++ and appface.
The above classification also represents two interface library implementation technologies, that is, two sources for obtaining messages used for the Self-painting window:
1. Change the window style through subclass and superclass. In fact, it is to call the Windows API setwindowlong or change the default message processing function of the Windows window through class derivation and inheritance.
2. Use the Hook Technology to change the default message processing for Windows.
1. setwindowlong
Setwindowlong (hwnd, // The Window handle of the UI window to be changed
Int nindex, // replace the window's default message processing function with gwl_wndproc
Long dwnewlong) // new default window message processing function
You can call this API function to replace the default message processing function of a window. In this way, you can intercept messages in the target window in the new window message processing function, then process the messages as needed. This API is used to obtain messages in the current window. It cannot obtain messages in the window subwindow.
This type of interface library is generally an Open Source library or an interface library that provides header files and Lib files.
This Windows API is rarely called directly, but its encapsulation-subclasswindow member function, which I think has been used by everyone. Let's see where setwindowlong is stored and how it works in the subclass process of various types.
1. Implementation in MFC
In the MFC program, you can first add a window class for subclass in the project, and then you can use the classwizard tool to complete the remaining subclass work. We use a custom cmybutton class as an example. When you open classwizard to add a corresponding variable for a button resource in our dialog-based project, we can see that the cmybutton class is defined directly, for example:
We define a member variable m_mybtn of cmybutton type for a button resource with ID idc_button1. At this time, classwizard will add the previous statement for us in the overloaded virtual function dodataexchange, as follows:
Void cmfcsampledlg: dodataexchange (cdataexchange * PDX)
{
Cdialog: dodataexchange (PDX );
// {Afx_data_map (cmfcsampledlg)
Ddx_control (PDX, idc_button1, m_mybtn); // This Is What classwizard adds for us.
//} Afx_data_map
}
Let's take a look at what the ddx_control function has done for us. In the source code dlgdata. cpp file of MFC, we can find the source code of this function:
Void afxapi ddx_control (cdataexchange * PDX, int NIDC, cwnd & rcontrol)
{
If (rcontrol. m_hwnd = NULL) // not subclassed yet
{
......
...
// Check out. Oh, subclasswindow is here.
If (! Rcontrol. subclasswindow (hwndctrl ))
{
Assert (false); // possibly trying to subclass twice?
Afxthrownotsupportedexception ();
}
......
...
}
}
Let's look at the implementation of the subclasswindow member function under MFC. In the source code wincore. cpp of MFC, we can see the implementation of subclasswindow in the basic class cwnd of the window class under all MFC:
Bool cwnd: subclasswindow (hwnd)
{
If (! Attach (hwnd ))
Return false;
// Allow any other subclassing to occur
Presubclasswindow ();
// Now hook into the afx wndproc
Wndproc * lplpfn = getsuperwndprocaddr ();
// Check whether it uses setwindowlong.
Wndproc oldwndproc = (wndproc): setwindowlong (hwnd, gwl_wndproc,
(DWORD) afxgetafxwndproc ());
......
...
Return true;
}
In the source code tracing instruction above, classwizard is a Windows API called setwindowlong, which is used for the sub-class chemical engineering that we have automatically completed. Of course, in MFC, you can also manually complete subclass by calling the subclasswindow member function.
2. Implementation under ATL/wtl
In the source code of ATL, In the atlwin. h file, we can find the implementation of subclasswindow in the base class cwindowimplbaset of all ATL window classes:
Template <class tbase, class twintraits>
Bool cwindowimplbaset <tbase, twintraits >:: subclasswindow (hwnd)
{
Atlassert (m_hwnd = NULL );
Atlassert (: iswindow (hwnd ));
M_thunk.init (getwindowproc (), this );
Wndproc pproc = (wndproc) & (m_thunk.thunk );
// Note that it also calls setwindowlong!
Wndproc pfnwndproc = (wndproc): setwindowlong (hwnd, gwl_wndproc, (long) pproc );
If (pfnwndproc = NULL)
Return false;
M_pfnsuperwindowproc = pfnwndproc;
M_hwnd = hwnd;
Return true;
}
In ATL/wtl, there is no automatic subclass mechanism under MFC. If subclass is required, the member function subclasswindow is called directly.
Ii. setwindowshookex
Setwindowshookex (INT idhook, // hook type
Hookproc lpfn, // hook callback function
Hinstance hmod, // application instance handle
DWORD dwthreadid) // thread ID
The setwindowshookex API can be used to set many types of hooks. They intercept different messages from threads whose thread ID is dwthreadid at different times. When writing the interface library, you generally set the hook type to wh_callwndproc to obtain and process the message before the window processes the message. In the callback function of the hook, you can obtain the window handle and the type and style of the window. In this way, you can know the type of message to be processed. Generally, wm_create messages are intercepted, and then wm_paint, wm_ncpaint, and other UI-related messages are processed to achieve the UI of the Self-painted window.
Setwindowlong can also be used in the interface library using hook technology. In the interface library written in C ++, after obtaining the window handle, that is, the setwindowlong technique is used to associate the window handle to a specific window class. In addition, the display effects of different styles can be made for the same Class Based on the window style. Unfortunately, the interface libraries on the market seldom perform such tedious differentiation.
This type of interface library usually appears in the DLL file format. Currently, the most popular is to encapsulate the interface library as a com DLL and create this COM component in the application, obtain related interfaces and call related interface functions to install hooks for applications. During the "Picasso style file open/save dialog box", we have been using the Save dialog box of Yahoo Messenger as a reference. In fact, Yahoo Messenger is using this type of interface library. Yahoo Messenger's interface library intercepts and processes messages from all threads in the process. Therefore, it can intercept messages created in some system windows and UI-related messages, to change the display style of Windows.
Finally, we will introduce several good open-source interface libraries or examples.
1. classxp (http://www.yonsm.net/read.php? 26)
Individual open-source interface library, written in C language, not perfect, Using Hook Technology. The interface library of Yahoo Messenger is similar to this interface library, But Yahoo Messenger is more complete.
2. profuis (http://www.codeproject.com/docking/prod_profuis.asp)
A partially open-source interface library with a commercial version with complete functions. Well-developed, written in MFC, using the setwindowlong technology.
3. guitoolkit (http://www.codeproject.com/library/guitoolkit.asp)
An open-source interface library, which is well-developed and written by MFC, uses the setwindowlong technology.
4. Example of controlspy in msdn.
An example in msdn is used to understand the messages of various controls.