首页 > 其他分享 >WPF single instance via mutex

WPF single instance via mutex

时间:2024-07-07 16:09:07浏览次数:21  
标签:IntPtr via DllImport System instance other static using WPF

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp198
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        [DllImport("user32", CharSet = CharSet.Unicode)]
        static extern IntPtr FindWindow(string cls, string win);

        [DllImport("user32")]
        static extern IntPtr SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32")]
        static extern bool IsIconic(IntPtr hWnd);
        [DllImport("user32")]
        static extern bool OpenIcon(IntPtr hWnd);

        protected override void OnStartup(StartupEventArgs e)
        {
            bool isNew;
            var mutex = new Mutex(true, "MySingleInstanceMtx", out isNew);
            if(!isNew) 
            {
                ActivateOtherWindow();
                Shutdown();
            }
        }

        private static void ActivateOtherWindow()
        {
            var other = FindWindow(null, "Single Instance");
            if(other!=IntPtr.Zero)
            {
                SetForegroundWindow(other);
                if(IsIconic(other))
                {
                    OpenIcon(other);
                }
            }
        }
    }
}

 

标签:IntPtr,via,DllImport,System,instance,other,static,using,WPF
From: https://www.cnblogs.com/Fred1987/p/18288610

相关文章

  • WPF MenuItem behavior MVVM
    //xaml<ImageGrid.Column="1"ClipToBounds="True"Source="{BindingSelectedItem.ImgUrl,ElementName=lbx}"><Image.ContextMenu><ContextMenu><MenuItemHeader="S......
  • WPF MVVM capture window keyboard
    //xaml<behavior:Interaction.Triggers><behavior:EventTriggerEventName="KeyDown"><behavior:CallMethodActionTargetObject="{Binding}"MethodName="Window_KeyDown"/></beha......
  • Towards Accurate and Robust Architectures via Neural Architecture Search
    基于网络架构搜索的准确性与鲁棒性结构研究论文链接:https://arxiv.org/abs/2405.05502项目链接:未开源Abstract为了保护深度神经网络免受对抗性攻击,对抗性训练因其有效性而受到越来越多的关注。然而,对抗训练的准确性和鲁棒性受到体系结构的限制,因为对抗训练通过调整隶属......
  • WPF自定义控件与样式-自定义按钮(Button)
    一、前言程序界面上的按钮多种多样,常用的就这几种:普通按钮、图标按钮、文字按钮、图片文字混合按钮。本文章记录了不同样式类型的按钮实现方法。二、固定样式的按钮固定样式的按钮一般在临时使用时或程序的样式比较固定时才会使用,按钮整体样式不需要做大的改动。2.1普通按钮-......
  • WPF Menu实现快捷键操作
    很多小伙伴说,在Menu中,实现单个快捷键操作很简单,怎么实现多个快捷键操作和,组合快捷键呢,今天他来了。上代码和效果图一、Ctrl+Shift+任意子母键实现快捷键组合<Windowx:Class="XH.TemplateLesson.MenuWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xa......
  • WPF DataContext
    后台代码:publicclassStudent{publicintId{get;set;}publicstringName{get;set;}publicintAge{get;set;}} 前台代码:<Windowx:Class="BindingTest.MainWindow"xmlns="http://schem......
  • 学懂C#编程:WPF应用开发系列——WPF之ComboBox控件的详细用法
    WPF(WindowsPresentationFoundation)中的ComboBox控件是一个下拉列表控件,允许用户从一组预定义的选项中选择一个选项。以下是ComboBox控件的详细用法,并附带示例说明。ComboBox的基本用法1.XAML定义:在XAML中定义一个ComboBox控件,并添加一些选项。<Windowx:Class="ComboBox......
  • Bug记录|vivia主题|Hexo+GitHub搭建个人博客
    1.将本地SSH添加到远程github 中,之后关联远程或push出现以下错误:fatal:Notagitrepository(oranyoftheparentdirectories):.git解决方案:执行 gitinit。gitinit2.hexog无法成功运行,出现以下错误:TypeError:C:\Users\Maxence\Desktop\项目\MyBlog\Hexo......
  • WPF Performance Suite, Microsoft Windows Performance Toolkit
    Copyfrom https://www.cnblogs.com/lindexi/p/12086719.htmlhttps://learn.microsoft.com/en-us/previous-versions/aa969767(v=vs.110) 1.Downloadurl:  https://download.microsoft.com/download/A/6/A/A6AC035D-DA3F-4F0C-ADA4-37C8E5D34E3D/setup/WinSDKPerformanceT......
  • WPF Datagrid ContextMenu MenuItem Command CommandParameter MultiBinding
     //xaml<Windowx:Class="WpfApp194.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas......