I could not find where someone had done this before in C# so I just wrote a
short class from scratch. This class creates a message-only window that you
may/may not be able to use/adapt to fit your needs. It's really rare that
anyone would ever need to do this but who knows
HTH
-sb
<MessageWindow.cs>
using System;
using System.Runtime.InteropServices;
namespace TestApplication
{
public class MessageWindow : IDisposable
{
#region DllImports
[DllImport("user32.dll")]
private static extern IntPtr CreateWindowEx(uint dwExStyle, string
lpClassName, string lpWindowName, uint dwStyle,
int x, int y, int nWidth, int nHeight, IntPtr hWndParent,
IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);
[DllImport("user32.dll")]
private static extern bool DestroyWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex,
IntPtr dwNewLong);
[DllImport("user32.dll")]
private static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc,
IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
#endregion
private const int GWL_WNDPROC = -4;
private const int HWND_MESSAGE = -3;
private delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg,
IntPtr wParam, IntPtr lParam);
private WndProcDelegate _WndProc;
private IntPtr _OldWndProc;
private IntPtr _hWnd;
public IntPtr Handle // public property
{
get { return _hWnd; }
}
public MessageWindow()
{
_hWnd = CreateWindowEx(0, "STATIC", "MyMessageOnlyWindow", 0, 0,
0, 0, 0,
new IntPtr(HWND_MESSAGE), IntPtr.Zero, IntPtr.Zero,
IntPtr.Zero);
_WndProc = new WndProcDelegate(WndProc);
_OldWndProc = new IntPtr(SetWindowLong(_hWnd, GWL_WNDPROC,
Marshal.GetFunctionPointerForDelegate(_WndProc)));
}
protected IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam,
IntPtr lParam)
{
System.Diagnostics.Debug.WriteLine(msg); // let's see what was
sent
return CallWindowProc(_OldWndProc, hWnd, msg, wParam, lParam);
}
#region IDisposable Members
public void Dispose()
{
if (_hWnd != IntPtr.Zero)
{
DestroyWindow(_hWnd);
_hWnd = IntPtr.Zero;
}
}
#endregion
}
}
<Program.cs>
using System;
using System.Runtime.InteropServices;
namespace TestApplication
{
class Program
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr
wParam, IntPtr lParam);
static void Main(string[] args)
{
MessageWindow m = new MessageWindow();
SendMessage(m.Handle, 55, IntPtr.Zero, IntPtr.Zero);
Console.ReadLine();
SendMessage(m.Handle, 77, IntPtr.Zero, IntPtr.Zero);
Console.ReadLine();
}
}
}