Subclassing via NativeWindow

L

Laura Martignas

Hi everybody

I'm trying to subclass Winamp's main window in order to develop a
software which will interact with Winamp.
For testing, I'm trying to intercept the closing of this window via
WM_CLOSE, by showing a MessageBox, but it doesn't work althoug the code
seems to be correct. Do you notice a mistake ?
My test program contains a form with 2 buttons : one button to activate
subclassing, and the second button to desactivate it.

Here is my code. First, my class deriving from NativeWindow :

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WinampTest
{
public delegate void WAHandler(object sender, EventArgs e);

public class WAWindow : NativeWindow, IDisposable
{
bool disposed;
public event WAHandler WAEvent;
bool subclassed;

public bool Subclassed
{
get { return subclassed; }
set { subclassed = value; }
}

public WAWindow(IntPtr hwnd)
{
AssignHandle(hwnd);
}

protected override void WndProc(ref Message m)
{
if (subclassed)
{
if (m.Msg == 0x0010) // WM_CLOSE
WAEvent(this, null);
}
base.WndProc(ref m);
}

public void Dispose()
{
if (!disposed)
{
ReleaseHandle();
GC.SuppressFinalize(this);
disposed = true;
}
}

~WAWindow()
{
Dispose();
}
}
}

And my test program :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace WinampTest
{
public partial class Form1 : Form
{
WAWindow w;
IntPtr hwnd;

const int WM_WA_IPC = 0x400;
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);

public Form1()
{
InitializeComponent();
hwnd = FindWindow("Winamp v1.x", null);
w = new WAWindow(hwnd);
w.WAEvent += new WAHandler(w_WAEvent);
}

void w_WAEvent(object sender, EventArgs e)
{
MessageBox.Show("Coucou");
}

private void button1_Click(object sender, EventArgs e)
{
w.Subclassed = true;
}

private void button2_Click(object sender, EventArgs e)
{
w.Subclassed = false;
}
}
}

So, nothing seems to be strange. Where is my mistake ?

Thanks
Laura
 
L

Laura Martignas

Stupid question.. but WinAmp is in the same process as your .NET
components?


No.. I just realized it today ! I have to program a system wide hook if I
want to access Winamp from my app... Stupid am I :)
 

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

Top