顶部的 UAC 窗口 [英] UAC Window On Top
问题描述
我似乎在这里陷入了死胡同.我有一个执行以下操作的项目:
I seem to be running into a dead end here. I have a project that does the following:
- 只是运行并停留在通知区域
- 当用户按下鼠标中键时,它会显示一个表单
- 然后用户可以从表单中选择任何内容,并且很高兴.
这工作得很好,但不起作用的是当表单显示时,它不会激活.
This is working just fine, however what is NOT working is that when the form is shown, it does NOT activate.
现在,在我将 app.manifest 修改为 requireAdmin
之前,它运行良好.每当显示表单时,它就会活跃起来.
Now, prior to me modifying the app.manifest to requireAdmin
, it was working fine. Any time the form was shown, boom it was active.
但是现在我以管理员身份运行表单,考虑到我需要能够用它控制我们的其他应用程序之一(也以管理员身份运行),表单出现得很好,但没有不激活.
But now that i'm running the form as Administrator, given that I need to be able to control one of our other applications with it (that is ran as admin as well), the form comes up just fine, but doesn't activate.
我试过:- TopMost = true
在 Form_Load 方法中- 使用 Form_Load 上的表单句柄调用 SetForegroundWindow
- 在 Form_Load 上调用 ShowDialog
并在构建表单时尝试它.- 在 Form_Load 以及在构建表单时调用 Activate
I have tried:
- TopMost = true
inside of the Form_Load method
- Calling SetForegroundWindow
with the forms handle on Form_Load
- Calling ShowDialog
both on Form_Load and also tried it when the form was built.
- Calling Activate
on Form_Load as well as when the form was built
这是我的布局:
- 程序运行,由于清单需要 UAC 权限,用户同意,然后开始
- 只出现一个通知图标,显示一个漂亮的小气泡,表示它正在运行
- Program.cs 监控键盘/鼠标挂钩
- 当鼠标中键或任何键盘钩子被触发时,它会创建一个新的表单对象
- 设置表单出现的位置
- 然后最后显示表单.
同样,如果 app.manifest 中没有 UAC 和 requireAdmin
,这也能正常工作.但是一旦它以管理员身份运行,不,就不会停留在前面.
Again, without UAC and requireAdmin
inside of the app.manifest, this works fine. But once it is running as admin, nope, wont stay in front.
我知道 MS 使 SetForegroundWindow
的要求在更高版本的 Windows(Vista、7、8)中更加严格,但我真的需要能够让这个表单显示为最顶层, 活动窗口(如上下文菜单).
I know that MS has made the SetForegroundWindow
requirements much stricter with later versions of windows (Vista, 7, 8), but i really need to be able to make this form show as the top most, active window (like a context menu).
我怎样才能正确地做到这一点?
How can i do that properly?
推荐答案
你可以把你的程序分成两部分,一个需要管理员权限的非管理员程序坐在托盘中观察中间按钮,另一半程序需要管理员权限按下鼠标按钮时启动.然后,未升高的一半可以根据需要调用 SetForegroundWindow
或 AllowSetForegroundWindow
.
You could split your program into two, a non-admin requiring half that sits in the tray and watches for the middle button, and the half that requires admin permission that you launch when the mouse button is pressed. The non-elevated half could then call SetForegroundWindow
or AllowSetForegroundWindow
as needed.
如果您想避免每次都出现 UAC 提示,您可以通过 CoCreateInstanceAsAdmin
方法缓存 COM 提升对象并重复使用它.
If you want to avoid a UAC prompt every time you can cache a COM elevation object via the CoCreateInstanceAsAdmin
method and use it repeatedly.
这篇关于顶部的 UAC 窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!