Stoitcho
Tue Apr 11 08:58:51 CDT 2006
In order to start a mesage pump in a console applicatio what you need to do
is
SomeForm f = new SomeForm();
Application.Run(f);
once the Application.Run is called it starts the message loop which means
the call will block there until the form is not closed. The console window
will still be visible and all Console.WriteXXX calls will output to it.
If you don't want to block tha main console applcication thread that can be
done in a worker thread also.
BTW, WinXP and Win2K support message only windows
Those windows are not visible doesn't have Z order and are used merely for
sending and receiving messages
(
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowfeatures.asp).
In .NET I believe they can be createed using NativeWindow
NativeWindow mwnd =null;
.....
CreateParams cp = new CreateParams();
cp.Parent = new IntPtr(-3);
NativeWindow mwnd = new NativeWindow();
mwnd.CreateHandle(cp);
I must say that I've never used them. I created test application and can
verify that such windows is created (Spy++).
Just a reminder that message-only widnows still require message loop.
--
HTH
Stoitcho Goutsev (100)
"sb" <stormfire1@yahoo.com> wrote in message
news:%236UipVSXGHA.3972@TK2MSFTNGP04.phx.gbl...
>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();
> }
> }
> }
>
>