简体   繁体   English

使用另一个应用程序实例和Caliburn.Micro从任务栏最大化WPF应用程序

[英]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.

相关问题 使用 Caliburn.Micro 的单实例 WPF 应用程序 - Single instance WPF application using Caliburn.Micro WPF Caliburn.Micro - 在Singe Window应用程序中导航的最佳方式 - WPF Caliburn.Micro - Best way to navigate in Singe Window Application 从WPF MVVM Caliburn绑定到用户控件的属性 - Binding to property of usercontrol from WPF MVVM Caliburn.Micro application doesn't work 从Caliburn.Micro,WPF,MVVM的另一个窗口获取信息 - Get info from another window in Caliburn.Micro, WPF, MVVM 如何使用Caliburn.Micro MVVM在WPF中的同一视图上从另一个视图模型更新list &lt;&gt;? - How to update list<> from another View model on same View in WPF using Caliburn.Micro MVVM? 如何在Caliburn.Micro应用程序中访问ShellViewModel? - How to access ShellViewModel in an Caliburn.Micro application? 在WPF中,应用程序MainWindow为空(使用Caliburn Micro) - Application MainWindow is null in WPF (using Caliburn Micro) 使用Caliburn.micro调用另一个ViewModel - Calling another ViewModel using Caliburn.micro Wpf Caliburn.Micro 按下关闭按钮停止控件从另一个线程更新 - Wpf Caliburn.Micro pressing Close button stops controls update from another thread 在WPF中使用INotifyDataErrorInfo和嵌入式UserControl(与Caliburn.Micro)一起使用 - Using INotifyDataErrorInfo with embedded UserControl in WPF (with Caliburn.Micro)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM