Location of This Hook in the System

The system calls the filter function for this hook only when the application has determined that there are no more pending messages in the queue but immediately before the thread yields itself to other waiting threads. This hook is called from within the message loop function.

A typical message loop is written like this:

while( GetMessage( &msg, NULL, 0, 0 ) ) 
{ 
	TranslateMessage( &msg ); 
	DispatchMessage( &msg ); 
}

When no more messages are present in the message queue, the GetMessage function puts the application into an idle state. An application in an idle state will yield control of the CPU to other applications. While the application is idle, GetMessage watches for any new messages placed in the queue. After a new message is placed there, GetMessage takes the application out of its idle state and proceeds to process this new message.

A VB message loop looks something like this:

While TRUE
	While PeekMessage(structMsg, 0, 0, 0, 1)
		If structMsg.message = WM_QUIT Then
			Exit Function
		Else
			If IsWindow And Not IsIconic(  ) then
				TranslateMessage (structMsg)
				DispatchMessage (structMsg)
			End If
		End If
	Loop
		
	If BackgroundProcessingReady(  ) Then
		DoBackGroundProcessing
	Else
		WaitMessage
	End If
Loop

Instead of using a GetMessage message loop, VB applications use a PeekMessage message loop. In doing so, the application must call the WaitMessage function to tell the system that it is in an idle state. The WaitMessage function places the application in an idle state ...

Get Subclassing and Hooking with Visual Basic now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.