Handle to main window.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • objectref

    Handle to main window.

    Hi to all,

    is there a way to get the window handle of the main window of an application
    or process ?
    For example, if someone opens Microsoft Word, he gets a window so he/she can
    write text.
    Spy++ gives that this window is called _Wwg.

    How we can get a handle to this window assuming that we do not know
    beforehand the name of the process,
    we just want to find the handle of every application's main window ?
    (or the window that currently has the focus/caret).

    Is this possible ??

    Thanks a lot for any help,

    objectref


  • Stoitcho Goutsev \(100\) [C# MVP]

    #2
    Re: Handle to main window.

    Hi objectref,

    ..NET doesn't provide enumaration of windwos. However you can always use
    PInvoke and call one of the windows enumeration APIs.

    The only thing that you can do with .NET classes is to call
    Process.GetProc esses() method to get array of all processes currently
    started on the machine. Then for each process you can read the
    MainWindowHandl e property.

    However, in this way you can read one of the process' toplevel windows.
    Process may have more then one main windows. For example look at Windows and
    Internet Explorer application. They have only one process and a separate
    thread for each GUI. For these application you'll get only one of the main
    windows. Another example could be .NET application that runs more then one
    application domains, which of which has its own GUI.

    --

    Stoitcho Goutsev (100) [C# MVP]


    "objectref" <objectref@medi atrel.com> wrote in message
    news:%23Ej8zQ0c EHA.712@TK2MSFT NGP11.phx.gbl.. .[color=blue]
    > Hi to all,
    >
    > is there a way to get the window handle of the main window of an[/color]
    application[color=blue]
    > or process ?
    > For example, if someone opens Microsoft Word, he gets a window so he/she[/color]
    can[color=blue]
    > write text.
    > Spy++ gives that this window is called _Wwg.
    >
    > How we can get a handle to this window assuming that we do not know
    > beforehand the name of the process,
    > we just want to find the handle of every application's main window ?
    > (or the window that currently has the focus/caret).
    >
    > Is this possible ??
    >
    > Thanks a lot for any help,
    >
    > objectref
    >
    >[/color]


    Comment

    • objectref

      #3
      Re: Handle to main window.

      Hi Stoitcho,

      i know all this and that's why i am calling EnumChildWindow s based on the
      MainWindowHandl e of each process.
      The problem is that i want to call PostMessage and send some characters in
      the window that have the focus at that time, ie, the application's active
      window.

      Do you know some way of doing that ??

      Thanks a lot for your answer,

      objectref


      "Stoitcho Goutsev (100) [C# MVP]" <100@100.com> wrote in message
      news:eUmxwn0cEH A.688@TK2MSFTNG P11.phx.gbl...[color=blue]
      > Hi objectref,
      >
      > .NET doesn't provide enumaration of windwos. However you can always use
      > PInvoke and call one of the windows enumeration APIs.
      >
      > The only thing that you can do with .NET classes is to call
      > Process.GetProc esses() method to get array of all processes currently
      > started on the machine. Then for each process you can read the
      > MainWindowHandl e property.
      >
      > However, in this way you can read one of the process' toplevel windows.
      > Process may have more then one main windows. For example look at Windows[/color]
      and[color=blue]
      > Internet Explorer application. They have only one process and a separate
      > thread for each GUI. For these application you'll get only one of the main
      > windows. Another example could be .NET application that runs more then one
      > application domains, which of which has its own GUI.
      >
      > --
      >
      > Stoitcho Goutsev (100) [C# MVP]
      >
      >
      > "objectref" <objectref@medi atrel.com> wrote in message
      > news:%23Ej8zQ0c EHA.712@TK2MSFT NGP11.phx.gbl.. .[color=green]
      > > Hi to all,
      > >
      > > is there a way to get the window handle of the main window of an[/color]
      > application[color=green]
      > > or process ?
      > > For example, if someone opens Microsoft Word, he gets a window so he/she[/color]
      > can[color=green]
      > > write text.
      > > Spy++ gives that this window is called _Wwg.
      > >
      > > How we can get a handle to this window assuming that we do not know
      > > beforehand the name of the process,
      > > we just want to find the handle of every application's main window ?
      > > (or the window that currently has the focus/caret).
      > >
      > > Is this possible ??
      > >
      > > Thanks a lot for any help,
      > >
      > > objectref
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Adam Clauss

        #4
        Re: Handle to main window.

        You will have to make calls to the Win32 API using PInvoke to call PostMessage. See:


        --
        Adam Clauss
        cabadam@tamu.ed u

        "objectref" <objectref@medi atrel.com> wrote in message news:%23i15q$0c EHA.3128@TK2MSF TNGP11.phx.gbl. ..[color=blue]
        > Hi Stoitcho,
        >
        > i know all this and that's why i am calling EnumChildWindow s based on the
        > MainWindowHandl e of each process.
        > The problem is that i want to call PostMessage and send some characters in
        > the window that have the focus at that time, ie, the application's active
        > window.
        >
        > Do you know some way of doing that ??
        >
        > Thanks a lot for your answer,
        >
        > objectref
        >
        >
        > "Stoitcho Goutsev (100) [C# MVP]" <100@100.com> wrote in message
        > news:eUmxwn0cEH A.688@TK2MSFTNG P11.phx.gbl...[color=green]
        > > Hi objectref,
        > >
        > > .NET doesn't provide enumaration of windwos. However you can always use
        > > PInvoke and call one of the windows enumeration APIs.
        > >
        > > The only thing that you can do with .NET classes is to call
        > > Process.GetProc esses() method to get array of all processes currently
        > > started on the machine. Then for each process you can read the
        > > MainWindowHandl e property.
        > >
        > > However, in this way you can read one of the process' toplevel windows.
        > > Process may have more then one main windows. For example look at Windows[/color]
        > and[color=green]
        > > Internet Explorer application. They have only one process and a separate
        > > thread for each GUI. For these application you'll get only one of the main
        > > windows. Another example could be .NET application that runs more then one
        > > application domains, which of which has its own GUI.
        > >
        > > --
        > >
        > > Stoitcho Goutsev (100) [C# MVP]
        > >
        > >
        > > "objectref" <objectref@medi atrel.com> wrote in message
        > > news:%23Ej8zQ0c EHA.712@TK2MSFT NGP11.phx.gbl.. .[color=darkred]
        > > > Hi to all,
        > > >
        > > > is there a way to get the window handle of the main window of an[/color]
        > > application[color=darkred]
        > > > or process ?
        > > > For example, if someone opens Microsoft Word, he gets a window so he/she[/color]
        > > can[color=darkred]
        > > > write text.
        > > > Spy++ gives that this window is called _Wwg.
        > > >
        > > > How we can get a handle to this window assuming that we do not know
        > > > beforehand the name of the process,
        > > > we just want to find the handle of every application's main window ?
        > > > (or the window that currently has the focus/caret).
        > > >
        > > > Is this possible ??
        > > >
        > > > Thanks a lot for any help,
        > > >
        > > > objectref
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • objectref

          #5
          Re: Handle to main window.

          Adam,

          i am already making the calls using PostMessage... These calls succeds if
          the process that
          i use PostMessage to, has only one window, for example, a dos box.
          I then find the process MainWindowHandl e, and then using PostMessage i am
          able to send
          it some charcters, like the Environment.New Line key.

          But, if the app that is running is, for example, Notepad, then the above
          solution does not work.
          I have, somehow, to find the Active Window of the process and THEN i use
          PostMessage
          to send some the characters to.
          Notepad is simplel, but MS Word, for example, is more complicated, it starts
          with over that 10 windows with it
          so i cannot tell for sure which is the active window that user is typing
          textand so i will be
          able to use PostMessage...
          And of course, the processes that are running, are not known to me so i can
          hardcode the handles
          using Spy++, all that has to be dynamically determined.

          Any other ideas ??...


          objectref




          "Adam Clauss" <cabadam@tamu.e du> wrote in message
          news:OOY$LF1cEH A.556@tk2msftng p13.phx.gbl...[color=blue]
          > You will have to make calls to the Win32 API using PInvoke to call[/color]
          PostMessage. See:[color=blue]
          > http://www.pinvoke.net/
          >
          > --
          > Adam Clauss
          > cabadam@tamu.ed u
          >
          > "objectref" <objectref@medi atrel.com> wrote in message[/color]
          news:%23i15q$0c EHA.3128@TK2MSF TNGP11.phx.gbl. ..[color=blue][color=green]
          > > Hi Stoitcho,
          > >
          > > i know all this and that's why i am calling EnumChildWindow s based on[/color][/color]
          the[color=blue][color=green]
          > > MainWindowHandl e of each process.
          > > The problem is that i want to call PostMessage and send some characters[/color][/color]
          in[color=blue][color=green]
          > > the window that have the focus at that time, ie, the application's[/color][/color]
          active[color=blue][color=green]
          > > window.
          > >
          > > Do you know some way of doing that ??
          > >
          > > Thanks a lot for your answer,
          > >
          > > objectref
          > >
          > >
          > > "Stoitcho Goutsev (100) [C# MVP]" <100@100.com> wrote in message
          > > news:eUmxwn0cEH A.688@TK2MSFTNG P11.phx.gbl...[color=darkred]
          > > > Hi objectref,
          > > >
          > > > .NET doesn't provide enumaration of windwos. However you can always[/color][/color][/color]
          use[color=blue][color=green][color=darkred]
          > > > PInvoke and call one of the windows enumeration APIs.
          > > >
          > > > The only thing that you can do with .NET classes is to call
          > > > Process.GetProc esses() method to get array of all processes currently
          > > > started on the machine. Then for each process you can read the
          > > > MainWindowHandl e property.
          > > >
          > > > However, in this way you can read one of the process' toplevel[/color][/color][/color]
          windows.[color=blue][color=green][color=darkred]
          > > > Process may have more then one main windows. For example look at[/color][/color][/color]
          Windows[color=blue][color=green]
          > > and[color=darkred]
          > > > Internet Explorer application. They have only one process and a[/color][/color][/color]
          separate[color=blue][color=green][color=darkred]
          > > > thread for each GUI. For these application you'll get only one of the[/color][/color][/color]
          main[color=blue][color=green][color=darkred]
          > > > windows. Another example could be .NET application that runs more then[/color][/color][/color]
          one[color=blue][color=green][color=darkred]
          > > > application domains, which of which has its own GUI.
          > > >
          > > > --
          > > >
          > > > Stoitcho Goutsev (100) [C# MVP]
          > > >
          > > >
          > > > "objectref" <objectref@medi atrel.com> wrote in message
          > > > news:%23Ej8zQ0c EHA.712@TK2MSFT NGP11.phx.gbl.. .
          > > > > Hi to all,
          > > > >
          > > > > is there a way to get the window handle of the main window of an
          > > > application
          > > > > or process ?
          > > > > For example, if someone opens Microsoft Word, he gets a window so[/color][/color][/color]
          he/she[color=blue][color=green][color=darkred]
          > > > can
          > > > > write text.
          > > > > Spy++ gives that this window is called _Wwg.
          > > > >
          > > > > How we can get a handle to this window assuming that we do not know
          > > > > beforehand the name of the process,
          > > > > we just want to find the handle of every application's main window ?
          > > > > (or the window that currently has the focus/caret).
          > > > >
          > > > > Is this possible ??
          > > > >
          > > > > Thanks a lot for any help,
          > > > >
          > > > > objectref
          > > > >
          > > > >
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Adam Clauss

            #6
            Re: Handle to main window.

            Hmm... I'm not sure about getting the "focused" window.

            Is it only certain apps that this will occur in? You mention liked notepad and MSWord, are these part of a set list of applications
            you need to interact with, or could it conceivable be ANY application? If it is a set list, you could come up with a routine that
            locates the currently in use Window for a given program. Each program in your list would have its own method for doing this. Look
            at the title of the main window to determine what program it is, then could use Spy++ to determine what pattern of windows to search
            for - ex: Search for a window of class "....", then a child of class "..." and so forth using the FindWindow/FindWindowEx APIs.

            Of course, if it is ANY application the above will not work. Would have to do some research to see if there is a
            GetWindowWithCu rrentFocus type API.

            --
            Adam Clauss
            cabadam@tamu.ed u

            "objectref" <objectref@medi atrel.com> wrote in message news:uQBncX1cEH A.2616@TK2MSFTN GP11.phx.gbl...[color=blue]
            > Adam,
            >
            > i am already making the calls using PostMessage... These calls succeds if
            > the process that
            > i use PostMessage to, has only one window, for example, a dos box.
            > I then find the process MainWindowHandl e, and then using PostMessage i am
            > able to send
            > it some charcters, like the Environment.New Line key.
            >
            > But, if the app that is running is, for example, Notepad, then the above
            > solution does not work.
            > I have, somehow, to find the Active Window of the process and THEN i use
            > PostMessage
            > to send some the characters to.
            > Notepad is simplel, but MS Word, for example, is more complicated, it starts
            > with over that 10 windows with it
            > so i cannot tell for sure which is the active window that user is typing
            > textand so i will be
            > able to use PostMessage...
            > And of course, the processes that are running, are not known to me so i can
            > hardcode the handles
            > using Spy++, all that has to be dynamically determined.
            >
            > Any other ideas ??...
            >
            >
            > objectref
            >
            >
            >
            >
            > "Adam Clauss" <cabadam@tamu.e du> wrote in message
            > news:OOY$LF1cEH A.556@tk2msftng p13.phx.gbl...[color=green]
            > > You will have to make calls to the Win32 API using PInvoke to call[/color]
            > PostMessage. See:[color=green]
            > > http://www.pinvoke.net/
            > >
            > > --
            > > Adam Clauss
            > > cabadam@tamu.ed u
            > >
            > > "objectref" <objectref@medi atrel.com> wrote in message[/color]
            > news:%23i15q$0c EHA.3128@TK2MSF TNGP11.phx.gbl. ..[color=green][color=darkred]
            > > > Hi Stoitcho,
            > > >
            > > > i know all this and that's why i am calling EnumChildWindow s based on[/color][/color]
            > the[color=green][color=darkred]
            > > > MainWindowHandl e of each process.
            > > > The problem is that i want to call PostMessage and send some characters[/color][/color]
            > in[color=green][color=darkred]
            > > > the window that have the focus at that time, ie, the application's[/color][/color]
            > active[color=green][color=darkred]
            > > > window.
            > > >
            > > > Do you know some way of doing that ??
            > > >
            > > > Thanks a lot for your answer,
            > > >
            > > > objectref
            > > >
            > > >
            > > > "Stoitcho Goutsev (100) [C# MVP]" <100@100.com> wrote in message
            > > > news:eUmxwn0cEH A.688@TK2MSFTNG P11.phx.gbl...
            > > > > Hi objectref,
            > > > >
            > > > > .NET doesn't provide enumaration of windwos. However you can always[/color][/color]
            > use[color=green][color=darkred]
            > > > > PInvoke and call one of the windows enumeration APIs.
            > > > >
            > > > > The only thing that you can do with .NET classes is to call
            > > > > Process.GetProc esses() method to get array of all processes currently
            > > > > started on the machine. Then for each process you can read the
            > > > > MainWindowHandl e property.
            > > > >
            > > > > However, in this way you can read one of the process' toplevel[/color][/color]
            > windows.[color=green][color=darkred]
            > > > > Process may have more then one main windows. For example look at[/color][/color]
            > Windows[color=green][color=darkred]
            > > > and
            > > > > Internet Explorer application. They have only one process and a[/color][/color]
            > separate[color=green][color=darkred]
            > > > > thread for each GUI. For these application you'll get only one of the[/color][/color]
            > main[color=green][color=darkred]
            > > > > windows. Another example could be .NET application that runs more then[/color][/color]
            > one[color=green][color=darkred]
            > > > > application domains, which of which has its own GUI.
            > > > >
            > > > > --
            > > > >
            > > > > Stoitcho Goutsev (100) [C# MVP]
            > > > >
            > > > >
            > > > > "objectref" <objectref@medi atrel.com> wrote in message
            > > > > news:%23Ej8zQ0c EHA.712@TK2MSFT NGP11.phx.gbl.. .
            > > > > > Hi to all,
            > > > > >
            > > > > > is there a way to get the window handle of the main window of an
            > > > > application
            > > > > > or process ?
            > > > > > For example, if someone opens Microsoft Word, he gets a window so[/color][/color]
            > he/she[color=green][color=darkred]
            > > > > can
            > > > > > write text.
            > > > > > Spy++ gives that this window is called _Wwg.
            > > > > >
            > > > > > How we can get a handle to this window assuming that we do not know
            > > > > > beforehand the name of the process,
            > > > > > we just want to find the handle of every application's main window ?
            > > > > > (or the window that currently has the focus/caret).
            > > > > >
            > > > > > Is this possible ??
            > > > > >
            > > > > > Thanks a lot for any help,
            > > > > >
            > > > > > objectref
            > > > > >
            > > > > >
            > > > >
            > > > >
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Adam Clauss

              #7
              Re: Handle to main window.

              This sounds like it might do it:
              GetForegroundWi ndow()
              MSDN:
              The GetForegroundWi ndow function returns a handle to the foreground window (the window with which the user is currently working).

              --
              Adam Clauss
              cabadam@tamu.ed u

              "objectref" <objectref@medi atrel.com> wrote in message news:uQBncX1cEH A.2616@TK2MSFTN GP11.phx.gbl...[color=blue]
              > Adam,
              >
              > i am already making the calls using PostMessage... These calls succeds if
              > the process that
              > i use PostMessage to, has only one window, for example, a dos box.
              > I then find the process MainWindowHandl e, and then using PostMessage i am
              > able to send
              > it some charcters, like the Environment.New Line key.
              >
              > But, if the app that is running is, for example, Notepad, then the above
              > solution does not work.
              > I have, somehow, to find the Active Window of the process and THEN i use
              > PostMessage
              > to send some the characters to.
              > Notepad is simplel, but MS Word, for example, is more complicated, it starts
              > with over that 10 windows with it
              > so i cannot tell for sure which is the active window that user is typing
              > textand so i will be
              > able to use PostMessage...
              > And of course, the processes that are running, are not known to me so i can
              > hardcode the handles
              > using Spy++, all that has to be dynamically determined.
              >
              > Any other ideas ??...
              >
              >
              > objectref
              >
              >
              >
              >
              > "Adam Clauss" <cabadam@tamu.e du> wrote in message
              > news:OOY$LF1cEH A.556@tk2msftng p13.phx.gbl...[color=green]
              > > You will have to make calls to the Win32 API using PInvoke to call[/color]
              > PostMessage. See:[color=green]
              > > http://www.pinvoke.net/
              > >
              > > --
              > > Adam Clauss
              > > cabadam@tamu.ed u
              > >
              > > "objectref" <objectref@medi atrel.com> wrote in message[/color]
              > news:%23i15q$0c EHA.3128@TK2MSF TNGP11.phx.gbl. ..[color=green][color=darkred]
              > > > Hi Stoitcho,
              > > >
              > > > i know all this and that's why i am calling EnumChildWindow s based on[/color][/color]
              > the[color=green][color=darkred]
              > > > MainWindowHandl e of each process.
              > > > The problem is that i want to call PostMessage and send some characters[/color][/color]
              > in[color=green][color=darkred]
              > > > the window that have the focus at that time, ie, the application's[/color][/color]
              > active[color=green][color=darkred]
              > > > window.
              > > >
              > > > Do you know some way of doing that ??
              > > >
              > > > Thanks a lot for your answer,
              > > >
              > > > objectref
              > > >
              > > >
              > > > "Stoitcho Goutsev (100) [C# MVP]" <100@100.com> wrote in message
              > > > news:eUmxwn0cEH A.688@TK2MSFTNG P11.phx.gbl...
              > > > > Hi objectref,
              > > > >
              > > > > .NET doesn't provide enumaration of windwos. However you can always[/color][/color]
              > use[color=green][color=darkred]
              > > > > PInvoke and call one of the windows enumeration APIs.
              > > > >
              > > > > The only thing that you can do with .NET classes is to call
              > > > > Process.GetProc esses() method to get array of all processes currently
              > > > > started on the machine. Then for each process you can read the
              > > > > MainWindowHandl e property.
              > > > >
              > > > > However, in this way you can read one of the process' toplevel[/color][/color]
              > windows.[color=green][color=darkred]
              > > > > Process may have more then one main windows. For example look at[/color][/color]
              > Windows[color=green][color=darkred]
              > > > and
              > > > > Internet Explorer application. They have only one process and a[/color][/color]
              > separate[color=green][color=darkred]
              > > > > thread for each GUI. For these application you'll get only one of the[/color][/color]
              > main[color=green][color=darkred]
              > > > > windows. Another example could be .NET application that runs more then[/color][/color]
              > one[color=green][color=darkred]
              > > > > application domains, which of which has its own GUI.
              > > > >
              > > > > --
              > > > >
              > > > > Stoitcho Goutsev (100) [C# MVP]
              > > > >
              > > > >
              > > > > "objectref" <objectref@medi atrel.com> wrote in message
              > > > > news:%23Ej8zQ0c EHA.712@TK2MSFT NGP11.phx.gbl.. .
              > > > > > Hi to all,
              > > > > >
              > > > > > is there a way to get the window handle of the main window of an
              > > > > application
              > > > > > or process ?
              > > > > > For example, if someone opens Microsoft Word, he gets a window so[/color][/color]
              > he/she[color=green][color=darkred]
              > > > > can
              > > > > > write text.
              > > > > > Spy++ gives that this window is called _Wwg.
              > > > > >
              > > > > > How we can get a handle to this window assuming that we do not know
              > > > > > beforehand the name of the process,
              > > > > > we just want to find the handle of every application's main window ?
              > > > > > (or the window that currently has the focus/caret).
              > > > > >
              > > > > > Is this possible ??
              > > > > >
              > > > > > Thanks a lot for any help,
              > > > > >
              > > > > > objectref
              > > > > >
              > > > > >
              > > > >
              > > > >
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Stoitcho Goutsev \(100\) [C# MVP]

                #8
                Re: Handle to main window.

                objectref,

                If you want to send keystrokes to the active application use SendKeys class.
                This is the easiest.
                It might be pretty reasonable as long as there is only one focused window
                and it belongs to the foreground (active) window.

                Anyways if you want to get the handle to the focused window you need to do a
                bit more.


                [DllImport("user 32.dll")]
                private static extern IntPtr GetForegroundWi ndow();
                [DllImport("user 32.dll")]
                private static extern IntPtr GetWindowThread ProcessId(IntPt r hwnd, IntPtr
                lpdwProcessId);
                [DllImport("user 32.dll")]
                private static extern bool AttachThreadInp ut(IntPtr idAttach , IntPtr
                idAttachTo, bool fAttach);
                [DllImport("user 32.dll")]
                private static extern IntPtr GetFocus();

                private void button1_Click(o bject sender, System.EventArg s e)
                {
                IntPtr thisThreadID = new IntPtr(AppDomai n.GetCurrentThr eadId());
                IntPtr activeHwnd = GetForegroundWi ndow();
                if(activeHwnd != IntPtr.Zero)
                {
                IntPtr activeThreadID = GetWindowThread ProcessId(activ eHwnd,
                IntPtr.Zero);
                if(AttachThread Input(activeThr eadID, thisThreadID, true))
                {
                IntPtr focusedHwnd = GetFocus();
                if(focusedHwnd != IntPtr.Zero)
                {
                Console.WriteLi ne("FocusedHwnd : {0}\n====", focusedHwnd.ToS tring());
                }
                AttachThreadInp ut(activeThread ID, thisThreadID, false);
                }
                }
                }

                Bare in mind that the thread executing that code has to be a UI thread. and
                that this code cannot detect its own focused window because it cannot attach
                a thread to itself. If you want to get your own focused window it is quite
                straightforward using Control class.

                --
                HTH
                Stoitcho Goutsev (100) [C# MVP]


                "objectref" <objectref@medi atrel.com> wrote in message
                news:%23i15q$0c EHA.3128@TK2MSF TNGP11.phx.gbl. ..[color=blue]
                > Hi Stoitcho,
                >
                > i know all this and that's why i am calling EnumChildWindow s based on the
                > MainWindowHandl e of each process.
                > The problem is that i want to call PostMessage and send some characters in
                > the window that have the focus at that time, ie, the application's active
                > window.
                >
                > Do you know some way of doing that ??
                >
                > Thanks a lot for your answer,
                >
                > objectref
                >
                >
                > "Stoitcho Goutsev (100) [C# MVP]" <100@100.com> wrote in message
                > news:eUmxwn0cEH A.688@TK2MSFTNG P11.phx.gbl...[color=green]
                > > Hi objectref,
                > >
                > > .NET doesn't provide enumaration of windwos. However you can always use
                > > PInvoke and call one of the windows enumeration APIs.
                > >
                > > The only thing that you can do with .NET classes is to call
                > > Process.GetProc esses() method to get array of all processes currently
                > > started on the machine. Then for each process you can read the
                > > MainWindowHandl e property.
                > >
                > > However, in this way you can read one of the process' toplevel windows.
                > > Process may have more then one main windows. For example look at Windows[/color]
                > and[color=green]
                > > Internet Explorer application. They have only one process and a separate
                > > thread for each GUI. For these application you'll get only one of the[/color][/color]
                main[color=blue][color=green]
                > > windows. Another example could be .NET application that runs more then[/color][/color]
                one[color=blue][color=green]
                > > application domains, which of which has its own GUI.
                > >
                > > --
                > >
                > > Stoitcho Goutsev (100) [C# MVP]
                > >
                > >
                > > "objectref" <objectref@medi atrel.com> wrote in message
                > > news:%23Ej8zQ0c EHA.712@TK2MSFT NGP11.phx.gbl.. .[color=darkred]
                > > > Hi to all,
                > > >
                > > > is there a way to get the window handle of the main window of an[/color]
                > > application[color=darkred]
                > > > or process ?
                > > > For example, if someone opens Microsoft Word, he gets a window so[/color][/color][/color]
                he/she[color=blue][color=green]
                > > can[color=darkred]
                > > > write text.
                > > > Spy++ gives that this window is called _Wwg.
                > > >
                > > > How we can get a handle to this window assuming that we do not know
                > > > beforehand the name of the process,
                > > > we just want to find the handle of every application's main window ?
                > > > (or the window that currently has the focus/caret).
                > > >
                > > > Is this possible ??
                > > >
                > > > Thanks a lot for any help,
                > > >
                > > > objectref
                > > >
                > > >[/color]
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • objectref

                  #9
                  Re: Handle to main window.

                  Hmmmm....

                  at first, thanks for your solution, it seems than know i am close enough,
                  but:

                  Let's assume that, for simplicity reasons, i want to test your code with a
                  pre-known
                  window handle. I take this from enumerating processes, find, let's say an
                  open Notepad window
                  and then get it's MainWindowHandl e, that is, 3475108.

                  But, the below code does not work. It reaches the "PostMessage"li ne but i do
                  not
                  get an Enter hit in notepad app.
                  Can you figure out why ?


                  private void button1_Click(o bject sender, System.EventArg s e)
                  {
                  IntPtr thisThreadID = new IntPtr(AppDomai n.GetCurrentThr eadId());
                  IntPtr activeHwnd = GetForegroundWi ndow();

                  if (activeHwnd != IntPtr.Zero)
                  {
                  IntPtr activeThreadID = GetWindowThread ProcessId((IntP tr) 3475108,
                  IntPtr.Zero);
                  if (AttachThreadIn put(activeThrea dID, thisThreadID, true))
                  {
                  IntPtr focusedHwnd = GetFocus();
                  if (focusedHwnd != IntPtr.Zero)
                  {
                  //Console.WriteLi ne("FocusedHwnd : {0}\n====",
                  focusedHwnd.ToS tring());
                  listBox1.Items. Add("FocusedHwn d: {0}\n====" +
                  focusedHwnd.ToS tring());
                  PostMessage((In tPtr) focusedHwnd, WM_CHAR, 13, 0);
                  }
                  AttachThreadInp ut(activeThread ID, thisThreadID, false);
                  }
                  }
                  }




                  Comment

                  • Stoitcho Goutsev \(100\) [C# MVP]

                    #10
                    Re: Handle to main window.

                    Hi objecref,

                    The problem I believe is that the Notepad is not the active application. How
                    I said there is only one focused window at a time and it is or it is a child
                    to the foreground (active) window. If you try GetFocus for a thread which
                    hasn't created the foreground window it always return null.

                    Just a little reminder. Even though you can post key messages to a window it
                    is not guarantee that the target window will accept them correctly. Each
                    input (UI) thread has associated keystate buffers and so on that play some
                    role in the key processing. If you just post key messages it won't go thru
                    all the internal key processing and won't update those structures. Methods
                    like GetKeyState, GetAsyncKeyStat e, GetKeyboardStat e and so on which
                    application use won't work. Key messages are only notifications aftere all
                    the work of precessing the keyboard is done. The only way to simulate real
                    keyboard activity is to use either SendInput or keybd_event API or SendKeys
                    class in .NET.
                    However because they synthesizes keyboard strokes, mouse movements and mouse
                    button clicks it you cannot specify the target window. Keystorkes go to the
                    focused window, mouse events go to the window under the mouse cursor
                    position.
                    Anyways if you still want to use post messages you may consider using
                    AttachThreadInp ut and SetKeyboardStat e method to handle some of the
                    situations. However some keys such as CapsLock, NumLock, etc cannot be
                    simulated unless SendInput or keybd_event are used.

                    --
                    HTH
                    Stoitcho Goutsev (100) [C# MVP]


                    "objectref" <objectref@medi atrel.com> wrote in message
                    news:ucCF2X7cEH A.2724@TK2MSFTN GP12.phx.gbl...[color=blue]
                    > Hmmmm....
                    >
                    > at first, thanks for your solution, it seems than know i am close enough,
                    > but:
                    >
                    > Let's assume that, for simplicity reasons, i want to test your code with a
                    > pre-known
                    > window handle. I take this from enumerating processes, find, let's say an
                    > open Notepad window
                    > and then get it's MainWindowHandl e, that is, 3475108.
                    >
                    > But, the below code does not work. It reaches the "PostMessage"li ne but i[/color]
                    do[color=blue]
                    > not
                    > get an Enter hit in notepad app.
                    > Can you figure out why ?
                    >
                    >
                    > private void button1_Click(o bject sender, System.EventArg s e)
                    > {
                    > IntPtr thisThreadID = new IntPtr(AppDomai n.GetCurrentThr eadId());
                    > IntPtr activeHwnd = GetForegroundWi ndow();
                    >
                    > if (activeHwnd != IntPtr.Zero)
                    > {
                    > IntPtr activeThreadID = GetWindowThread ProcessId((IntP tr) 3475108,
                    > IntPtr.Zero);
                    > if (AttachThreadIn put(activeThrea dID, thisThreadID, true))
                    > {
                    > IntPtr focusedHwnd = GetFocus();
                    > if (focusedHwnd != IntPtr.Zero)
                    > {
                    > //Console.WriteLi ne("FocusedHwnd : {0}\n====",
                    > focusedHwnd.ToS tring());
                    > listBox1.Items. Add("FocusedHwn d: {0}\n====" +
                    > focusedHwnd.ToS tring());
                    > PostMessage((In tPtr) focusedHwnd, WM_CHAR, 13, 0);
                    > }
                    > AttachThreadInp ut(activeThread ID, thisThreadID, false);
                    > }
                    > }
                    > }
                    >
                    >
                    >
                    >[/color]


                    Comment

                    • objectref

                      #11
                      Re: Handle to main window.

                      Hi Stoitcho,

                      i finally did that:
                      I use EnumWindows and EnumChildWindow s and i present the user
                      in a treeview their relation for all open processes in the machine.
                      Then, the user select the windows that he needs to send keystrokes
                      and an adjustable timer is responsible to send them at timer intervals.

                      I did that because, as you said, the GetFocus needs to have the windows
                      in the foreground. I need these windows to be minimized and the whole
                      process
                      need not have any interaction wit the user, no flashing windows, no
                      SetForeGroundWi ndow,
                      nothing of all these.
                      So, as you understand, SendKeys was not an option for me. Imagine for
                      example, to have a Notepad app,
                      a dos-box and another one Winword app, running minimized and all getting the
                      same characters
                      at some timer interval, without interrupt the use working on something else
                      in that machine...

                      Anyway, the solution as it is right now is working perfectly.

                      Thanks again, you and Adam for your help!

                      objectref



                      "Stoitcho Goutsev (100) [C# MVP]" <100@100.com> wrote in message
                      news:OEiXF59cEH A.3260@TK2MSFTN GP09.phx.gbl...[color=blue]
                      > Hi objecref,
                      >
                      > The problem I believe is that the Notepad is not the active application.[/color]
                      How[color=blue]
                      > I said there is only one focused window at a time and it is or it is a[/color]
                      child[color=blue]
                      > to the foreground (active) window. If you try GetFocus for a thread which
                      > hasn't created the foreground window it always return null.
                      >
                      > Just a little reminder. Even though you can post key messages to a window[/color]
                      it[color=blue]
                      > is not guarantee that the target window will accept them correctly. Each
                      > input (UI) thread has associated keystate buffers and so on that play some
                      > role in the key processing. If you just post key messages it won't go thru
                      > all the internal key processing and won't update those structures.[/color]
                      Methods[color=blue]
                      > like GetKeyState, GetAsyncKeyStat e, GetKeyboardStat e and so on which
                      > application use won't work. Key messages are only notifications aftere all
                      > the work of precessing the keyboard is done. The only way to simulate real
                      > keyboard activity is to use either SendInput or keybd_event API or[/color]
                      SendKeys[color=blue]
                      > class in .NET.
                      > However because they synthesizes keyboard strokes, mouse movements and[/color]
                      mouse[color=blue]
                      > button clicks it you cannot specify the target window. Keystorkes go to[/color]
                      the[color=blue]
                      > focused window, mouse events go to the window under the mouse cursor
                      > position.
                      > Anyways if you still want to use post messages you may consider using
                      > AttachThreadInp ut and SetKeyboardStat e method to handle some of the
                      > situations. However some keys such as CapsLock, NumLock, etc cannot be
                      > simulated unless SendInput or keybd_event are used.
                      >
                      > --
                      > HTH
                      > Stoitcho Goutsev (100) [C# MVP]
                      >
                      >
                      > "objectref" <objectref@medi atrel.com> wrote in message
                      > news:ucCF2X7cEH A.2724@TK2MSFTN GP12.phx.gbl...[color=green]
                      > > Hmmmm....
                      > >
                      > > at first, thanks for your solution, it seems than know i am close[/color][/color]
                      enough,[color=blue][color=green]
                      > > but:
                      > >
                      > > Let's assume that, for simplicity reasons, i want to test your code with[/color][/color]
                      a[color=blue][color=green]
                      > > pre-known
                      > > window handle. I take this from enumerating processes, find, let's say[/color][/color]
                      an[color=blue][color=green]
                      > > open Notepad window
                      > > and then get it's MainWindowHandl e, that is, 3475108.
                      > >
                      > > But, the below code does not work. It reaches the "PostMessage"li ne but[/color][/color]
                      i[color=blue]
                      > do[color=green]
                      > > not
                      > > get an Enter hit in notepad app.
                      > > Can you figure out why ?
                      > >
                      > >
                      > > private void button1_Click(o bject sender, System.EventArg s e)
                      > > {
                      > > IntPtr thisThreadID = new IntPtr(AppDomai n.GetCurrentThr eadId());
                      > > IntPtr activeHwnd = GetForegroundWi ndow();
                      > >
                      > > if (activeHwnd != IntPtr.Zero)
                      > > {
                      > > IntPtr activeThreadID = GetWindowThread ProcessId((IntP tr)[/color][/color]
                      3475108,[color=blue][color=green]
                      > > IntPtr.Zero);
                      > > if (AttachThreadIn put(activeThrea dID, thisThreadID, true))
                      > > {
                      > > IntPtr focusedHwnd = GetFocus();
                      > > if (focusedHwnd != IntPtr.Zero)
                      > > {
                      > > //Console.WriteLi ne("FocusedHwnd : {0}\n====",
                      > > focusedHwnd.ToS tring());
                      > > listBox1.Items. Add("FocusedHwn d: {0}\n====" +
                      > > focusedHwnd.ToS tring());
                      > > PostMessage((In tPtr) focusedHwnd, WM_CHAR, 13, 0);
                      > > }
                      > > AttachThreadInp ut(activeThread ID, thisThreadID, false);
                      > > }
                      > > }
                      > > }
                      > >
                      > >
                      > >
                      > >[/color]
                      >
                      >[/color]


                      Comment

                      • hisham serhan

                        #12
                        Re: Handle to main window.

                        hii
                        i have read the problem and i have the same problem;
                        but i cant fix it.
                        i am working on a MY Task Manager program in c#
                        i wana do exactly the same one in Windows xp

                        almost i don but i need your help :
                        How can i get the list curent Applications Running (windows)
                        i got the processes list but i need the Application list.

                        i have thinking that it`s the same problem but you looked for a windows
                        opens
                        please help to do that one
                        thx for all the informations

                        *** Sent via Developersdex http://www.developersdex.com ***
                        Don't just participate in USENET...get rewarded for it!

                        Comment

                        Working...