Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Application loses focus

Application loses focus

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tommy Svensson
    wrote on last edited by
    #1

    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

    L _ 2 Replies Last reply
    0
    • T Tommy Svensson

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • T Tommy Svensson

        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

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        Try BringWindowToTop function.

        «_Superman_» I love work. It gives me something to do between weekends.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups