Question: How to resize a console window in C++

Computer builds, hardware and software discussion or troubleshooting, including peripherals. Essentially a general place to talk about desktop computers.
Locked
Anonymous

Question: How to resize a console window in C++

Post by Anonymous »

I'm using CodeWarrior to compile C++ code. The console default is 80 characters in width and 30 characters in height. I was wondering how to modify these values, I'd assume they're measured in pixels.

Archived topic from Iceteks, old topic ID:2254, old post ID:19226
User avatar
Red Squirrel
Posts: 29206
Joined: Wed Dec 18, 2002 12:14 am
Location: Northern Ontario
Contact:

Question: How to resize a console window in C++

Post by Red Squirrel »

The standard console size is 80 by 24 characters I believe and can't be changed within the program, at least I don't think so...

Archived topic from Iceteks, old topic ID:2254, old post ID:19228
Honk if you love Jesus, text if you want to meet Him!
Anonymous

Question: How to resize a console window in C++

Post by Anonymous »

The console size doens't have to be changed at run time, just initially. The user can change the window size the way it is right now by dragging the bottom right of the window to the desired size. I know there is a way to do it, I've seen it done before. The reason I need this is a program I'm making outputs more than can be fit on the screen. The screen can be resized, but there should be a way to do it automatically.

Thanks for the greeting Red, I hope this forum will be a valuable resource ^^

Archived topic from Iceteks, old topic ID:2254, old post ID:19232
Fat Cat
Posts: 16
Joined: Wed Mar 31, 2004 8:33 pm

Question: How to resize a console window in C++

Post by Fat Cat »

One non-automatic method you might try is to change the default size of the console window at runtime. To do this run your console application and then do a right click on the title bar of the console window. Choose the DEAFAULTS option and then the LAYOUT tab. Change the WIDTH to something like 120 or whatever you like. When you now run the application you will be able to drag the console size well past the old 80 chars.

I'm sure there must be a way to pass these size parameters on to the Windows system from your program, but haven't figured it out yet. A better method would be to build an MFC application rather than a console application. That way you can output your data into a textbox which can wordwrap and resize to fit the text etc.

Archived topic from Iceteks, old topic ID:2254, old post ID:19233
Anonymous

Question: How to resize a console window in C++

Post by Anonymous »

Changing the defaults work exactly as you suggested, but it becomes the default for all console apps, which is too big in most cases.

I found an exam question on the internet that asks pretty much exactly what I want done, but it's a little over my ability to answer it.

3) This question is about classes in C++.

Note: All parts of this question should be read thoroughly before any code is written.

a) Declare a class TWindow. It should have 7 member variables: 4 integers to hold left, top, width and height of the window. It should have a pointer to a string that forms the title of the window and 2 more integers to hold the window colour and text colour.
(5 marks)


B) Implement in C++, four access member functions for the class: ‘SetWinSize’ (to set the size of the window), ‘SetWinTitle’ (to set the title of the window by dynamically allocating the string), ‘SetWinColour’ (to set the colour of the window) and ‘SetTextColour’ (to set the text colour of the window).
(10 marks)

c) Implement in C++ code a class constructor that initialises the window. The constructor should have default values for each parameter (Title, Left, Top, Width and Height of window, WindowColour and TextColour), such that if the constructor is called with no actual parameters it will initialise the object's data members to set the window size to 80x25, the window colour to blue, the window text to white and the title string to NULL.
Implement in C++ the class destructor to release the heap memory used to hold the string.
(7 marks)
Q3 continued on next page……….


d) Assuming that the function ‘Draw( )’ (outlined in Figure 1 below), has been implemented as a member function of the class TWindow, implement a main program section which declares an object of type TWindow called Window1. Window1 should use all the default parameters of the class constructor, but override the title with the string “Window1”. The window should then be drawn by calling the member function ‘Draw( )’.

(3 marks)


(Total: 25 marks)


void TWindow::Draw()
{
textbackground(mWinColour);
textcolor(mTextColour);
window(mLeft,mTop,mLeft+mWidth-1,mTop+mHeight-1);
clrscr();

if (mTitle==NULL) return;

if (strlen(mTitle) > 0)
{
gotoxy((mWidth-strlen(mTitle))/2,1);
cout << mTitle;
}
else mTitle=NULL;
}



It's the part 'c' that I'd especially like answered. If you can make a window that's 80x25, you could just as easily adjust the size.

Archived topic from Iceteks, old topic ID:2254, old post ID:19234
Fat Cat
Posts: 16
Joined: Wed Mar 31, 2004 8:33 pm

Question: How to resize a console window in C++

Post by Fat Cat »

Microsoft Visual C++ includes a sample application that has routines that allow you to set the console to any size you like and get the current console size info etc. You just pass the size parameters you want to change the size of the console window

Unfortunately its a bit big to post here as it contains about 28 files (including header files). Called "Console sample (console functions)"

Archived topic from Iceteks, old topic ID:2254, old post ID:19235
Locked