How to change window size in C++?

32,218

Solution 1

That function is useless for console applications, which don't own a window (unless they create one using the CreateWindow API, which is atypical for console apps). Instead their output is connected to csrss, which does have windows.

You should be using

  • SetConsoleScreenBufferSize
  • SetConsoleWindowInfo

instead.

There's an example at http://www.cplusplus.com/forum/windows/10731/

Solution 2

This works for me:

HWND hwnd = GetConsoleWindow();
if( hwnd != NULL ){ MoveWindow(hwnd ,340,550 ,680,150 ,TRUE); }

Solution 3

If your looking for changing the screen buffer then :

HANDLE buff = GetStdHandle(STD_OUTPUT_HANDLE);
COORD sizeOfBuff;
sizeOfBuff.X=150;
sizeOfBuff.Y=100;
SetConsoleScreenBufferSize(buff,sizeOfBuff);

To resize the screen use DaveWalley's solution.

Or you could do this (only for resizing)

HWND hwnd = GetConsoleWindow();
if( hwnd != NULL ){ SetWindowPos(hwnd ,0,0,0 ,1000,300 ,SWP_SHOWWINDOW|SWP_NOMOVE); }

Be sure to include:

#define _WIN32_WINNT 0x0502
#include<windows.h>

at the begining of the file. Literally as the first lines.

Got the solution by reding about the functions mentioned by Ben Voigt.

Share:
32,218
Ghanashyam Chakravarthi
Author by

Ghanashyam Chakravarthi

File Systems and Computational Methods

Updated on July 09, 2022

Comments

  • Ghanashyam Chakravarthi
    Ghanashyam Chakravarthi almost 2 years

    Hi I have to run a program in C++ and I want to make sure when the program is executed, it opens the console in a particular size/dimensions, so that the display in my program is proper. I need help as I don't know how to do it. I am using Dev C++ 5.42(Orwell). I tried using

    #include<iostream> 
    #include<windows.h> 
    
    using namespace std; 
    
    SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); 
    
    int main(){ 
        cout<<"Hello World"; 
    }
    

    and got an error

    [Error] expected constructor, destructor, or type conversion before '(' token
    

    I'm a beginner and hence I don't know much about these things.

    • Shoe
      Shoe over 10 years
      We need to see more of the code surrounding that line.
    • chris
      chris over 10 years
      Did you include <windows.h> for that function? And the console window doesn't take too kindly to being resized beyond its max dimensions, so keep that in mind.
    • Adam Rosenfield
      Adam Rosenfield over 10 years
      If you're trying to change the size of a console window, use SetConsoleDisplayMode()
    • enhzflep
      enhzflep over 10 years
      Further to the other comments, even if it did compile and run, what exactly do you think the flags SWP_NOMOVE and SWP_NOSIZE do? (Hint: exactly what they say.) You use one or the other or neither, but not both - unless you're trying to change the Z-order of the window without altering its size or position on screen.
    • Ghanashyam Chakravarthi
      Ghanashyam Chakravarthi over 10 years
      @Jefffrey the full code is as follows #include<iostream> #include<windows.h> using namespace std; SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); int main(){ cout<<"Hello World"; }
    • Ghanashyam Chakravarthi
      Ghanashyam Chakravarthi over 10 years
      @AdamRosenfield can you give me an example on how to use the function SetConsoleDisplayMode() ?
    • chris
      chris over 10 years
      Putting code before main doesn't make it execute before main. Just put it at the start of main unless you truly have a good reason for why it needs to run before main, in which case there are methods available to make that happen.
    • Ghanashyam Chakravarthi
      Ghanashyam Chakravarthi over 10 years
      @enhzflep it is still giving the same error. Btw I got the line of code from the internet, so I don't know what does what.
    • Ghanashyam Chakravarthi
      Ghanashyam Chakravarthi over 10 years
      @chris now it says hwnd not declared
    • chris
      chris over 10 years
      @GhanashyamBC, Well, you need a proper handle to the window. One of the console functions provides it, but really, hwnd is just a normal variable, and like any other, it needs to be declared, set/initialized, etc.
    • Ghanashyam Chakravarthi
      Ghanashyam Chakravarthi over 10 years
      so how is it declared ?
    • Ben Voigt
      Ben Voigt over 10 years
      possible duplicate of Reducing console size
    • IInspectable
      IInspectable over 10 years
      C++ is not a Do-What-I-Mean programming language. Compilers are very particular about syntax. Read The Definitive C++ Book Guide and List - Introductory.