0

I'm writing a WPF application in Caliburn.Micro that needs to minimize to the Taskbar when closed. That part is easy using Hardcodet TaskbarIcon control. This app should also be a single instance application which I'm using a global mutex for.

The problem I'm running into is: I want to maximize the current instance from the taskbar if another instance of the application is trying to start. So check the mutex, if it cant get a lock, find the other instance and maximize it from the taskbar and shut itself down. I can't do a user32.dll ShowWindow because there is no window handle to grab when its in the taskbar.

I ideally want to do a SendMessage from the opening instance to the existing instance and tell it to maximize itself, but I cant figure out how to handle a SendMessage event using Caliburn.Micro. Unfortunately, this is the only solution I can think of and I can't figure out how to do it.

Chris Lees
  • 2,140
  • 3
  • 21
  • 41
  • Sounds like you need to do inter-process communication. Maybe this SO question will help: [What is the simplest method of inter-process communication between 2 C# processes?](http://stackoverflow.com/q/528652/4265041). Or this one: [How to remote invoke another process method from C# application](http://stackoverflow.com/q/19999049/4265041). – Steven Rands Jan 29 '15 at 16:47

1 Answers1

0

Have a look at PostMessage

Here is a great example of someone using PostMessage to do exactly what you're talking about.

Basically, you use PostMessage to broadcast a custom message:

NativeMethods.PostMessage(
                (IntPtr)NativeMethods.HWND_BROADCAST,
                NativeMethods.WM_SHOWME,
                IntPtr.Zero,
                IntPtr.Zero);

Then you override WndProc to receive the message:

protected override void WndProc(ref Message m) 
{
    if(m.Msg == NativeMethods.WM_SHOWME) 
    {
        // code here to maximize 
    }
    base.WndProc(ref m);
}

Note that you need to register your custom message and extern in the needed win32 stuff:

internal class NativeMethods 
{
    public const int HWND_BROADCAST = 0xffff;
    public static readonly int WM_SHOWME = RegisterWindowMessage("WM_SHOWME");
    [DllImport("user32")]
    public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
    [DllImport("user32")]
    public static extern int RegisterWindowMessage(string message);
}
Gordon True
  • 963
  • 7
  • 11
  • The problem is, I can't override WndProc because I'm using MVVM with Caliburn.Micro. Theres nowhere (that I cant find) where I can override that method. – Chris Lees Feb 01 '15 at 16:48
  • That's cause WndProc is from Windows Forms not WPF. I would look at the suggested answers above. – Nigel Sampson Feb 02 '15 at 22:54