Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SetWindowsHookEx and WM_PAINT

Status
Not open for further replies.

Corin93

Programmer
May 5, 2005
4
GB
Hi all,
I'm trying to write an application that will detect screen updates and send them across to a monitor station. It's effectively like a VNC application, but the supervisors here want to recieve more information than VNC provides, for training and monitoring purposes.

So, what I'm trying to get is how to use SetWindowsHookEx to grab all paint messages that occur on-screen, so that I can then grab the update rectangle and send it down the wire.

I think I need to use WH_GETMESSAGE and then trap the WM_Paint messages (&HF) that come in to the delegate procedure, but I'm not getting anything through at all.

Here's the source:

[tt]Delegate Function msgHookDelegate(ByVal Code As Integer, ByVal wParam As Integer, ByVal lParam As MSGHookStruct) As Integer

<MarshalAs(UnmanagedType.FunctionPtr)> _
Private msgCallBackDel As msgHookDelegate

Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" _
(ByVal idHook As Integer, ByVal lpfn As msgHookDelegate, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer

Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As MSGHookStruct) As Integer

Structure MSGHookStruct
Public hwnd As Long
Public message As Long
Public wParam As Long
Public lParam As Long
Public time As Long
Public pt As POINTAPI
End Structure

Public msgHandle As Integer

Public Sub HookPaintMessages()
msgCallBackDel = New msgHookDelegate(AddressOf MessageCallback)

msgHandle = SetWindowsHookEx(WH_GETMESSAGE, msgCallBackDel, Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
End Sub

Public Function MessageCallback(ByVal Code As Integer, ByVal wParam As Integer, ByVal lParam As MSGHookStruct) As Integer
console.writeline("Message Callback. Code=" & Code)

Return CallNextHookEx(msgHandle , Code, wParam, lParam)
End Function[/tt]


Any help with why this isn't working would be very much appreciated!

Corin



 
After some refinement, it now calls the MessageCallback function once and once only, no matter what else happens to the form:

Code:
Imports System.Convert
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Drawing
Imports System.Threading

Delegate Function msgHookDelegate(ByVal Code As Integer, ByVal wParam As Integer, ByVal lParam As MSGHookStruct) As Integer

<MarshalAs(UnmanagedType.FunctionPtr)> _
Private msgCallBackDel As msgHookDelegate

Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" _
(ByVal idHook As Integer, ByVal lpfn As msgHookDelegate, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer

Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As MSGHookStruct) As Integer

Structure MSGHookStruct
	Public hwnd As Long
	Public message As Long
	Public wParam As Long
	Public lParam As Long
	Public time As Long
	Public pt As POINTAPI
End Structure

Structure POINTAPI
	Public X As Long
	Public Y As Long
End Structure
Public Const WH_GETMESSAGE = 3
Public msgHandle As Integer

Public Sub HookPaintMessages()
	msgCallBackDel = New msgHookDelegate(AddressOf MessageCallback)

	msgHandle = SetWindowsHookEx(WH_GETMESSAGE, msgCallBackDel, Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
End Sub

Public Function MessageCallback(ByVal Code As Integer, ByVal wParam As Integer, ByVal lParam As MSGHookStruct) As Integer
	Console.WriteLine("Message Callback. Code=" & Code)

	Return CallNextHookEx(msgHandle, Code, wParam, lParam)
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
	HookPaintMessages()
End Sub


Corin



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top