Trapping Windows Messages from other applications

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Okay I'm trying to write an application to monitor a running process. Right
now I have a front end which allows the user to pick the programs they want
to monitor. The interval between how often I information for this process,
things like CPU usage, peak memory, virtual memory, etc... All this I get
from System.Diagnostics.Process. I display this information in a listview and
things are working fine. The user can then select one of these processes
being monitored and "I'll display modules in a treeview to them.

Now the thing I want to also do is display windows messages in a listbox. So
for example say I'm monitoring a program called MyApp.exe. I want to select
this program and then go back to this program and contiue working in it.
Clicking command buttons, entering data, selecting menu options. But I want
all these message to be captured by this monitoring program in that listbox
so when I return to it I would see a windows message for
WM_MOUSEMOVE
WM_LBUTTONDOWN
WM_LBUTTONUP
WM_LBUTTONDBLCLK
WM_RBUTTONDOWN
WM_RBUTTONUP
WM_RBUTTONDBLCLK
WM_MBUTTONDOWN
WM_MBUTTONUP
WM_MBUTTONDBLCLK
etc...

I will also create another list of what messages I want to capture but first
I need to capture them and can't figure this part out.
 
Hi,

For that you will have to P/invoke the win32 API, IIRC the function is
called SetWinEventHook, you will need either the hWnd of the main window or
the process ID of the target process.
Take a look at google groups you will find the answer there,

cheers,
 
Joe,

This can be accomplished using the System.Windows.Forms.NativeWindow
class. See the following example.


using System;
using System.Windows.Forms;

namespace WindowsApplication
{
public class MyHookClass : NativeWindow
{
public MyHookClass(IntPtr hWnd)
{
// Assign the handle from the source window to this class.
this.AssignHandle(hWnd);
}

protected override void WndProc(ref Message m)
{
const int WM_MOUSEMOVE = 0x0200;

switch (m.Msg)
{
case WM_MOUSEMOVE:
// Do what you need to do.
break;
}

base.WndProc (ref m);
}
}
}


Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top