By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,509 Members | 1,261 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,509 IT Pros & Developers. It's quick & easy.

Subclassing via NativeWindow

P: n/a
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
Jul 23 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Stupid question.. but WinAmp is in the same process as your .NET components?

"Laura Martignas" <la********@libertysurf.frha scritto nel messaggio
news:Xn**************************@193.252.117.183. ..
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

Jul 24 '06 #2

P: n/a
"Laura T" <laura.t@_yahoo.comwrote in
news:eC*************@TK2MSFTNGP03.phx.gbl:
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 :)

Jul 24 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.