无论进程是否在进程列表中,C# 都无法禁用或启用 Aero

发布于 2025-01-06 18:30:45 字数 1420 浏览 1 评论 0原文

您好,我正在尝试修复以下问题:当我打开特定程序时,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小草泠泠 2025-01-13 18:30:45

您的 for 循环不正确。您正在一一检查每个进程名称。所以这取决于哪个过程恰好是最后发生的。如果“chrome”位于进程列表的中间,您将调用 EnableComposition(false),并且在 for 循环的下一次迭代中,您将调用 EnableComposition (正确)

像这样的东西应该起作用:

    bool processFound = false;
    foreach (Process proc in procs)
    {
        if (proc.ProcessName.Contains("chrome"))
        {
            processFound = true;
        }
    }

    if (processFound)
    {
        EnableComposition(false);
    }
    else
    {
        EnableComposition(true);
    }

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 call EnableComposition(false) and on the next iteration through the for loop you will call EnableComposition(true).

Something like this should work instead:

    bool processFound = false;
    foreach (Process proc in procs)
    {
        if (proc.ProcessName.Contains("chrome"))
        {
            processFound = true;
        }
    }

    if (processFound)
    {
        EnableComposition(false);
    }
    else
    {
        EnableComposition(true);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文