2012-02-17 2 views
0

안녕하세요 저는 Speific 프로그램을 열 때 Aero를 사용하지 않도록 설정하고 Speific 프로그램을 닫고 Aero를 다시 사용하도록 설정하려고합니다.C# cant 프로세스가 프로세스 목록에 있거나 없으면 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 모두 ​​실행됩니다.

내가 뭘 잘못 했니?

감사의 말 전진.

답변

1

for 루프가 올바르지 않습니다. 각 프로세스 이름을 하나씩 확인하고 있습니다. 따라서 어떤 프로세스가 마지막으로 발생하는지에 따라 달라집니다. "chrome"이 프로세스 목록의 중간에 있다면 EnableComposition(false)을 호출하고 for 루프를 통해 다음 반복시 EnableComposition(true)이라고 부릅니다. 이 대신 작동해야처럼

뭔가 :

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

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

아, didnt 한 통지, 당신은 이제 완벽하게 작동합니다 감사합니다! – Svante

+0

@ user1216303 -이 대답을 거부했습니다. 이 문제에 새로운 문제가 있습니까? – Justin

+0

죄송합니다. 클릭 한 적이 없습니다. – Svante