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#
  4. Get Previous Window Handle?

Get Previous Window Handle?

Scheduled Pinned Locked Moved C#
questionjson
7 Posts 4 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.
  • K Offline
    K Offline
    Kasdoffe
    wrote on last edited by
    #1

    If my program is running and I start a program such as Internet Explorer, how do I get the window handle of IE when my programs is activated and gets focus again? I want to send keystrokes to differing programs. Currently, the only window I can get is the foreground window using the GetForeGroundWindow API when my program initially starts. If a new program starts while mine is running, in order to send keystrokes to it, I need to close my program and restart it, so I can get the new foreground window. Make sense? Any ideas?

    S 1 Reply Last reply
    0
    • K Kasdoffe

      If my program is running and I start a program such as Internet Explorer, how do I get the window handle of IE when my programs is activated and gets focus again? I want to send keystrokes to differing programs. Currently, the only window I can get is the foreground window using the GetForeGroundWindow API when my program initially starts. If a new program starts while mine is running, in order to send keystrokes to it, I need to close my program and restart it, so I can get the new foreground window. Make sense? Any ideas?

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      You can enumerate through the currently running processes and use the Process.MainWindowHandle[^] property to get the window handle. Something like

      Process[] processes = Process.GetProcessesByName("notepad");
      foreach(Process p in processes)
      {
      IntPtr handle = p.MainWindowHandle;
      }

      Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      K 1 Reply Last reply
      0
      • S S Senthil Kumar

        You can enumerate through the currently running processes and use the Process.MainWindowHandle[^] property to get the window handle. Something like

        Process[] processes = Process.GetProcessesByName("notepad");
        foreach(Process p in processes)
        {
        IntPtr handle = p.MainWindowHandle;
        }

        Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        K Offline
        K Offline
        Kasdoffe
        wrote on last edited by
        #3

        That doesn't help though. Within my code, I don't know which program just started or which program just had focus. It could be notepad, it could be IE, it could Vis Studio, or FireFox. It doesn't matter what the program is, I want the window handle of the window that had focus prior to my program.

        D 1 Reply Last reply
        0
        • K Kasdoffe

          That doesn't help though. Within my code, I don't know which program just started or which program just had focus. It could be notepad, it could be IE, it could Vis Studio, or FireFox. It doesn't matter what the program is, I want the window handle of the window that had focus prior to my program.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          Unless your application is already running when the focus changes, there's no way to tell which application just had the focus. Your application will start way too late to get any notification of which window just lost the focus. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          L 1 Reply Last reply
          0
          • D Dave Kreskowiak

            Unless your application is already running when the focus changes, there's no way to tell which application just had the focus. Your application will start way too late to get any notification of which window just lost the focus. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

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

            Yes, exactly. It will already be running. Here's the example again. My program is running, but doesn't have focus. I start IE or MS Word and do blah blah blah in it. I click back or focus my program. What's the handle of the app that just had focus before mine? -- modified at 9:11 Wednesday 12th October, 2005

            D 1 Reply Last reply
            0
            • L Lost User

              Yes, exactly. It will already be running. Here's the example again. My program is running, but doesn't have focus. I start IE or MS Word and do blah blah blah in it. I click back or focus my program. What's the handle of the app that just had focus before mine? -- modified at 9:11 Wednesday 12th October, 2005

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              There is no API call you can make to give you the last window that had the focus. The only thing I can think of is to override the WndProc procedure in your main window and handle the WM_ACTIVATE[^] message. You'll have to track the window handle of the window that is being deactivated, then get the parent window of that window handle until you reach the top-level window handle of the application. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              K 1 Reply Last reply
              0
              • D Dave Kreskowiak

                There is no API call you can make to give you the last window that had the focus. The only thing I can think of is to override the WndProc procedure in your main window and handle the WM_ACTIVATE[^] message. You'll have to track the window handle of the window that is being deactivated, then get the parent window of that window handle until you reach the top-level window handle of the application. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                K Offline
                K Offline
                Kasdoffe
                wrote on last edited by
                #7

                I couldn't seem to get that to work. However, I did find another solution. I found a class that receives all mouse and keyboard events from the system. I hooked up a couple events and now my program is told when someone clicks the mouse or presses a key. Then I just GetForeGroundWindow to get the active window and change the IntPtr my program is using to know what has focus. Works great!

                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