2012-06-05 17 views
7

Mam problem z programem, który traci fokus. To nie jest mój program. Jak mogę napisać drugi program, aby ustawić fokus na to okno co 1-2 sekundy? Czy można to zrobić?Jak ustawić ostrość na inne okno?

+0

Czy chcesz, aby fokus przełączał się między Twoim programem a drugim programem co sekundę? Czy w twoim zgłoszeniu chciałbyś przynieść drugi program na przód co 2 sekundy (na wypadek, gdyby znów się cofnął)? – Faraday

+0

Czy jest to program (inny proces programowy) czy ur forma dziecka? –

+0

jego inny program i chcę, aby mój program przyniósł tylko to na ostrości ... – Endiss

Odpowiedz

8

Można użyć następujących Win32 API, jeśli chcesz wnieść jakiś inny program/procesu

 [DllImport("coredll.dll")] 
     static extern bool SetForegroundWindow (IntPtr hWnd); 

     private void BringToFront(Process pTemp) 
     { 
      SetForegroundWindow(pTemp.MainWindowHandle); 
     } 
+11

W systemie Windows powinieneś użyć 'user32.dll', ponieważ' coredll.dll' jest dla Windows Mobile! –

2

użyj spy ++ lub innych narzędzi ui aby znaleźć nazwę klasy okna chcesz ustawić ostrość, powiedzieć jej: focusWindowClassName . Następnie dodaj poniższe funkcje:

[DllImport("USER32.DLL")] 
public static extern bool SetForegroundWindow(IntPtr hWnd); 

[System.Runtime.InteropServices.DllImport("User32.dll")] 
public static extern bool ShowWindow(IntPtr handle, int nCmdShow); 

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] 
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 

Then: 

IntPrt hWnd = FindWindow("focusWindowClassName", null); // this gives you the handle of the window you need. 

// then use this handle to bring the window to focus or forground(I guessed you wanted this). 

// sometimes the window may be minimized and the setforground function cannot bring it to focus so: 

/*use this ShowWindow(IntPtr handle, int nCmdShow); 
*there are various values of nCmdShow 3, 5 ,9. What 9 does is: 
*Activates and displays the window. If the window is minimized or maximized, *the system restores it to its original size and position. An application *should specify this flag when restoring a minimized window */ 

ShowWindow(hWnd, 9); 
//The bring the application to focus 
SetForegroundWindow(hWnd); 

// you wanted to bring the application to focus every 2 or few second 
// call other window as done above and recall this window again. 
Powiązane problemy