Hi i really want to set the window position of my console applicaton
I've managed to find Console:: SetWindowPosition bu using google but cant find how to use it...
http://msdn2.microsoft.com/en-us/library/system.console.setwindowposition.aspx
This link shows an example.
But it wont compile
I'm guessing its missing header files, what are they?

I dont realy care how i do it, o if there is another way, then that would be great

Dani AI

Generated

You are mixing three different things: moving the desktop window, panning the console’s viewport, and moving the text cursor. The .NET call Console::SetWindowPosition only repositions the view rectangle inside the console buffer; it does not move the console window on the desktop. To move the actual window in native C++, grab the console’s HWND and reposition it. Also note that width/height for window-positioning APIs are pixels, not character cells, which is why values like 55x55 do not do what you expect. As @Ancient Dragon suggested, get the handle and reposition it; just be aware that GetConsoleWindow can return NULL if no console is attached. (learn.microsoft.com)

A minimal example that preserves the current size and only moves the window:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>

#pragma comment(lib, "User32.lib") // MSVC: link User32 without changing project settings

int main() {
    HWND hwnd = GetConsoleWindow();
    if (!hwnd) { std::cerr << "No console window attached.\n"; return 1; }

    ShowWindow(hwnd, SW_SHOWNOACTIVATE);                           // ensure it is shown
    SetWindowPos(hwnd, nullptr, 100, 100, 0, 0,                    // move to (100,100)
                 SWP_NOZORDER | SWP_NOSIZE);                       // do not resize
    std::cin.get();
    return 0;
}

Build notes and gotchas:

  • Link against User32 (for SetWindowPos/ShowWindow). With MSVC the pragma above is enough; otherwise add User32.lib in Linker->Input. (learn.microsoft.com)
  • If you see “identifier not found” for GetConsoleWindow, define _WIN32_WINNT to at least 0x0500 before including windows.h, as the header gates that API by target OS. (learn.microsoft.com)
  • ’s gotoxy example moves the cursor, not the window; it ultimately calls SetConsoleCursorPosition. Useful for text layout, but unrelated to window placement. (learn.microsoft.com)

Recommended Answers

All 7 Replies

anyone?

Here are the win32 api console functions. include windows.h and you will probably have to have the Windows Platform SDK, free for download from Microsoft.com

First call GetConsoleWindow() to get the handle to the console window, they you can call MoveWindow() to move it to wherever you want. I have not done it myself with a console window.

Is this how you use it?
It won't work!!!!
thanks

#include <iostream>
#include "Windows.h"
#include "Wincon.h"

//
int main()
{
  HWND a;

//a = GetConsoleWindow();
  //HWND WINAPI GetConsoleWindow(void);
a = GetConsoleWindow();
   MoveWindow(a,1,1,55,55,1);

} // end Mai

this works with VC++ 2005 Express and the Windows Platform SDK installed You have to add User32.lib to the list of link libraries.

#include <windows.h>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    HWND hWnd = GetConsoleWindow();
    MoveWindow(hWnd,100,100,300,400,TRUE);
    cin.ignore();
	return 0;
}

I get these errors:
error C3861: 'GetConsoleWindow': identifier not found, even with argument-dependent lookup
error C2061: syntax error : identifier '_TCHAR'

I'm have not got windows sdk installed, checked and its like 800mb, is it possible just to download the specific library?
thanks

this works with VC++ 2005 Express and the Windows Platform SDK installed You have to add User32.lib to the list of link libraries.

#include <windows.h>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    HWND hWnd = GetConsoleWindow();
    MoveWindow(hWnd,100,100,300,400,TRUE);
    cin.ignore();
    return 0;
}

I don't think you can get just part of the SDK. But I think you can get it on CD for just a few $$$.

The _TCHAR allows the compiler to compile for either UNICODE or not. If your compiler doesn't understand that macro you can replace it with char

try this it will work!


#include<iostream>
#include<windows.h>
using namespace std;
void gotoxy(int a, int b);
int main()
{
    cout << "Cursor before:" << endl;
    gotoxy(23, 12);
    cout << "Cursor after:";
}

void gotoxy(int x, int y)
{
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(hStdOut, coord);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.