无论进程是否在进程列表中,C# 都无法禁用或启用 Aero
您好,我正在尝试修复以下问题:当我打开特定程序时,aero 应该被禁用,而当特定程序关闭时,我希望再次启用 aero。
我的代码:
{
const uint DWM_EC_DISABLECOMPOSITION = 0;
const uint DWM_EC_ENABLECOMPOSITION = 1;
[DllImport("dwmapi.dll", EntryPoint = "DwmEnableComposition")]
extern static uint DwmEnableComposition(uint compositionAction);
public Form1()
{
InitializeComponent();
}
int count = 1;
public static bool EnableComposition(bool enable)
{
try
{
if (enable)
{
DwmEnableComposition(DWM_EC_ENABLECOMPOSITION);
}
else
{
DwmEnableComposition(DWM_EC_DISABLECOMPOSITION);
}
return true;
}
catch
{
return false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
Process[] procs = Process.GetProcesses();
foreach (Process proc in procs)
{
string chrome = "chrome";
string list;
list = proc.ProcessName;
if (list.Contains(chrome))
{
EnableComposition(false);
}
else if(!list.Contains(chrome))
{
EnableComposition(true);
}
}
}
}
问题:如果程序打开,它在 if 语句中同时运行 true 和 false。
我做错了什么?
提前致谢。
Hi I am trying to fix that when I open a speific program aero should been disable and when the speific program close I want the aero be enabled again.
My code:
{
const uint DWM_EC_DISABLECOMPOSITION = 0;
const uint DWM_EC_ENABLECOMPOSITION = 1;
[DllImport("dwmapi.dll", EntryPoint = "DwmEnableComposition")]
extern static uint DwmEnableComposition(uint compositionAction);
public Form1()
{
InitializeComponent();
}
int count = 1;
public static bool EnableComposition(bool enable)
{
try
{
if (enable)
{
DwmEnableComposition(DWM_EC_ENABLECOMPOSITION);
}
else
{
DwmEnableComposition(DWM_EC_DISABLECOMPOSITION);
}
return true;
}
catch
{
return false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
Process[] procs = Process.GetProcesses();
foreach (Process proc in procs)
{
string chrome = "chrome";
string list;
list = proc.ProcessName;
if (list.Contains(chrome))
{
EnableComposition(false);
}
else if(!list.Contains(chrome))
{
EnableComposition(true);
}
}
}
}
Problem: If the program is open it runs both true and false in the if statement.
What have I do wrong?
Thanks In Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
for
循环不正确。您正在一一检查每个进程名称。所以这取决于哪个过程恰好是最后发生的。如果“chrome”位于进程列表的中间,您将调用EnableComposition(false)
,并且在for
循环的下一次迭代中,您将调用EnableComposition (正确)
。像这样的东西应该起作用:
Your
for
loop is not correct. You are checking each process name one by one. So it depends on which process happens to come last. If "chrome" is in the middle of the process list, you will callEnableComposition(false)
and on the next iteration through thefor
loop you will callEnableComposition(true)
.Something like this should work instead: