Getting console window's hWnd

Discussion in 'C Programming' started by Quentin Pope, Aug 13, 2011.

  1. Quentin Pope

    Quentin Pope Guest

    Hello

    I am writing a console app for Windows (running on Vista 32 bit at
    present)

    I would like to be able to resize the DOS window the program is running
    in, from within my code

    For this I would need to get that window's hWnd

    Can anyone advise how to do this?

    Many Thanks.
     
    Quentin Pope, Aug 13, 2011
    #1
    1. Advertising

  2. Quentin Pope <> writes:

    > I am writing a console app for Windows (running on Vista 32 bit at
    > present)
    >
    > I would like to be able to resize the DOS window the program is running
    > in, from within my code
    >
    > For this I would need to get that window's hWnd
    >
    > Can anyone advise how to do this?


    People in a Windows group are a good bet. I think

    comp.os.ms-windows.programmer.win32
    comp.os.msdos.programmer

    might be worth a try, but do some work first. I searched for "console
    window handle" and the top result looked perfectly acceptable.

    --
    Ben.
     
    Ben Bacarisse, Aug 13, 2011
    #2
    1. Advertising

  3. Quentin Pope

    BartC Guest

    "Quentin Pope" <> wrote in message
    news:j26ngo$v68$...

    > I would like to be able to resize the DOS window the program is running
    > in, from within my code
    >
    > For this I would need to get that window's hWnd
    >
    > Can anyone advise how to do this?


    You need MSDN, perhaps start here:

    http://msdn.microsoft.com/en-us/library/ms682073(v=VS.85).aspx

    or search for msdn console.

    I think I once used something like GetStdHandle(STD_OUTPUT_HANDLE).

    --
    Bartc
     
    BartC, Aug 13, 2011
    #3
  4. "BartC" <> schrieb im Newsbeitrag
    news:j26r8b$oad$...
    ....
    > I think I once used something like GetStdHandle(STD_OUTPUT_HANDLE).

    ....
    yep.
    I agree.
    But I am not expirienced in Win64.
     
    Heinrich Wolf, Aug 14, 2011
    #4
  5. Quentin Pope

    Geoff Guest

    On Sat, 13 Aug 2011 20:40:56 +0000 (UTC), Quentin Pope
    <> wrote:

    >Hello
    >
    >I am writing a console app for Windows (running on Vista 32 bit at
    >present)
    >
    >I would like to be able to resize the DOS window the program is running
    >in, from within my code
    >
    >For this I would need to get that window's hWnd
    >
    >Can anyone advise how to do this?
    >
    >Many Thanks.


    A Windows console app doesn't have an hWnd as such, you can't call the
    Windows API functions to resize the window and you don't have a
    message pump to receive resize messages if the user resizes you.

    Others have already sent you to the console API reference. The handles
    to your console will be available via GetStdHandle, the screen buffer
    info via GetConsoleScreenBufferInfo and you also need to set a console
    control handler via SetConsoleCtrlHandler.

    For more detail you should look at the Microsoft forums or windows
    programming newsgroups.
     
    Geoff, Aug 14, 2011
    #5
  6. Quentin Pope

    jacob navia Guest

    Le 13/08/11 22:40, Quentin Pope a écrit :
    > Hello
    >
    > I am writing a console app for Windows (running on Vista 32 bit at
    > present)
    >
    > I would like to be able to resize the DOS window the program is running
    > in, from within my code
    >
    > For this I would need to get that window's hWnd
    >
    > Can anyone advise how to do this?
    >
    > Many Thanks.


    1) Find the title of the console
    2) Findthe window with that title

    #include <windows.h>

    int main()
    {
    char title[500]; // to hold title
    HWND hwnd;

    GetConsoleTitle( title, 500 );
    hwnd = FindWindow( NULL, title );
    printf("The hwnd of the console is: %p\n",hwnd);
    }

    This supposes that the title of the console is less than 500 chars
     
    jacob navia, Aug 14, 2011
    #6
  7. On Aug 14, 10:03 am, jacob navia <> wrote:
    > [...]
    >    GetConsoleTitle( title, 500 );
    >    hwnd = FindWindow( NULL, title );
    > [...]
    > This supposes that the title of the console is less than 500 chars


    And this supposes that the console title uniquely identifies the
    window. Haven't you ever started two instances of the same application?
     
    Harald van Dijk, Aug 14, 2011
    #7
  8. Quentin Pope

    Geoff Guest

    On Sun, 14 Aug 2011 10:03:00 +0200, jacob navia <>
    wrote:

    >Le 13/08/11 22:40, Quentin Pope a écrit :
    >> Hello
    >>
    >> I am writing a console app for Windows (running on Vista 32 bit at
    >> present)
    >>
    >> I would like to be able to resize the DOS window the program is running
    >> in, from within my code
    >>
    >> For this I would need to get that window's hWnd
    >>
    >> Can anyone advise how to do this?
    >>
    >> Many Thanks.

    >
    >1) Find the title of the console
    >2) Findthe window with that title
    >
    >#include <windows.h>
    >
    >int main()
    >{
    > char title[500]; // to hold title
    > HWND hwnd;
    >
    > GetConsoleTitle( title, 500 );
    > hwnd = FindWindow( NULL, title );
    > printf("The hwnd of the console is: %p\n",hwnd);
    >}
    >
    >This supposes that the title of the console is less than 500 chars


    It also supposes that the window title is unique.
     
    Geoff, Aug 14, 2011
    #8
  9. Quentin Pope

    jacob navia Guest

    Le 14/08/11 10:24, Harald van Dijk a écrit :
    > On Aug 14, 10:03 am, jacob navia<> wrote:
    >> [...]
    >> GetConsoleTitle( title, 500 );
    >> hwnd = FindWindow( NULL, title );
    >> [...]
    >> This supposes that the title of the console is less than 500 chars

    >
    > And this supposes that the console title uniquely identifies the
    > window. Haven't you ever started two instances of the same application?


    If you need more sphistication use EnumWindows directly. Then you
    will find ALL windows with the given name.

    In this example I wanted to keep this simple. Actually FindWindow
    calls indirectly EnumWindows.
     
    jacob navia, Aug 14, 2011
    #9
  10. On Aug 14, 11:13 am, jacob navia <> wrote:
    > Le 14/08/11 10:24, Harald van Dijk a écrit :
    > > On Aug 14, 10:03 am, jacob navia<>  wrote:
    > >> [...]
    > >>     GetConsoleTitle( title, 500 );
    > >>     hwnd = FindWindow( NULL, title );
    > >> [...]
    > >> This supposes that the title of the console is less than 500 chars

    >
    > > And this supposes that the console title uniquely identifies the
    > > window. Haven't you ever started two instances of the same application?

    >
    > If you need more sphistication use EnumWindows directly. Then you
    > will find ALL windows with the given name.


    In which case you don't know which one's yours, so it is unsuitable
    for resizing the window. Take BartC's post, follow the link to MSDN,
    and you will see that there is a more direct way of getting the window
    handle. Which still doesn't really do anything useful for you, but
    that's for other reasons :)
     
    Harald van Dijk, Aug 14, 2011
    #10
  11. Quentin Pope

    jacob navia Guest

    Le 14/08/11 12:13, Robert Wessel a écrit :
    > On Sun, 14 Aug 2011 11:13:20 +0200, jacob navia<>
    > wrote:
    >
    >> Le 14/08/11 10:24, Harald van D?k a écrit :
    >>> On Aug 14, 10:03 am, jacob navia<> wrote:
    >>>> [...]
    >>>> GetConsoleTitle( title, 500 );
    >>>> hwnd = FindWindow( NULL, title );
    >>>> [...]
    >>>> This supposes that the title of the console is less than 500 chars
    >>>
    >>> And this supposes that the console title uniquely identifies the
    >>> window. Haven't you ever started two instances of the same application?

    >>
    >> If you need more sphistication use EnumWindows directly. Then you
    >> will find ALL windows with the given name.
    >>
    >> In this example I wanted to keep this simple. Actually FindWindow
    >> calls indirectly EnumWindows.

    >
    >
    > Which doesn't address the problem - which of (potentially) multiple
    > windows with the same name is the console window for the current
    > application. GetConsoleWindow() is likely more appropriate.


    Yes, you are right. I posted a code that is working and I never
    really looked around for better alternatives.
     
    jacob navia, Aug 14, 2011
    #11
  12. Quentin Pope

    BruceS Guest

    On Sun, 14 Aug 2011 12:31:34 +0200, jacob navia wrote:

    > Le 14/08/11 12:13, Robert Wessel a écrit :
    >> On Sun, 14 Aug 2011 11:13:20 +0200, jacob navia<>
    >> wrote:
    >>
    >>> Le 14/08/11 10:24, Harald van D?k a écrit :
    >>>> On Aug 14, 10:03 am, jacob navia<> wrote:
    >>>>> [...]
    >>>>> GetConsoleTitle( title, 500 );
    >>>>> hwnd = FindWindow( NULL, title );
    >>>>> [...]
    >>>>> This supposes that the title of the console is less than 500 chars
    >>>>
    >>>> And this supposes that the console title uniquely identifies the
    >>>> window. Haven't you ever started two instances of the same
    >>>> application?
    >>>
    >>> If you need more sphistication use EnumWindows directly. Then you will
    >>> find ALL windows with the given name.
    >>>
    >>> In this example I wanted to keep this simple. Actually FindWindow
    >>> calls indirectly EnumWindows.

    >>
    >>
    >> Which doesn't address the problem - which of (potentially) multiple
    >> windows with the same name is the console window for the current
    >> application. GetConsoleWindow() is likely more appropriate.

    >
    > Yes, you are right. I posted a code that is working and I never really
    > looked around for better alternatives.


    I'm curious. Is this sort of thing the reason why some clc regulars try
    to direct off-topic questions to more appropriate groups?
     
    BruceS, Aug 15, 2011
    #12
  13. BruceS <> writes:
    > On Sun, 14 Aug 2011 12:31:34 +0200, jacob navia wrote:
    >> Le 14/08/11 12:13, Robert Wessel a écrit :

    [...]
    >>> Which doesn't address the problem - which of (potentially) multiple
    >>> windows with the same name is the console window for the current
    >>> application. GetConsoleWindow() is likely more appropriate.

    >>
    >> Yes, you are right. I posted a code that is working and I never really
    >> looked around for better alternatives.

    >
    > I'm curious. Is this sort of thing the reason why some clc regulars try
    > to direct off-topic questions to more appropriate groups?


    Yes.

    Personally I do it for two reasons: to help the original poster get
    better information (the folks in comp.os.ms-windows.programmer.win32
    know more about this than most of the posters here), and to try to
    reduce the noise level here (I come here to discuss C, not Windows
    programming).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 15, 2011
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Storm
    Replies:
    2
    Views:
    912
    Storm
    Oct 16, 2004
  2. brewman

    Obtaining window handle (hWnd)

    brewman, Oct 15, 2003, in forum: HTML
    Replies:
    5
    Views:
    12,199
    brewman
    Oct 16, 2003
  3. LT
    Replies:
    7
    Views:
    2,130
    Phlip
    Jul 25, 2004
  4. Joseph
    Replies:
    3
    Views:
    1,252
    Howard
    Jul 20, 2005
  5. Miktor
    Replies:
    7
    Views:
    12,162
    Joe Bacigalupa
    Aug 9, 2005
Loading...

Share This Page