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. MFC: How to set the focus to the previously focused window?

MFC: How to set the focus to the previously focused window?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++tutorial
5 Posts 5 Posters 1 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.
  • E Offline
    E Offline
    Erik
    wrote on last edited by
    #1

    Hi, I need to set the focus to the window that had the focus before my dialog was first selected. To be more precise: as soon as the user clicks a button in my dialog, I want to set the focus to the window that had the focus before the user clicked in my dialog. I also want to be able to realize if that window has been closed in the meantime. What is the best way to achieve that? Thanks alot for any suggestions.

    T H 2 Replies Last reply
    0
    • E Erik

      Hi, I need to set the focus to the window that had the focus before my dialog was first selected. To be more precise: as soon as the user clicks a button in my dialog, I want to set the focus to the window that had the focus before the user clicked in my dialog. I also want to be able to realize if that window has been closed in the meantime. What is the best way to achieve that? Thanks alot for any suggestions.

      T Offline
      T Offline
      tagopi
      wrote on last edited by
      #2

      hi store the handle to window for the previous window in memory, recall this hwnd to set focus whenever you want. "I also want to be able to realize if that window has been closed" use IsWindowVisible option for this. Regards, A. Gopinath.

      M 1 Reply Last reply
      0
      • T tagopi

        hi store the handle to window for the previous window in memory, recall this hwnd to set focus whenever you want. "I also want to be able to realize if that window has been closed" use IsWindowVisible option for this. Regards, A. Gopinath.

        M Offline
        M Offline
        msr_codeproject
        wrote on last edited by
        #3

        Hi, adding to Gopinath's point you can use FindWindow API also. Refer it in msdn. Regards msr

        1 Reply Last reply
        0
        • E Erik

          Hi, I need to set the focus to the window that had the focus before my dialog was first selected. To be more precise: as soon as the user clicks a button in my dialog, I want to set the focus to the window that had the focus before the user clicked in my dialog. I also want to be able to realize if that window has been closed in the meantime. What is the best way to achieve that? Thanks alot for any suggestions.

          H Offline
          H Offline
          Hans Dietrich
          wrote on last edited by
          #4

          Erik wrote:

          I want to set the focus to the window that had the focus

          You can handle the WM_SETFOCUS message. One of its parameters is the handle to the window that has lost the keyboard focus.

          Erik wrote:

          I also want to be able to realize if that window has been closed

          You can use the IsWindow() function to determine if the window has been closed (i.e., destroyed).

          Best wishes, Hans


          [Hans Dietrich Software]

          E 1 Reply Last reply
          0
          • H Hans Dietrich

            Erik wrote:

            I want to set the focus to the window that had the focus

            You can handle the WM_SETFOCUS message. One of its parameters is the handle to the window that has lost the keyboard focus.

            Erik wrote:

            I also want to be able to realize if that window has been closed

            You can use the IsWindow() function to determine if the window has been closed (i.e., destroyed).

            Best wishes, Hans


            [Hans Dietrich Software]

            E Offline
            E Offline
            Emilio Garavaglia
            wrote on last edited by
            #5

            Hans Dietrich wrote:

            You can use the IsWindow() function to determine if the window has been closed (i.e., destroyed).

            Although this works in the most of the case, it is not "safe": when a window is destroyed its handle becomes invalid and the is ID (the handle value) can be reused by the system for another subsequently created window. If you don't look at all that while happening, IsWindow may return TRUE, but the window may not be the one you intended. (I mean: "my name (handle) is the same of my grandfather, I was born after his death, and I live in what it was his house. But I'm not him"! But if you send a letter to him, I'll read it as mine and, hopefully, I can understand the misleading only from the context".)

            2 bugs found. > recompile ... 65534 bugs found. :doh:

            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