In the nearest project (VB), you need to handle the list scroll bar scroll event, and there is no list scroll incident in VB, so the event is handled by the callback function.
Using the callback function is actually when a function is called (usually an API function), the address of one of its functions (this function is a callback function) is passed to that function. And that function is required to call the callback function using the delivered address, then you can use this opportunity to process messages in the callback function or complete a certain action.
The callback function is equivalent to an interrupt processing function, which is automatically called by the system in compliance with the conditions you set. To this end, you need to do three things:
1, statement;
2. Define the definition callback function, which is related to the specific API function, generally explain the parameters and return values of the callback function in the help;
3, set the trigger condition, which is transformed into the address as a parameter in your function in your function to make the system call.
Note and definition should pay attention to: The callback function is called by the system, and should be placed in the public module, and it cannot be regarded as a member function of a class.
The hook function is a special case of the callback function. It is used to refer to the callback function used with the setWindowsHooKex function as the hook function.
In this example, the rolling event of the List is treated with a callback function.
The API function used is
'Setting information for the specified window
Public Declare Function SetWindowlong Lib "User32" Alias "SETWINDOWLONGA"
Byval hwnd as long, _ 'Handle of the window getting information
BYVAL NINDEX As Long, _ '
BYVAL DWNEWLONG AS long_ 'Novel value of window information specified by NINDEX
AS Long
The information that nindex wants to retrieve can be any of the following constants:
Const GWL_EXSTYLE = (-20) 'extended window style
Const GWL_Style = (-16) 'window style
Const GWL_WndProc = (-4) 'The address of the window function of the window
Const gwl_hinstance = (-6) 'Handle with an instance of the window
Const GWL_HWNDPARENT = (-8) 'The handle of the parent of the window.
'Don't use setWindowWord to change this value
CONST GWL_ID = (-12) 'The identifier of a child window in the dialog
Const gwl_userdata = (-21) 'meaning by application
Const dwl_dlgproc = 4 'This window dialog function address
Const dwl_msgresult = 0 'The value returned by a message processed in the dialog function
Const dwl_user = 8 'meaning by application
'Transfer message information to the specified window process
'Return value specifies the message processing result, it is related to the message sent
Private Declare Function CallWindowProc Lib "User32" Alias "CallWindowProca" _
(ByVal LPPREVWNDFUNC AS Long, _ 'points to the front window process
BYVAL HWND As Long, _ 'Points the handle of the window process of the received message byVal MSG as long, _' Specify the message type
Byval WParam as long, _ 'Specifies the rest, message-specific information.
'The content of this parameter is related to the MSG parameter value
Byval lParam as long _ 'Specifies the rest, message-specific information.
'The content of this parameter is related to the MSG parameter value
AS Long
LPPREVWNDFUNC points to a pointer to a window process If this value is obtained by calling the getWindowlong function, and the NLndex parameter in the function is set to GWL_WNDPROC or DWL_DLGPROC, then it is actually either the address of the window or dialog or the representative The handle of this address.
First define a global variable, used to store the return address of the callback function
Public g_oldproc as long
Const GWL_WndProc = (-4)
'Custom Message Processing Function ScrollProc, the function must be placed in the public module
'Handling List rolling events
Public Function ScrollProc (Byval Hwnd As Long, BYVAL IMSG As Long, _
BYVAL WPARAM As Long, Byval LParam as Long AS Long
Const WM_HSCROLL = & H114
Const SB_ENDSCROLL = 8
'Get a message
IF (IMSG = WM_HSCROLL) THEN
IF SB_ENDSCROLL = VAL ("& H" & Right (HEX (WPARAM), 4)) THEN
'Todo
'Add your code here
END IF
END IF
'Set the callback function
ScrollProc = CallWindowProc (g_oldproc, hwnd, IMSG, WPARAM, LPARAM)
END FUNCTION
'Set the message processing function of the list control to the custom message processing function ScrollProc while the program starts running
g_oldproc = setwindowlong (DG_Result.hWnd, GWL_WndProc, Addressof ScrollProc)
'Restore list control message processing when you quit
'Restore the message handler of the list control
Call setWindowlong (DG_RESULT.HWND, GWL_WNDPROC, G_OLDPROC)
Note: The API cannot be called in an addressing method. If the address method is used, the pointer to the pointer is transmitted. The API will not return data, and the access data is wrong, so you need to deliver a string pointer with ByVal.
Basic principle of hook
Basic principle of hook
The essence of the hook is a program used to handle system messages, and hang it into the system through system call. There are many types of hooks, and each hook is responsible for intercepting and handling corresponding messages. The hook mechanism allows the application to intercept and process messages or specific events sent to the specified window, which can be created in this process can be created in this process. In a specific message, the hook program first cuts this message and obtains the control of it before arriving at the window. At this point, various modifications can be performed on the intercepted message, even forcibly termination of the continued delivery of the message.
Any hook is maintained by the system, which is a pointer list (hook table), which corrects the respective processing functions of the hook. Recently installed hooks are placed in the beginning of the chain, the earliest mounted hook is placed in the final, when the message monitored by the hooks, the first hook handler of the operating system calls the list is handled, that is, the last joined hook Get control. The hook processing function mentioned here must be a callback function (Callback function) and cannot be defined as a class member function, and must be defined as a normal C function. When using the hook, it can be divided into two categories only depending on its monitoring range, where the thread hook can only monitor a thread, and the global hook can monitor all threads running under the current system. . Obviously, the thread hook can be seen as a subset of the global hook, although the global hook is powerful but also is more cumbersome: The implementation of its hook function must be packaged in the dynamic link library. The operating system performs message interception by calling the first hook processing function at the beginning of the hook linker. Therefore, in order to set the hook, simply placing the callback function is placed in the chain, the operating system will be called first. The function setWindowsHooKex () is responsible for placing the callback function in the start position of the hook table.
After the setWindowsHooKex () function completes the installation of the hook, if the monitored event occurs, the system immediately calls the hook processing function at the beginning of the corresponding hook chain table, each hook processing function is considered when the corresponding processing is performed. Do you need to pass an event to the next hook handler. If you want to pass, you can solve it through the function callnesthookex (). Despite this, in actual use, it is highly recommended whether or not an event transfer is required at the final call of the CallNexThooKex () function, otherwise some unpredictable system behavior or system locking. This function will return the address of the next hook processing in the hook table, as for the specific return value type, depending on the set hook type.
Finally, since the mounting hook has a certain impact on the performance of the system, it should be unloaded in time to release the resources in time after the hook is used. The function releases the hook is unHookWindowsHookex ()