У меня проблема с программой, которая теряет фокус. Это не моя программа. Как я могу написать вторую программу для установки фокуса на это окно каждые 1-2 секунды? Можно ли это сделать?Как установить фокус в другое окно?
7
A
ответ
8
Вы можете использовать следующие Win32 API, если вы хотите, чтобы принести некоторые другие программы/процесса
[DllImport("coredll.dll")]
static extern bool SetForegroundWindow (IntPtr hWnd);
private void BringToFront(Process pTemp)
{
SetForegroundWindow(pTemp.MainWindowHandle);
}
+11
В Windows вы должны использовать 'user32.dll', потому что' coredll.dll' для Windows Mobile! –
2
использовать Spy ++ или других инструментов пользовательского интерфейса, чтобы найти имя класса окна вы хотите, чтобы сосредоточиться, сказать свое: focusWindowClassName , Затем добавьте следующие функции:
[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.
Вы хотите сказать, что вы хотите, чтобы фокус переключался между вашей программой и этой второй программой каждую секунду? Или в вашем приложении хотелось бы вывести другую программу на фронт каждые 2 секунды (в случае, если она вернется обратно)? – Faraday
Это программа (другой программный процесс) или ур детская форма? –
его отличная программа, и я хочу, чтобы моя программа приносила только ее внимание ... – Endiss