2012-06-05 22 views
7

Tôi gặp sự cố với chương trình mất tiêu điểm. Nó không phải là chương trình của tôi. Làm thế nào tôi có thể viết một chương trình thứ hai để tập trung vào cửa sổ đó sau mỗi 1-2 giây? Có thể làm điều đó không?Làm cách nào để đặt trọng tâm sang cửa sổ khác?

+0

Bạn có muốn tập trung chuyển đổi giữa chương trình của bạn và chương trình thứ hai khác sau mỗi giây không? Hoặc trong ứng dụng của bạn sẽ muốn đưa chương trình khác lên phía trước cứ 2 giây một lần (trong trường hợp nó đã quay trở lại lần nữa)? – Faraday

+0

Đây có phải là chương trình (quy trình chương trình khác) hoặc hình thức trẻ em không? –

+0

chương trình khác biệt của nó và tôi muốn chương trình của tôi chỉ tập trung vào tập trung ... – Endiss

Trả lời

8

Bạn có thể sử dụng sau Win32 API nếu bạn muốn mang lại một số chương trình khác/quá trình

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

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

Trên Windows, bạn nên sử dụng 'user32.dll', vì' coredll.dll' dành cho Windows Mobile! –

2

sử dụng gián điệp ++ hoặc các công cụ ui khác để tìm ra tên lớp của cửa sổ bạn muốn tập trung, nói nó: focusWindowClassName . Sau đó, thêm các chức năng dưới đây:

[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. 
Các vấn đề liên quan