1

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.

Yoav Feuerstein
  • 1,925
  • 2
  • 22
  • 53
ghanashyam
  • 85
  • 1
  • 3
  • 13
  • We need to see more of the code surrounding that line. – Shoe Jan 16 '14 at 05:38
  • Did you include `` for that function? And the console window doesn't take too kindly to being resized beyond its max dimensions, so keep that in mind. – chris Jan 16 '14 at 05:40
  • 1
    If you're trying to change the size of a *console* window, use [`SetConsoleDisplayMode()`](http://msdn.microsoft.com/en-us/library/windows/desktop/ms686028%28v=vs.85%29.aspx) – Adam Rosenfield Jan 16 '14 at 05:46
  • 1
    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. – enhzflep Jan 16 '14 at 06:01
  • @Jefffrey the full code is as follows #include #include using namespace std; SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); int main(){ cout<<"Hello World"; } – ghanashyam Jan 16 '14 at 06:09
  • @AdamRosenfield can you give me an example on how to use the function SetConsoleDisplayMode() ? – ghanashyam Jan 16 '14 at 06:10
  • 1
    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. – chris Jan 16 '14 at 06:11
  • @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 Jan 16 '14 at 06:13
  • @chris now it says hwnd not declared – ghanashyam Jan 16 '14 at 06:14
  • @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. – chris Jan 16 '14 at 06:20
  • so how is it declared ? – ghanashyam Jan 16 '14 at 06:40
  • possible duplicate of [Reducing console size](http://stackoverflow.com/questions/12900713/reducing-console-size) – Ben Voigt Jan 16 '14 at 06:47
  • C++ is not a Do-What-I-Mean programming language. Compilers are very particular about syntax. Read [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1889329) - Introductory. – IInspectable Jan 16 '14 at 10:36

3 Answers3

7

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/

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
3

This works for me:

HWND hwnd = GetConsoleWindow();
if( hwnd != NULL ){ MoveWindow(hwnd ,340,550 ,680,150 ,TRUE); }
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
DaveWalley
  • 817
  • 10
  • 22
1

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.

Samarth S
  • 313
  • 4
  • 5