发布于 2012-05-14 08:56:30
5楼
下面是将指定窗口句柄置前的项目函数:
BOOL ForceForegroundWindow(HWND hWnd)
{
#pragma code("user32.dll")
DWORD GetWindowThreadProcessId(
HWND hWnd, // handle to window
LPDWORD lpdwProcessId // address of variable for process identifier
);
BOOL AttachThreadInput(
DWORD idAttach, // thread to attach
DWORD idAttachTo, // thread to attach to
BOOL fAttach // attach or detach
);
HWND GetForegroundWindow(VOID);
BOOL SetForegroundWindow(
HWND hWnd // handle to window to bring to foreground
);
BOOL IsIconic(
HWND hWnd // handle to window
);
BOOL ShowWindow(
HWND hWnd, // handle to window
int nCmdShow // show state of window
);
#pragma code()
long int ThreadID1,ThreadID2;
BOOL bRet;
// Nothing to do if already in foreground.
if (hWnd == GetForegroundWindow()) return TRUE;
// First need to get the thread responsible for this window,
// and the thread for the foreground window.
ThreadID1=GetWindowThreadProcessId(GetForegroundWindow(),NULL);
ThreadID2=GetWindowThreadProcessId(hWnd,NULL);
// By sharing input state,thread share their concept of the active window.
if (ThreadID1 != ThreadID2)
{
AttachThreadInput(ThreadID1,ThreadID2,TRUE);
bRet = SetForegroundWindow(hWnd);
AttachThreadInput(ThreadID1,ThreadID2,FALSE);
}
else bRet = SetForegroundWindow(hWnd);
// Restore and repaint
if (IsIconic(hWnd))
ShowWindow(hWnd,SW_RESTORE);
else ShowWindow(hWnd,SW_SHOWNORMAL);
return bRet;
}
无论成与败,无论甜与苦,我还是我。