Application loses focus
-
Hi! I have an application which sits in the system tray globally listening for a combination key press event (CTRL+ALT+T). Instead of opening the app by right clicking the app icon in the tray bar (clicking Open), I would like to open the app window upon CTRL+ALT+T. When that happens, sometimes (more often than not) the window loses focus/gets inactivated and I manually need to click it to regain focus. What else besides the following lines of code can be done to force the app foreground, topmost, focused and active?
((CMyAppDlg*) AfxGetApp()->GetMainWnd())->ShowWindow(SW_SHOW);
((CMyAppDlg*) AfxGetApp()->GetMainWnd())->SetFocus();
((CMyAppDlg*) AfxGetApp()->GetMainWnd())->SetForegroundWindow();
Thx! /T
-
Hi! I have an application which sits in the system tray globally listening for a combination key press event (CTRL+ALT+T). Instead of opening the app by right clicking the app icon in the tray bar (clicking Open), I would like to open the app window upon CTRL+ALT+T. When that happens, sometimes (more often than not) the window loses focus/gets inactivated and I manually need to click it to regain focus. What else besides the following lines of code can be done to force the app foreground, topmost, focused and active?
((CMyAppDlg*) AfxGetApp()->GetMainWnd())->ShowWindow(SW_SHOW);
((CMyAppDlg*) AfxGetApp()->GetMainWnd())->SetFocus();
((CMyAppDlg*) AfxGetApp()->GetMainWnd())->SetForegroundWindow();
Thx! /T
The behavior of the SetForegroundWindow Function[^] can be affected by SPI_GETFOREGROUNDLOCKTIMEOUT as described in the SystemParametersInfo Function[^] See if this works for you:
HWND hWnd = GetForegroundWindow(); DWORD dwThread = GetCurrentThreadId(); DWORD dwProcessID = GetWindowThreadProcessId(hWnd,NULL); if(hWnd > 0) { AttachThreadInput(dwProcessID,dwThread,TRUE); (AfxGetApp()->GetMainWnd())->SetForegroundWindow(); AttachThreadInput(dwProcessID,dwThread,FALSE); }
Best Wishes, -David Delaune -
Hi! I have an application which sits in the system tray globally listening for a combination key press event (CTRL+ALT+T). Instead of opening the app by right clicking the app icon in the tray bar (clicking Open), I would like to open the app window upon CTRL+ALT+T. When that happens, sometimes (more often than not) the window loses focus/gets inactivated and I manually need to click it to regain focus. What else besides the following lines of code can be done to force the app foreground, topmost, focused and active?
((CMyAppDlg*) AfxGetApp()->GetMainWnd())->ShowWindow(SW_SHOW);
((CMyAppDlg*) AfxGetApp()->GetMainWnd())->SetFocus();
((CMyAppDlg*) AfxGetApp()->GetMainWnd())->SetForegroundWindow();
Thx! /T
Try
BringWindowToTop
function.«_Superman_» I love work. It gives me something to do between weekends.