我试图带来一个窗口前景。我正在使用此代码。但它不起作用。有人可以帮忙吗?
ShowWindowAsync(wnd.hWnd, SW_SHOW);
SetForegroundWindow(wnd.hWnd);
// Code from Karl E. Peterson, www.mvps.org/vb/sample.htm
// Converted to Delphi by Ray Lischner
// Published in The Delphi Magazine 55, page 16
// Converted to C# by Kevin Gale
IntPtr foregroundWindow = GetForegroundWindow();
IntPtr Dummy = IntPtr.Zero;
uint foregroundThreadId = GetWindowThreadProcessId(foregroundWindow, Dummy);
uint thisThreadId = GetWindowThreadProcessId(wnd.hWnd, Dummy);
if (AttachThreadInput(thisThreadId, foregroundThreadId, true))
{
BringWindowToTop(wnd.hWnd); // IE 5.5 related hack
SetForegroundWindow(wnd.hWnd);
AttachThreadInput(thisThreadId, foregroundThreadId, false);
}
if (GetForegroundWindow() != wnd.hWnd)
{
// Code by Daniel P. Stasinski
// Converted to C# by Kevin Gale
IntPtr Timeout = IntPtr.Zero;
SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, Timeout, 0);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, Dummy, SPIF_SENDCHANGE);
BringWindowToTop(wnd.hWnd); // IE 5.5 related hack
SetForegroundWindow(wnd.hWnd);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, Timeout, SPIF_SENDCHANGE);
}
代码解释
使窗口成为前景窗口需要的不仅仅是调用 SetForegroundWindow API。您必须首先确定前台线程并将其附加到您的窗口,使用 AttachThreadInput,然后调用 SetForegroundWindow。这样他们就可以共享输入状态。
首先我调用 GetForegroundWindow 来获取当前前景窗口的句柄。然后对 GetWindowThreadProcessId 的几次调用检索与当前前台窗口关联的线程以及我想要带到前台的窗口。如果这些线程相同,则只需调用 SetForegroundWindow 即可。否则,前台线程将附加到我带到前面的窗口,并与当前前台窗口分离。AttachThreadInput API 处理这个问题。
内容取自这里 谢谢。