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. GetActiveWindow()

GetActiveWindow()

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.
  • I Offline
    I Offline
    iayd
    wrote on last edited by
    #1

    Hi all, I faced with an intresting event about GetActiveWindow() method. When I call the code blok below by clicking to a button, I can see the messagebox as "asdasd". But when I call the code blok in a worker thread I cannot see the messagebox.

    CWnd *wnd;
    wnd = GetActiveWindow();
    if(pDlgb == wnd)
    MessageBox(_T("asdasd"));

    What is the reason for that? Is there any body who can give me an advice about getting the active window in a worker thread? Thanks, ibrahim

    M 1 Reply Last reply
    0
    • I iayd

      Hi all, I faced with an intresting event about GetActiveWindow() method. When I call the code blok below by clicking to a button, I can see the messagebox as "asdasd". But when I call the code blok in a worker thread I cannot see the messagebox.

      CWnd *wnd;
      wnd = GetActiveWindow();
      if(pDlgb == wnd)
      MessageBox(_T("asdasd"));

      What is the reason for that? Is there any body who can give me an advice about getting the active window in a worker thread? Thanks, ibrahim

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Because MFC keeps internal table(s) of (CWnd) windows on a per-thread-basis. Therefore , the CWnd returned by GetActiveWindow() on a different thread can't possibly be the same pointer as your pDlgb. It will be a temporary CWnd object also attached to your dialog's HWND (assuming that window is actually the active window). Compare the HWNDs instead

      if(pDlgb->GetSafeHwnd() == wnd->GetSafeHwnd())

      Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      S 1 Reply Last reply
      0
      • M Mark Salsbery

        Because MFC keeps internal table(s) of (CWnd) windows on a per-thread-basis. Therefore , the CWnd returned by GetActiveWindow() on a different thread can't possibly be the same pointer as your pDlgb. It will be a temporary CWnd object also attached to your dialog's HWND (assuming that window is actually the active window). Compare the HWNDs instead

        if(pDlgb->GetSafeHwnd() == wnd->GetSafeHwnd())

        Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        It's a little more complicated than that, although it’s true that MFC's handle maps are thread specific. Here’s how MFC implements CWnd::GetActiveWindow:

        _AFXWIN_INLINE CWnd* PASCAL CWnd::GetActiveWindow()
        { return CWnd::FromHandle(::GetActiveWindow()); }

        Now the following is from MSDN on the ::GetActiveWindow[^] function: The GetActiveWindow function retrieves the window handle to the active window attached to the calling thread's message queue. So ::GetActiveWindow is also thread specific, although you could use the AttachThreadInput[^] to get around this.

        Steve

        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