以下はコンソールウィンドウの大きさとフォントサイズを変更するプログラムです。
► スポイラーを表示
#include <stdio.h>
#include <stdlib.h>
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#endif
#ifndef OEMRESOURCE
#define OEMRESOURCE
#endif
#ifndef STRICT
#define STRICT
#endif
#include <windows.h>
namespace fw
{
class exerror
{
private:
char * str;
public:
exerror(){ str = NULL; }
const char * get(){
if (str != NULL){ LocalFree(str); str = NULL; }
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPSTR>(&str), // こうしなければ成功しない。理由は不明
0,
NULL
);
return str;
}
const char * show(){ return str; }
~exerror(){ if (str != NULL){ LocalFree(str); str = NULL; } }
};
}
HANDLE GetConsoleH(DWORD mode)
{
SECURITY_ATTRIBUTES secuatt = {
sizeof(SECURITY_ATTRIBUTES),
NULL, // 呼び出しプロセスのデフォルトのセキュリティデスクリプタを使用
TRUE }; // 新プロセス作成時、ハンドルを継承
return CreateConsoleScreenBuffer(
mode,
0, // スクリーンバッファを共有しない
&secuatt,
CONSOLE_TEXTMODE_BUFFER,
NULL // 予約領域。常にNULL
);
}
HANDLE GetConsoleH_i()
{
return GetConsoleH(GENERIC_READ);
}
HANDLE GetConsoleH_o()
{
return GetConsoleH(GENERIC_WRITE);
}
int main()
{
const int fontW = 5;
const int fontH = 8;
const int winW = 640;
const int winH = 480;
BOOL result;
BOOL MaxWin = FALSE; // current window
HANDLE oConsoleH;
// 現在のフォントサイズを取得(プログラム終了後、プログラム開始前の状態に戻すため)__________________________________________
//
HANDLE iConsoleH = GetConsoleH_i();
if (iConsoleH == INVALID_HANDLE_VALUE) // 失敗
{
printf("失敗:GetConsoleH_i()");// todo
fw::exerror error;
printf("error:\n%s\n", error.get());
}
CONSOLE_FONT_INFOEX FontInfo;
result = GetCurrentConsoleFontEx(iConsoleH, MaxWin, &FontInfo);
if (result == 0) // 失敗
{
printf("失敗:GetCurrentConsoleFontEx(iConsoleH, MaxWin, &FontInfo)\n");
fw::exerror error;
printf("error:\n%s\n", error.get() );
}
CloseHandle(iConsoleH);
//
// _______________________________________________________________________________________________________________________________
oConsoleH = GetConsoleH_o();
if (oConsoleH == INVALID_HANDLE_VALUE) // 失敗
{
printf("失敗:GetConsoleH_o()");// todo
fw::exerror error;
printf("error:\n%s\n", error.get());
}
// 画面サイズを設定 ________________________________________________________________________________________________
//
SMALL_RECT rect;
rect.Top = 0;
rect.Left = 0;
rect.Right = winW / fontW - 1; // ウインドウの横幅をwinW/fontWにしたいから-1した座標を指定している
rect.Bottom = winH / fontH - 1; // ウインドウの縦幅をwinH/fontHにしたいから-1した座標を指定している
result = SetConsoleWindowInfo(oConsoleH, TRUE/*絶対位置で指定*/, &rect);
if (result == 0) // 失敗
{
printf("失敗:SetConsoleWindowInfo(oConsoleH, TRUE/*絶対位置で指定*/, &rect)\n");
fw::exerror error;
printf("error:\n%s\n", error.get());
}
//
// __________________________________________________________________________________________________________________
// フォントサイズを設定 ___________________________________________________________________________________________________________
//
CONSOLE_FONT_INFOEX NewFontInfo = FontInfo;
COORD & fontsize = NewFontInfo.dwFontSize;
fontsize.X = fontW;
fontsize.Y = fontH;
result = SetCurrentConsoleFontEx(oConsoleH, MaxWin, &NewFontInfo);
if (result == 0) // 失敗
{
printf("失敗:SetCurrentConsoleFontEx(oConsoleH, MaxWin, &NewFontInfo)\n");
fw::exerror error;
printf("error:\n%s\n", error.get());
}
//
// ______________________________________________________________________________________________________________________________
CloseHandle(oConsoleH);
getchar();
// フォントサイズをプログラム開始前の状態に戻す
oConsoleH = GetConsoleH_o();
if (oConsoleH == INVALID_HANDLE_VALUE) // 失敗
{
printf("失敗:GetConsoleH_o()");// todo
fw::exerror error;
printf("error:\n%s\n", error.get());
}
result = SetCurrentConsoleFontEx(oConsoleH, MaxWin, &FontInfo);
if (result == 0) // 失敗
{
printf("失敗:SetCurrentConsoleFontEx(oConsoleH, MaxWin, &FontInfo)\n");
fw::exerror error;
printf("error:\n%s\n", error.get());
}
CloseHandle(oConsoleH);
return 0;
}
失敗:GetCurrentConsoleFontEx(iConsoleH, MaxWin, &FontInfo)
error:
パラメーターが間違っています。
失敗:SetConsoleWindowInfo(oConsoleH, TRUE/*絶対位置で指定*/, &rect)
error:
パラメーターが間違っています。
失敗:SetCurrentConsoleFontEx(oConsoleH, MaxWin, &NewFontInfo)
error:
パラメーターが間違っています。
そして、パラメーターが間違っているらしいということもわかりました。
私には何がどう間違っているのかわかりません。
わかる方が居られましたらご回答おねがい致します。