Win32 程序在启动时激活前一个启动程序的窗口
UWP 程序天生单实例。当然,新 API (10.0.17134)开始也提供了多实例功能。不过,传统 Win32 程序可就要自己来控制单实例了。
本文介绍简单的几个 Win32 方法调用,使 Win32 程序也支持单实例。
激活之前进程的窗口
我们可以通过进程名称找到此前已经启动过的进程实例,如果发现,就激活它的窗口。
[STAThread]
static void Main(string[] args)
{
var current = Process.GetCurrentProcess();
var process = Process.GetProcessesByName(current.ProcessName).FirstOrDefault(x => x.Id != current.Id);
if (process != null)
{
var hwnd = process.MainWindowHandle;
ShowWindow(hwnd, 9);
return;
}
// 启动自己的主窗口,此部分代码省略。
}
[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hwnd, uint nCmdShow);
你一定觉得那个 9 很奇怪,它是多个不同的 nCmdShow 的值:
- 0
Hide - 1
Minimized - 2
Maximized - 9
Restore
另外,找到的窗口此时可能并不处于激活状态。例如在 Windows 10 中,此窗口可能在其他桌面上。那么我们需要添加额外的代码将其显示出来。
在前面的 ShowWindow 之后,再调用一下 SetForegroundWindow 即可将其激活到最前面来。如果在其他桌面,则会切换到对应的桌面。
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
var hwnd = process.MainWindowHandle;
ShowWindow(hwnd, 9);
SetForegroundWindow(hwnd);
找到并激活窗口
以上方法适用于普通的主窗口。然而当窗口并不是进程的主窗口,或者 ShowInTaskBar 设为了 false 的时候就不生效了(此时窗口句柄会改变)。
于是,我们需要改用其他的方式来查找窗口。
[STAThread]
static void Main(string[] args)
{
var hwnd = FindWindow(null, "那个窗口的标题栏文字");
if (hwnd != IntPtr.Zero)
{
ShowWindow(hwnd, 9);
return;
}
// 启动自己的主窗口,此部分代码省略。
}
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
参考资料
- Controlling Window State Of Other Applications using C#
- c# - How to show/hide an application with Visible and ShowInTaskBar as false - Stack Overflow
- ShowWindowAsync function (Windows)
- How do I maximize/minimize applications programmatically in C#?
Win32 程序在启动时激活前一个启动程序的窗口的更多相关文章
- WinCE C#程序,控制启动时仅仅能启动一个程序,使用相互排斥量来实现,该实现方法測试通过
</pre><pre code_snippet_id="430174" snippet_file_name="blog_20140718_5_46349 ...
- GNOME启动时激活NumLock(小键盘数字锁定)
首先下载numlockx官方源提供的安装包,解压后进入目录运行终端,切换到root账户执行以下命令: python ./setup.py 然后依次点击GNOME菜单项上的“系统->首选项-> ...
- tomcat启动时非常慢,启动时 一直卡在Root WebApplicationContext: initialization completed(转)
转载地址:https://www.cnblogs.com/youxin/p/8793554.html 使用方式: JAVA_OPTS="-Djava.awt.headless=true -D ...
- tomcat启动时非常慢,启动时 一直卡在Root WebApplicationContext: initialization completed
每次重启自己的服务tomcat都需要卡住很长时间,每次都是日志停在 Root WebApplicationContext: initialization completed in 744 ms这个地方 ...
- 关于MyEclipse启动时的插件启动(Maven4MyEclipse)
在myEclipse的应用中有许多插件在开发的时候都用不到,那么,这些插件在启动myEclipse的时候一起启动的越少越好了 Maven4Myeclipse update 每当启动myEclipse的 ...
- hadoop集群启动时DataNode节点启动失败
错误日志如下: ************************************************************/ 2018-03-07 18:57:35,121 INFO o ...
- Tomcat启动时一闪而过,看不多错误信息解决方案
转自:https://wangxh89.iteye.com/blog/1824806 有时Tomcat的启动窗口一闪而过,根本就看不出启动过程中发生了什么错误.这中间的原因有好多种,最常见的解决办法就 ...
- iOS 应用程序启动时要做什么
当您的应用程序启动(无论是在前台或后台),使用您的应用程序委托application:willFinishLaunchingWithOptions:和application:didFinishLaun ...
- 当程序以Windows Services形式启动时当前路径不对
当程序以Windows Services形式启动时当前路径不对 @(操作系统)[博客|dotNet] 很多时候我们需要将我们的程序写成利用Windows服务的形式来让它能够自启动.今天遇到一个问题,当 ...
随机推荐
- codeforces GYM 100971F 公式题或者三分
F. Two Points time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- PHP处理MySQL事务代码
php使用mysqli进行事务处理 <?php$db = new mysqli("localhost","root",""," ...
- git bash 出显错误不能用,怎么解决
解决方法: 好像就是64的会出问题,其实32位的git也可以安装在64位的系统上. 将你64位的git卸掉了后,下载一个32位的git安装,就可以正常使用了, 当然,你的32位的出了错,卸了后也这样处 ...
- SpringSecurity——基于Spring、SpringMVC和MyBatis自定义SpringSecurity权限认证规则
本文转自:https://www.cnblogs.com/weilu2/p/springsecurity_custom_decision_metadata.html 本文在SpringMVC和MyBa ...
- HDU 2795 线段树单点更新
Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- nyoj1015——二分图染色
二部图 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 二部图又叫二分图,我们不是求它的二分图最大匹配,也不是完美匹配,也不是多重匹配,而是证明一个图是不是二部图.证 ...
- linux杀毒软件clamav安装与使用
#clamav安装与使用 ###第一步:Clamav下载http://www.clamav.net/downloads wget http://www.clamav.net/downloads/pro ...
- my.cnf 参数说明
[mysql] prompt="\\u@\\h:\p \\R:\\m:\\s [\\d]>" The prompt command reconfigures the de ...
- shiro权限定义的三种方法
1.在配置文件中定义 <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFa ...
- 小练习:Two Sum
1.example Given nums = [, , , ], target = , Because nums[] + nums[] = + = , , ]. 2.solve class Solut ...