如果 SetForegroundWindow 和 ShowWindowAsync 不起作用,如何设置前景窗口? [英] How can I set a foreground window if SetForegroundWindow and ShowWindowAsync doesn't work?

查看:30
本文介绍了如果 SetForegroundWindow 和 ShowWindowAsync 不起作用,如何设置前景窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 c# winform 应用程序.我已将 ShowInTaskbar 属性设置为 false,因为我不希望程序出现在任务栏中,但像这样 SetForegroundWindow 或 ShowWindowAsync 不起作用.

It a is a c# winform app. I've set ShowInTaskbar prop to false because I don't want the program to appear in taskbar, but like this SetForegroundWindow or ShowWindowAsync don't work.

推荐答案

Raymond Chen 在他的经典文章 前台激活权限就像爱情:你不能偷,它必须给你.粗略地说,你不能使用像 SetForegroundWindow 这样的调用来窃取前台激活,你需要从已经有前台激活的进程中获得它.

Raymond Chen explains the things the govern the rules in Windows around foreground activation in his classic article Foreground activation permission is like love: You can't steal it, it has to be given to you. Roughly speaking, you cannot steal foreground activation using a call like SetForegroundWindow, you need to be given it from a process which already has the foreground activation.

在您的情况下,当您在任务栏设置中显示时,任务栏能够为您的应用程序提供前台激活,因为当您单击窗口的任务栏图标时,这将(简要地)激活任务栏.如果您没有在任务栏中显示激活,您需要找到一种方法来拥有一个可以为您提供的应用程序.明确允许的一种方法是使用通知窗口(托盘)图标.

In your case, when you have show in taskbar set, the taskbar is able to give your application the foreground activation, because when you click on the taskbar icon for your window, this will (briefly) give the activation to the taskbar. If you don't have the activation shown in the taskbar, you need to find a way to have an application that does give it to you. One way that is explicitly allowed is to use a notification window (tray) icon.

这篇关于如果 SetForegroundWindow 和 ShowWindowAsync 不起作用,如何设置前景窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆