Getting console window's hWnd

Q

Quentin Pope

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.
 
B

Ben Bacarisse

Quentin Pope said:
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.
 
G

Geoff

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.
 
J

jacob navia

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
 
H

Harald van Dijk

[...]
   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?
 
G

Geoff

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.
 
J

jacob navia

Le 14/08/11 10:24, Harald van Dijk a écrit :
[...]
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.
 
H

Harald van Dijk

Le 14/08/11 10:24, Harald van Dijk a écrit :
[...]
    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 :)
 
J

jacob navia

Le 14/08/11 12:13, Robert Wessel a écrit :
Le 14/08/11 10:24, Harald van D?k a écrit :
[...]
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.
 
B

BruceS

Le 14/08/11 12:13, Robert Wessel a écrit :
Le 14/08/11 10:24, Harald van D?k a écrit :
[...]
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?
 
K

Keith Thompson

BruceS said:
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top