Re: Switch between active windows (different programs)
"William DePalo [MVP VC++]" wrote:
"Joachim" <Joachim@discussions.microsoft.com> wrote in message
news:61D28A56-7FEC-4DB0-9767-4D67289852ED@microsoft.com...
Is there a way to grab a handle or something else to be able to select and
bring to front (set as active window) a window from another program (which
you yourself is not the author of?
Well, maybe.
You need a window handle which usually can be gotten with FindWindow() or
EnumWindows(). Armed with that you can call SetWindowPos() to position where
you want in the Z-order.
Once upon a time, you could call SetForegroundWindow() which would both
bring the window to the fore and get it the keyboard focus. Since '98 and
2K, the function will instead flash the "button" associated with the target
window rather than set the focus.
Regards,
Will
Thanks Will,
I did try the following small program
#include "stdafx.h"
#include <iostream>
#include <afxwin.h> //Req for CWnd
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM)
{
CString s;
CWnd::FromHandle(hwnd)->GetWindowText(s);
_tprintf(_T("Window Title: %s\n"), (LPCTSTR) s);
if (s == _T("Microsoft Outlook"))
{
RECT l_r;
CWnd::FromHandle(hwnd)->GetWindowRect(&l_r);
// CWnd::FromHandle(hwnd)->SetWindowPos(&CWnd::wndTop, l_r.left, l_r.top,
l_r.right - l_r.left, l_r.top - l_r.bottom, SWP_SHOWWINDOW);
BOOL l_res = CWnd::FromHandle(hwnd)->SetWindowPos(&CWnd::wndTop, 0, 0,
500, 500, SWP_SHOWWINDOW);
if (l_res == FALSE)
{
int a = 2;
}
}
return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
EnumWindows(EnumWindowsProc, NULL);
char a;
std::cin >> a;
return 0;
}
and the SetWindowPos function succeeded for Microsoft Outlook, but it didn't
put the outlook window up front... :(