C#如果进程是否在进程列表中,则无法禁用或启用Aero
嗨,我正在尝试修复当我打开一个特定程序时,aero应该被禁用,当特定程序关闭时,我希望aero再次被启用 我的代码:C#如果进程是否在进程列表中,则无法禁用或启用Aero,c#,aero,C#,Aero,嗨,我正在尝试修复当我打开一个特定程序时,aero应该被禁用,当特定程序关闭时,我希望aero再次被启用 我的代码: { const uint DWM_EC_DISABLECOMPOSITION = 0; const uint DWM_EC_ENABLECOMPOSITION = 1; [DllImport("dwmapi.dll", EntryPoint = "DwmEnableComposition")] extern static uint Dwm
{
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
我做错了什么
提前感谢。您的
for
循环不正确。您正在逐个检查每个进程名称。因此,这取决于哪个过程恰好排在最后。如果“Chrome”位于进程列表的中间,您将调用<代码> EnabeLogMo位置(false),并且在下一次迭代中,通过<代码>对于循环,您将调用<代码> EnabeLogMo位置(true)< /C> >
这样的方法应该可以奏效:
bool processFound = false;
foreach (Process proc in procs)
{
if (proc.ProcessName.Contains("chrome"))
{
processFound = true;
}
}
if (processFound)
{
EnableComposition(false);
}
else
{
EnableComposition(true);
}
@user1216303-我注意到你不接受这个答案。你有什么新问题吗?