2012-06-05 32 views
7

Tengo un problema con un programa que pierde el foco. No es mi programa ¿Cómo puedo escribir un segundo programa para establecer el foco en esa ventana cada 1-2 segundos? ¿Es posible hacer eso?¿Cómo establecer el foco en otra ventana?

+0

¿Está diciendo que desea cambiar el enfoque entre su programa y este otro segundo programa cada segundo? ¿O en su aplicación le gustaría llevar el otro programa al frente cada 2 segundos (en caso de que haya pasado hacia atrás)? – Faraday

+0

¿Es un programa (proceso de programa diferente) o tu forma de niño? –

+0

es un programa diferente y quiero que mi programa lo traiga solo al foco ... – Endiss

Respuesta

8

permite utilizar las siguientes API Win32 si usted quiere traer a algún otro programa/proceso

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

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

En Windows, debe usar 'user32.dll', porque' coredll.dll' es para Windows Mobile. –

2

uso espía ++ u otras herramientas de interfaz de usuario para encontrar el nombre de la clase de la ventana que desea enfocar, decir que es: focusWindowClassName . A continuación, agregue las funciones siguientes:

[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. 
Cuestiones relacionadas