Ghanashyam Chakravarthi Ghanashyam Chakravarthi - 1 month ago 11
C++ Question

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

Answer

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/