views:

77

answers:

5

How can i trap a Windows system message (like WM_SETTEXT) that was sent by some window (VLC player window in my case)? I've tried to inherit NativeWindow class and override WndProc like this:

class VLCFilter : NativeWindow
{
    System.IntPtr iHandle;
    const int WM_SETTEXT = 0x000C;

    public VLCFilter()
    {
        Process p = Process.GetProcessesByName("vlc")[0];
        iHandle = p.MainWindowHandle;
    }

    protected override void WndProc(ref Message aMessage)
    {
        base.WndProc(ref aMessage);

        if (aMessage.HWnd != iHandle)
        return false;

        if (aMessage.Msg == WM_SETTEXT)
        {
            MessageBox.Show("VLC window text changed!");
        }
    }
}

I have checked with Microsoft Spy++ that WM_SETTEXT message is sent by VLC player but my code doesn't seem to get the work done. I've refered mainly to: http://www.codeproject.com/kb/dotnet/devicevolumemonitor.aspx

I'm trying to make this work for some time with no success. What am I doing wrong? What I am not doing? Maybe there is easier way to do this?

My initial goal is to catch when VLC player (that could be playing somewhere in the background and is not emmbed in my application) repeats its playback (have noticed that WM_SETTEXT message is sent then and I'm trying to find it out like this).

A: 

Do your stuff before the call to the base implementation, else values in Message could have changed.

leppie
Good point there ;)
+1  A: 

Is your code even being reached? I'm guessing you've inherited from NativeWindow but haven't made your actual windows inherit from your VLCFilter class. Which is in fact going to be a really difficult thing because you'll probably have to rewrite System.Windows.Forms.Form... (I'm guessing there's inheritance in there, but honestly not sure the internal structure in the framework.)

Perhaps you should inherit from Form instead and then have your forms inherit from your new class instead of Form?

lc
A: 

Somewhere in your code, you should be making a call to NativeWindow.AssignHandle. If you aren't (or if you're passing the wrong handle), then your overridden WndProc won't be called.

Edit: However, because VLC is running in a separate process, this technique won't work. The documentation for the NativeWindow.AssignHandle method states:

Note: The handle to assign cannot be in a different application process.

Dave Cluderay
A: 

I suppose, you could use hook techniques. It's designed for such cases.

Also, this links could be useful, despite they are easiely googled. http://www.codeproject.com/KB/cs/netwin32hooks.aspx http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx

Andrew_B
Hook techniques where the way to go. Very heplful:http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx
+1  A: 

Hello and thanks for Your answers. ;) Following the: http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx did the trick and now I'm hooked up to event i wanted. Everything works fine, there's just one glitch: when ovverriding WndProc it starts recieving messages as soon as form is created. Is there a way to temporarily disable WndProc from recieving those messages and enable only when i want to get them?