ghanashyam
ghanashyam

Reputation: 85

How to change window size in C++?

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.

Upvotes: 1

Views: 39674

Answers (3)

DaveWalley
DaveWalley

Reputation: 807

This works for me:

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

Upvotes: 3

Samarth S
Samarth S

Reputation: 335

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.

Upvotes: 1

Ben Voigt
Ben Voigt

Reputation: 283911

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/

Upvotes: 7

Related Questions