Help....Thanks.
4:05 pm......9:40 pm....
/*---------------------------------------------------------------------------
File name: conFS.cpp
Author: HJin (email: fish_sea_bird [at] yahoo [dot] com )
Created on: 7/16/2007 23:49:08
Environment: Windows XP Professional SP2 English +
Visual Studio 2005 v8.0.50727.762
Modification history:
===========================================================================
Problem statement:
---------------------------------------------------------------------------
怎么让C++写的小程序在双击运行之后立刻变为全屏?
Analysis:
---------------------------------------------------------------------------
This is some old school for dos age.
There are generally two options for you to finish this task:
1) call SetConsoleDisplayMode();
2) send an input sequence Alt+Enter using SendInput().
You may google them to find huge references.
That has been said, I use neither. Instead I use a library found at
http://home.arcor.de/bjoern.bilger/schlange-quellcode/ic.hpp
Sample output:
---------------------------------------------------------------------------
full screen console application.
Reference:
---------------------------------------------------------------------------
1. http://bbs.bc-cn.net/viewthread.php?tid=155795
*/
#include <iostream>
#include "ic.hpp"
using namespace std;
int main()
{
int i=10;
char str[] = "c++ full screen console application.";
// enable full screen mode for the console
ic::con.enableWndFSMode();
cout<<i<<endl;
cout<<str<<endl;
getchar();
return 0;
}
ic.hpp
#ifndef INCLUDE_GUARD_IC_HPP
#define INCLUDE_GUARD_IC_HPP///// Includes /////////////////////////////////////////////////////////////////
#define _WIN32_WINNT 0x0500
#include <string>
#include <tchar.h>
#include <windows.h>
///////////////////////////////////////////////////////////////////////////////////// Macros ///////////////////////////////////////////////////////////////////
#ifndef CONSOLE_FULLSCREEN_MODE
#define CONSOLE_FULLSCREEN_MODE 1
#endif
#ifndef CONSOLE_WINDOWED_MODE
#define CONSOLE_WINDOWED_MODE 2
#endif
#ifndef min
#define min(a, b) (((a) < (b)) ? (a) : (b))
#endif
////////////////////////////////////////////////////////////////////////////////namespace ic
{
///// Color ////////////////////////////////////////////////////////////////
typedef int Color;
///////////////////////////////////////////////////////////////////////////////// TextColor ////////////////////////////////////////////////////////////
enum TextColor
{
FG_BLACK = 0,
FG_DARKRED = FOREGROUND_RED,
FG_DARKGREEN = FOREGROUND_GREEN,
FG_DARKBLUE = FOREGROUND_BLUE,
FG_OCHER = FOREGROUND_RED | FOREGROUND_GREEN,
FG_VIOLET = FOREGROUND_RED | FOREGROUND_BLUE,
FG_TURQUOISE = FOREGROUND_GREEN | FOREGROUND_BLUE,
FG_GREY = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,FG_DARKGREY = FOREGROUND_INTENSITY | FG_BLACK,
FG_RED = FOREGROUND_INTENSITY | FG_DARKRED,
FG_GREEN = FOREGROUND_INTENSITY | FG_DARKGREEN,
FG_BLUE = FOREGROUND_INTENSITY | FG_DARKBLUE,
FG_YELLOW = FOREGROUND_INTENSITY | FG_OCHER,
FG_PINK = FOREGROUND_INTENSITY | FG_VIOLET,
FG_LIGHTBLUE = FOREGROUND_INTENSITY | FG_TURQUOISE,
FG_WHITE = FOREGROUND_INTENSITY | FG_GREY
};
///////////////////////////////////////////////////////////////////////////////// BgColor //////////////////////////////////////////////////////////////
enum BgColor
{
BG_BLACK = 0,
BG_DARKRED = BACKGROUND_RED,
BG_DARKGREEN = BACKGROUND_GREEN,
BG_DARKBLUE = BACKGROUND_BLUE,
BG_OCHER = BACKGROUND_RED | BACKGROUND_GREEN,
BG_VIOLET = BACKGROUND_RED | BACKGROUND_BLUE,
BG_TURQUOISE = BACKGROUND_GREEN | BACKGROUND_BLUE,
BG_GREY = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE,BG_DARKGREY = BACKGROUND_INTENSITY | BG_BLACK,
BG_RED = BACKGROUND_INTENSITY | BG_DARKRED,
BG_GREEN = BACKGROUND_INTENSITY | BG_DARKGREEN,
BG_BLUE = BACKGROUND_INTENSITY | BG_DARKBLUE,
BG_YELLOW = BACKGROUND_INTENSITY | BG_OCHER,
BG_PINK = BACKGROUND_INTENSITY | BG_VIOLET,
BG_LIGHTBLUE = BACKGROUND_INTENSITY | BG_TURQUOISE,
BG_WHITE = BACKGROUND_INTENSITY | BG_GREY
};
///////////////////////////////////////////////////////////////////////////////// Console //////////////////////////////////////////////////////////////
class Console
{
// Origin of the coordinate system
static const COORD origin;
// Handle: Console window
HWND hWnd;
// Handle: Standard output device
HANDLE hConsoleOutput;
// Trigger: Window buffered mode
bool wndBufMode;
// Already implemented functions without declaration in <windows.h>
typedef BOOL (WINAPI *SETCONSOLEDISPLAYMODE) (HANDLE, DWORD, PCOORD);
SETCONSOLEDISPLAYMODE SetConsoleDisplayMode;
// Constructors
Console ();
public:
~Console ();// Singleton access
static Console& getInstance ();// Hide/Show console
void hide ();
void show ();// Minimize/Maximize/Restore console
void minimize ();
void maximize ();
void restore ();// Clear console (Only color, only text or both)
void clearColor (Color color = FG_WHITE | BG_BLACK);
void clearText (TCHAR character = TEXT(' '));
void clear (Color color = FG_WHITE | BG_BLACK, TCHAR character = TEXT(' '));// Get/Set: Color
Color getColor () const;
void setColor (Color color);// Get/Set: Text color
TextColor getTextColor () const;
void setTextColor (TextColor color);// Get/Set: Background color
BgColor getBgColor () const;
void setBgColor (BgColor color);// Get/Set: Cursor position
int getCurPosX () const;
int getCurPosY () const;
void setCurPos (int x, int y);// Get/Set: Cursor size
int getCurSize () const;
void setCurSize (int size);// Enable/Disable: Window buffered mode
bool isWndBufMode () const;
void enableWndBufMode ();
void disableWndBufMode ();// Enable/Disable: Window fullscreen mode
bool isWndFSMode () const;
void enableWndFSMode ();
void disableWndFSMode ();// Get/Set: Window position
int getWndPosX () const;
int getWndPosY () const;
void setWndPos (int x, int y);// Get/Set: Window buffer size
int getWndBufSizeX () const;
int getWndBufSizeY () const;
void setWndBufSize (int x, int y);// Get/Set: Window size
int getWndSizeX () const;
int getWndSizeY () const;
void setWndSize (int x, int y);// Get: Maximal window size
int getMaxWndSizeX () const;
int getMaxWndSizeY () const;// Get/Set: Title
std::basic_string<TCHAR> getTitle () const;
void setTitle (const std::basic_string<TCHAR>& title);
private:
// Helper
CONSOLE_CURSOR_INFO getCCI () const;
CONSOLE_SCREEN_BUFFER_INFO getCSBI () const;// Helper: getTextColor()/getBgColor()
inline BgColor getBgColor (Color color) const { return static_cast<BgColor>(color & 0xF0); }
inline TextColor getTextColor (Color color) const { return static_cast<TextColor>(color & 0x0F); }// Helper: setWndSize()
void zeroWndSize ();// Forbidden
Console (const Console&);
Console& operator= (const Console&);
};extern Console& con;
///////////////////////////////////////////////////////////////////////////////// Shorties /////////////////////////////////////////////////////////////
namespace shorties
{
inline void hide () { con.hide(); }
inline void show () { con.show(); }inline void minimize () { con.minimize(); }
inline void maximize () { con.maximize(); }
inline void restore () { con.restore(); }inline void clrcol (Color color = FG_WHITE | BG_BLACK) { con.clearColor(color); }
inline void clrtext (TCHAR character = TEXT(' ')) { con.clearText(character); }
inline void clrscr (Color color = FG_WHITE | BG_BLACK, TCHAR character = TEXT(' ')) { con.clear(color,character); }inline void textcolor (TextColor color) { con.setTextColor(color); }
inline void bgcolor (BgColor color) { con.setBgColor(color); }inline void gotoxy (int x, int y) { con.setCurPos(x,y); }
inline void home () { con.setCurPos(0,0); }
inline void cursize (int size) { con.setCurSize(size); }inline void resize (int x, int y) { con.setWndSize(x,y); }
inline void title (const std::basic_string<TCHAR>& title) { con.setTitle(title); }
}
////////////////////////////////////////////////////////////////////////////
}#endif
///// Includes /////////////////////////////////////////////////////////////////
#include \"ic.hpp\"
////////////////////////////////////////////////////////////////////////////////namespace ic
{
///// Console //////////////////////////////////////////////////////////////
const COORD Console::origin = {0,0};Console::Console ()
: hWnd(GetConsoleWindow())
, hConsoleOutput(GetStdHandle(STD_OUTPUT_HANDLE))
, wndBufMode(false)
{
HMODULE kernel32 = GetModuleHandle(TEXT(\"kernel32.dll\"));
SetConsoleDisplayMode = reinterpret_cast<SETCONSOLEDISPLAYMODE>(GetProcAddress(kernel32,\"SetConsoleDisplayMode\"));disableWndBufMode();
setWndPos(100,100);
setWndSize(80,25);
clear();
}Console::~Console ()
{
enableWndBufMode();
}Console& Console::getInstance ()
{
static Console instance;
return instance;
}void Console::hide ()
{
ShowWindow(hWnd,SW_HIDE);
}void Console::show ()
{
ShowWindow(hWnd,SW_SHOW);
}void Console::minimize ()
{
ShowWindow(hWnd,SW_MINIMIZE);
}void Console::maximize ()
{
ShowWindow(hWnd,SW_MAXIMIZE);
}void Console::restore ()
{
ShowWindow(hWnd,SW_NORMAL);
}void Console::clearColor (Color color)
{
DWORD attrsWritten;
FillConsoleOutputAttribute(hConsoleOutput,color,getWndBufSizeX()*getWndBufSizeY(),origin,&attrsWritten);
}void Console::clearText (TCHAR character)
{
DWORD charsWritten;
FillConsoleOutputCharacter(hConsoleOutput,character,getWndBufSizeX()*getWndBufSizeY(),origin,&charsWritten);
}void Console::clear (Color color, TCHAR character)
{
clearColor(color);
clearText(character);
}TextColor Console::getTextColor () const
{
return getTextColor(getCSBI().wAttributes);
}void Console::setTextColor (TextColor color)
{
SetConsoleTextAttribute(hConsoleOutput,color|getBgColor());
}BgColor Console::getBgColor () const
{
return getBgColor(getCSBI().wAttributes);
}void Console::setBgColor (BgColor color)
{
SetConsoleTextAttribute(hConsoleOutput,getTextColor()|color);
}Color Console::getColor () const
{
return getCSBI().wAttributes;
}void Console::setColor (Color color)
{
SetConsoleTextAttribute(hConsoleOutput,color);
}int Console::getCurPosX () const
{
return getCSBI().dwCursorPosition.X;
}int Console::getCurPosY () const
{
return getCSBI().dwCursorPosition.Y;
}void Console::setCurPos (int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hConsoleOutput,pos);
}int Console::getCurSize () const
{
CONSOLE_CURSOR_INFO cci = getCCI();if(!cci.bVisible)
return 0;return cci.dwSize;
}void Console::setCurSize (int size)
{
CONSOLE_CURSOR_INFO cci;if(size > 0)
{
cci.bVisible = TRUE;
cci.dwSize = size;
}
else
{
cci.bVisible = FALSE;
cci.dwSize = 100;
}SetConsoleCursorInfo(hConsoleOutput,&cci);
}bool Console::isWndBufMode () const
{
return wndBufMode;
}void Console::enableWndBufMode ()
{
SetConsoleMode(hConsoleOutput,ENABLE_PROCESSED_OUTPUT|ENABLE_WRAP_AT_EOL_OUTPUT);
wndBufMode = true;
}void Console::disableWndBufMode ()
{
SetConsoleMode(hConsoleOutput,ENABLE_PROCESSED_OUTPUT);
setWndBufSize(getWndSizeX()+1,getWndSizeY()+1);
wndBufMode = false;
}bool Console::isWndFSMode () const
{
DWORD flags;
GetConsoleDisplayMode(&flags);return flags & CONSOLE_FULLSCREEN_MODE;
}void Console::enableWndFSMode ()
{
COORD newScreenBufferDimensions;
SetConsoleDisplayMode(hConsoleOutput,CONSOLE_FULLSCREEN_MODE,&newScreenBufferDimensions);
}void Console::disableWndFSMode ()
{
COORD newScreenBufferDimensions;
SetConsoleDisplayMode(hConsoleOutput,CONSOLE_WINDOWED_MODE,&newScreenBufferDimensions);
}int Console::getWndPosX () const
{
RECT rect;
GetWindowRect(hWnd,&rect);return rect.left;
}int Console::getWndPosY () const
{
RECT rect;
GetWindowRect(hWnd,&rect);return rect.top;
}void Console::setWndPos (int x, int y)
{
SetWindowPos(hWnd,0,x,y,0,0,SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
}int Console::getWndBufSizeX () const
{
return getCSBI().dwSize.X;
}int Console::getWndBufSizeY () const
{
return getCSBI().dwSize.Y;
}void Console::setWndBufSize (int x, int y)
{
if(!wndBufMode)
return;COORD size;
size.X = x;
size.Y = y;
SetConsoleScreenBufferSize(hConsoleOutput,size);
}int Console::getWndSizeX () const
{
return getCSBI().srWindow.Right - getCSBI().srWindow.Left + 1;
}int Console::getWndSizeY () const
{
return getCSBI().srWindow.Bottom - getCSBI().srWindow.Top + 1;
}void Console::setWndSize (int x, int y)
{
if(!wndBufMode)
{
zeroWndSize();COORD bufSize;
bufSize.X = min(x,getMaxWndSizeX());
bufSize.Y = min(y,getMaxWndSizeY());
SetConsoleScreenBufferSize(hConsoleOutput,bufSize);
}SMALL_RECT wndSize;
wndSize.Top = 0;
wndSize.Left = 0;
wndSize.Right = min(x,getMaxWndSizeX()) - 1;
wndSize.Bottom = min(y,getMaxWndSizeY()) - 1;
SetConsoleWindowInfo(hConsoleOutput,TRUE,&wndSize);
}int Console::getMaxWndSizeX () const
{
return GetLargestConsoleWindowSize(hConsoleOutput).X;
}int Console::getMaxWndSizeY () const
{
return GetLargestConsoleWindowSize(hConsoleOutput).Y;
}std::basic_string<TCHAR> Console::getTitle () const
{
const int MAX_TITLE_LEN = 64 * 1024;TCHAR title [MAX_TITLE_LEN];
GetConsoleTitle(title,MAX_TITLE_LEN);return std::basic_string<TCHAR>(title);
}void Console::setTitle (const std::basic_string<TCHAR>& title)
{
SetConsoleTitle(title.c_str());
}CONSOLE_CURSOR_INFO Console::getCCI () const
{
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hConsoleOutput,&cci);return cci;
}CONSOLE_SCREEN_BUFFER_INFO Console::getCSBI () const
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsoleOutput,&csbi);return csbi;
}void Console::zeroWndSize ()
{
SMALL_RECT wndSize;
wndSize.Top = 1;
wndSize.Left = 1;
wndSize.Right = 1;
wndSize.Bottom = 1;
SetConsoleWindowInfo(hConsoleOutput,TRUE,&wndSize);COORD bufSize;
bufSize.X = 1;
bufSize.Y = 1;
SetConsoleScreenBufferSize(hConsoleOutput,bufSize);
}Console& con = Console::getInstance();
////////////////////////////////////////////////////////////////////////////
}