C# Windows程序窗口置前台的几种方法
这个是从别的地方看来的,放我这里
第一种:SetForegroundWindow,这个方法时灵时不灵。有人说,在自己的程序里把自己的窗口之前一般就不灵,而置前其它程序的窗口就灵。我觉得这是有原因的:当一个程序运行这个函数的时候,我们可以想象,如果是用鼠标操作,那么当前窗口实际上正在操作,很可能在瞬间离开后,焦点又回来了,那么看上去更就没有吧其它窗口置前的效果,所以这个函数内部可能做了延迟,略等一下,在这个时间内,当前窗口不会被置前。就是这个时间,函数已经调用完成,所以看上去,自己的窗口并没有置前。
第二种:SwitchToThisWindow,这个方法很好使,还会把最小化的窗口复原。但是Windows说,这个函数以后可能不支持。但是我觉得可以放心使用,Windows当前一般就那几个版本,程序不会计划运行100年不升级吧。这个函数可以直接链接,但是XP以前的版本可能不行,所以,使用动态加载可能保险点。动态加载代码:
- typedef void (WINAPI*PROCSWITCHTOTHISWINDOW)(HWND,BOOL);
- PROCSWITCHTOTHISWINDOW SwitchToThisWindow;
- HMODULE hUser32 = GetModuleHandle(L"user32");
- SwitchToThisWindow = (PROCSWITCHTOTHISWINDOW)GetProcAddress(hUser32,"SwitchToThisWindow");
- SwitchToThisWindow(frame,1);
第三种方法:BringWindowToTop,这个函数比上一个功能少点,实质是调用SetWindowPos函数。
我的具体用法是这样子
//[DllImport("user32.dll")]
//public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll ", SetLastError = true)]
static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
//[DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
//public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
public const int SW_RESTORE = 9;
public static IntPtr formhwnd;
Process[] MyProcesses = Process.GetProcesses();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = @"D:\开发区工作\钢筋软件\开发资料\2017\源代码\MTC部分\廊坊凯博\KBMES\";
startInfo.FileName = "Client.exe";
bool temp = false;
foreach (Process MyProcess in MyProcesses)
{
if (MyProcess.ProcessName == "Client")
{
//IntPtr handle = MyProcess.MainWindowHandle;
//ShowWindow(handle,1);
temp = true;
SwitchToThisWindow(MyProcess.MainWindowHandle,true);
break;
}
}
if (temp == false)
Process.Start(startInfo);
后面还看到
SwitchToThisWindow这个是遗留函数,也就是微软为了和以前版本windows兼容而留下来的,以后的版本可能会被移除。
SetForegroundWindow就是推荐的函数。
SetActiveWindow这三者有什么区别和联系 SetActiveWindow不能激活后台程序,也就是窗口被隐藏了,不能用它激活。
ShowWindow的API函数是显示窗口,
它的第二个参数是决定窗体的状态的。
| Value | Meaning |
|---|---|
|
Minimizes a window, even if the thread that owns the window is not responding. This flag should only be used when minimizing windows from a different thread. |
|
Hides the window and activates another window. |
|
Maximizes the specified window. |
|
Minimizes the specified window and activates the next top-level window in the Z order. |
|
Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window. |
|
Activates the window and displays it in its current size and position. |
|
Sets the show state based on the SW_ value specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. |
|
Activates the window and displays it as a maximized window. |
|
Activates the window and displays it as a minimized window. |
|
Displays the window as a minimized window. This value is similar to SW_SHOWMINIMIZED, except the window is not activated. |
|
Displays the window in its current size and position. This value is similar to SW_SHOW, except that the window is not activated. |
|
Displays a window in its most recent size and position. This value is similar to SW_SHOWNORMAL, except that the window is not activated. |
|
Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time. |
好吧,这些都在C#用到了非托管类的API了,不知道VS里面的类有没有这样的功能。
C# Windows程序窗口置前台的几种方法的更多相关文章
- VS编程,WPF中,获取鼠标相对于当前程序窗口的坐标的一种方法
原文:VS编程,WPF中,获取鼠标相对于当前程序窗口的坐标的一种方法 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/ ...
- VC.【转】窗口置于前台并激活的方法
1.VC 窗口置于前台并激活的方法 - CSDN博客.html https://blog.csdn.net/oXunFeng/article/details/52681279 2.(http://ww ...
- windows下获取IP地址的两种方法
windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...
- Duilib改进窗口拖动,使整个窗口都能拖动两种方法(转载)
转载:http://www.cnblogs.com/XiHua/articles/3490490.html 转载:http://blog.csdn.net/lostspeed/article/deta ...
- 体温数据上传程序开发+获取时间的三种方法+DB Browser下载及安装
今天开始了体温上传程序的开发 今日所学: 获取时间 (21条消息) (转)安卓获取时间的三种方法_sharpeha的博客-CSDN博客_安卓获取时间 DB Browser安装教程 (20条消息) sq ...
- java web 程序---内置对象application的log方法的使用
application的主要方法里,有log方法,是日志文件里可以查看到信息的. 当老师写好代码后,他发现在tomact里的log目录下找不到信息,原因是:我们用myeclipse这个客户端软件,应该 ...
- 将Java程序作成exe文件的几种方法【转载】
看到网上有同志的介绍将Java程序作成exe文件的方法,写的不错,但是也许是这篇文章完成的时间比较早,许多内容已经不合适了.我在这里补充几条: 一.exe4j 说明:exe4j可以将Jar文件制作成e ...
- qt客户端程序使用svg图片资源的几种方法
直接使用svg格式文件资源的情况 1. 直接在UI控件属性面板中选择部分支持icon图标的控件的icon来源,这样图标可以显示 2.给toolbutton添加样式 qproperty-icon: ur ...
- C#程序读取MAC地址的五种方法(转)
public class GetMac { ///<summary> /// 根据截取ipconfig /all命令的输出流获取网卡Mac ///</summary> ///& ...
随机推荐
- 第21件事 资源支持离不开RACI表
十步法的第九步寻求资源支持.资源主要包括人力资源.物力资源和财力资源.人力资源,即需要多少人:物力资源,即需要多少软硬件设备:财力资源,即需要多少预算.根据产品或项目目标,资源估算时要考虑需要什么样的 ...
- C# 不添加WEB引用调用WSDL接口
在项目中添加WEB引用耦合度较高,更新时要更新引用,所以我建议不添加WEB引用调用WSDL接口,废话不多说,直接上代码 例如WSDL地址为:http://XXX.XX.XXX.XXX:9115/WsP ...
- EF架构获取数据时报错:The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. Do you want to correct the value?
场景: EF底层,获取完主表,点击按钮,添加主表,字表内容时,报以上错误 解决方案: 在EF文件的空白处右键--属性,将“应用延迟加载”,改为False
- 迁移桌面程序到MS Store(4)——桌面程序调用Win10 API
上一篇我们讨论了如何在转制的桌面程序中,通过StartupTask来实现转制版本的开机自启动.实际操作中,我们通过编辑Packaging工程中的Package.appxmanifest文件,来添加自启 ...
- Spring AOP 源码分析 - 创建代理对象
1.简介 在上一篇文章中,我分析了 Spring 是如何为目标 bean 筛选合适的通知器的.现在通知器选好了,接下来就要通过代理的方式将通知器(Advisor)所持有的通知(Advice)织入到 b ...
- Fusioncharts的数字格式化
1. 小数点位数格式化 <chart ... decimals='2' > Eg.数值12.432, 13.4 and 13,使用<chart ... decimals= ...
- Asp.net管线事件(页面请求周期)
在处理该请求时将由 HttpApplication 类执行以下事件. 希望扩展 HttpApplication 类的开发人员尤其需要注意这些事件. . 对请求进行验证,将检查浏览器发送的信息,并确定其 ...
- JAVA常见安全问题复现
地址来源于乌云知识库,作者z_zz_zzz 0x01 任意文件下载 web.xml的配置: <servlet> <description></description> ...
- TakePhoto实现拍照得到图片和从相册得到图片
在学郭霖大神的第一行代码的时候,学到利用相机拍照和从本地相册取照片的那一小节的时候,代码写出来但是出了很多问题,APP老是崩溃,一番百度最终还是没有找到解决办法 无奈只能用别人现成的轮子了,然后就发现 ...
- Jenkins邮箱设置
首先需要在系统管理--系统设置中设置系统管理员邮件地址: 然后在邮件通知里设置用户名密码,可以发生测试邮件 这里需要注意的是上面的系统管理员邮件地址必须和用户名一致 还需要注意的是测试邮件可能被当做垃 ...