[英]Maximize WPF application from the taskbar using another application instance and Caliburn.Micro
I'm writing a WPF application in Caliburn.Micro that needs to minimize to the Taskbar when closed. 我正在Calicali.Micro中编写一个WPF应用程序,该应用程序在关闭时需要最小化到任务栏。 That part is easy using Hardcodet TaskbarIcon control. 使用Hardcodet TaskbarIcon控件很容易做到这一点。 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. 我无法执行user32.dll ShowWindow,因为在任务栏中没有窗口句柄可供抓取。
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. 理想情况下,我想从打开的实例到现有实例执行一个SendMessage并告诉它最大化自身,但是我无法弄清楚如何使用Caliburn.Micro处理SendMessage事件。 Unfortunately, this is the only solution I can think of and I can't figure out how to do it. 不幸的是,这是我能想到的唯一解决方案,我不知道该怎么做。
Have a look at PostMessage 看看PostMessage
Here is a great example of someone using PostMessage to do exactly what you're talking about. 这是一个很好的例子,有人使用PostMessage来完成您正在说的事情。
Basically, you use PostMessage to broadcast a custom message: 基本上,您使用PostMessage广播自定义消息:
NativeMethods.PostMessage(
(IntPtr)NativeMethods.HWND_BROADCAST,
NativeMethods.WM_SHOWME,
IntPtr.Zero,
IntPtr.Zero);
Then you override WndProc to receive the message: 然后,您重写WndProc以接收消息:
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: 请注意,您需要在所需的win32内容中注册自定义消息和extern:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.