c# wpf单实例运行并激活前实例
来源:互联网 发布:mac微信无法退出 编辑:程序博客网 时间:2024/05/14 09:08
先创建程序设置:
Properties——Settings.settings
名称类型范围值wlong用户0
在App.g.cs文件中加入:
using System.Threading;
using System.Runtime.InteropServices;
using System.Reflection;
using xxxxxxxxxxxxxxxxxxxxxxx.Properties;//项目的名称
#region Win32 API 函数
//该函数设置由不同线程产生的窗口的显示状态;
//如果函数原来可见,返回值为非零;如果函数原来被隐藏,返回值为零。
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
//该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。系统给创建前台窗口的线程分配的权限稍高于其他线程。
//如果窗口设入了前台,返回值为非零;如果窗口未被设入前台,返回值为零。
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
// IsIconic、IsZoomed ------ 分别判断窗口是否已最小化、最大化
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool IsZoomed(IntPtr hWnd);
//获取当前系统中被激活的窗口
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern bool FlashWindow(IntPtr hWnd, bool bInvert);
#endregion
private const int SW_SHOWNORMAL = 1;
private const int SW_RESTORE = 9;
Main方法:
[System.STAThreadAttribute()]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
public static void Main()
{
bool b;
string a = "1"; //名字任意
using (Mutex m = new Mutex(true, a, out b))
{
if (b)
{
m.ReleaseMutex();
App app = new App();
app.InitializeComponent();
app.Run();
}
else
{
Settings s = new Settings();
IntPtr i = (IntPtr)s.w;
SetForegroundWindow(i);
if (IsIconic(i))
{
ShowWindowAsync(i, SW_RESTORE);
}
else
{
ShowWindowAsync(i, SW_SHOWNORMAL);
}
GetForegroundWindow();
FlashWindow(i, true);
}
}
}
在App.xaml.cs中:
using xxxxxx.Properties;
namespace xxxxxxxxxxx
{
public partial class App: System.Windows.Application
{
protected override void OnExit(ExitEventArgs e)
{
Settings r = new Settings();
r.Reset();
base.OnExit(e);
}
}
}
在Window1.xaml.cs中:(加入Loaded事件)
using xxxxxxxxxxxxxxxxxxxx.Properties;
using System.Windows.Interop;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
IntPtr hwnd = ((HwndSource)PresentationSource.FromVisual(this)).Handle;
Settings r = new Settings();
r.w = (Int64)hwnd;
r.Save();
}
- c# wpf单实例运行并激活前实例
- C# WinForm 程序单实例运行,再次运行时激活前一个实例
- 【转】C# WinForm 程序单实例运行,再次运行时激活前一个实例
- 激活前一个实例,保持单实例运行
- 程序只运行一个实例,并激活前一个实例
- 程序只运行一个实例,并激活前一个实例
- 程序只运行一个实例,并激活前一个实例
- 程序只运行一个实例,并激活前一个实例
- WPF-单实例运行设置
- C# 单实例运行
- c# 单实例运行
- 可执行文件单实例运行且激活已运行的实例
- vc 只运行一个实例,并激活已运行实例
- c#WinForm 程序单实例运行并打开前一个实例(Mutex+API)
- C# 单实例运行控制
- C# windows应用程序单实例运行实例
- 只运行一个实例并激活以启动实例
- 程序只运行一个实例,并且把前一个激活
- GNU ARM汇编指令
- linux bash command bash脚本命令
- 多线程的基本知识
- CSS空格
- 网页打印中页眉和页脚的问题
- c# wpf单实例运行并激活前实例
- 决定成败的49个细节
- TreeView控件
- 初探jconsole+weblogic9.2
- 反射技术
- Linux2.6内核调度器介绍
- org.hibernate.exception.SQLGrammarException
- 游戏引擎十大核心竞争力 之 (一);场景分割(alpha纯预览版、持续更新)
- 一些资源下载