Alternate e697dbe9c5997e35395fe158628dd8c5209481da
for Visual Studio 2022 and Windows 11.
読み取り中…
検索中…
一致する文字列を見つけられません
Console.cpp
[詳解]
1// ----------------------------------------------------------------------------
6
7#include "pch.h"
8#include "Console.h"
9
10using namespace alt;
11
14{
15 ZeroMemory (
17}
18
20{
21 ::SetConsoleTextAttribute (
23}
24
25BOOL Console::Create () const
26{
27 return ::AllocConsole ();
28}
29
31{
32 BOOL response = FALSE;
33
34 _hObject = ::GetStdHandle (STD_OUTPUT_HANDLE);
35 if (_hObject != INVALID_HANDLE_VALUE)
36 {
38 if (response != FALSE)
39 {
40 response = this->SetColor (ForeColor::White, BackColor::Black);
41 }
42 }
43
44 return response;
45}
46
47BOOL Console::SetColor (ForeColor foreColor, BackColor backColor) const
48{
49 WORD wAttribute = static_cast<int>(foreColor) | static_cast<int>(backColor);
50
51 return ::SetConsoleTextAttribute (_hObject, wAttribute);
52}
53
54DWORD Console::Write (LPCTSTR lpctszString) const
55{
56 DWORD dwWritten;
57 BOOL ret = ::WriteConsole (
58 _hObject, lpctszString, lstrlen (lpctszString), &dwWritten, NULL);
59
60 return dwWritten;
61}
62
63VOID Console::Format (LPCTSTR format, ...) const
64{
65 va_list args;
66 INT ilen;
67 LPTSTR lptszString;
68
69 va_start (args, format);
70
71 ilen = (_vsctprintf (format, args) + 1) * sizeof (TCHAR);
72 lptszString = new TCHAR[ilen];
73
74 if (lptszString != NULL)
75 {
76 _vstprintf_s (lptszString, ilen, format, args);
77 }
78
79 this->Write (lptszString);
80 delete[] lptszString;
81
82 va_end (args);
83}
84
85BOOL Console::SetTitle (LPCTSTR lpctszTitle) const
86{
87 return ::SetConsoleTitle (lpctszTitle);
88}
89
90BOOL Console::SetCursorPosition (SHORT x, SHORT y) const
91{
92 COORD coord = { x, y };
93
94 return ::SetConsoleCursorPosition (_hObject, coord);
95}
96
97BOOL Console::SetBufferSize (SHORT x, SHORT y) const
98{
99 COORD coord = { x, y };
100 DWORD dwNumbberOfCharsWritten;
101
102 ::WriteConsoleOutputCharacter (
103 _hObject, _T (" "), sizeof (TCHAR), coord, &dwNumbberOfCharsWritten);
104
105 return ::SetConsoleScreenBufferSize (_hObject, coord);
106}
107
108BOOL Console::SetWindowInfo (SHORT width, SHORT height) const
109{
110 SMALL_RECT rect = { 0, 0, width - 1, height - 1 };
111
112 return ::SetConsoleWindowInfo (_hObject, TRUE, &rect);
113}
114
115BOOL Console::Clear () const
116{
117 BOOL ret = FALSE;
118 CONSOLE_SCREEN_BUFFER_INFO csbi;
119
120 do
121 {
122 ret = this->GetScreenBufferInfo (csbi);
123 if (!ret) break;
124 DWORD dwSize = csbi.dwSize.X * csbi.dwSize.Y;
125
126 COORD coord = { 0, 0 };
127 DWORD dwWritten;
128 ret = FillConsoleOutputCharacter (
129 _hObject, (TCHAR)' ', dwSize, coord, &dwWritten);
130 if (!ret) break;
131
132 ret = this->GetScreenBufferInfo (csbi);
133 if (!ret) break;
134
135 ret = FillConsoleOutputAttribute (
136 _hObject, csbi.wAttributes, dwSize, coord, &dwWritten);
137 if (!ret) break;
138
139 ret = this->SetCursorPosition (0, 0);
140 } while (false);
141
142 return ret;
143}
144
145// private functions --------------------------------------------------
146
147BOOL Console::GetScreenBufferInfo (CONSOLE_SCREEN_BUFFER_INFO& csbi) const
148{
149 return ::GetConsoleScreenBufferInfo (_hObject, &csbi);
150}
コンソールに関するWindowsAPIを集約したクラス
プリコンパイル済みヘッダー ファイルです。
BOOL APIENTRY SetColor(ForeColor foreColor, BackColor backColor) const
文字色、背景色を設定します。
Definition: Console.cpp:47
DWORD APIENTRY Write(LPCTSTR lpctszString) const
コンソールに文字を出力します。
Definition: Console.cpp:54
APIENTRY ~Console()
デストラクタ
Definition: Console.cpp:19
BOOL APIENTRY SetTitle(LPCTSTR lpctszTitle) const
コンソールタイトルを設定します。
Definition: Console.cpp:85
BOOL APIENTRY SetCursorPosition(SHORT x, SHORT y) const
出力位置を設定します。
Definition: Console.cpp:90
BOOL APIENTRY Init()
Consoleクラスを初期化します。
Definition: Console.cpp:30
APIENTRY Console()
コンストラクタ
Definition: Console.cpp:12
BOOL APIENTRY Create() const
コンソール出力ウィンドウを作成します。
Definition: Console.cpp:25
BOOL APIENTRY GetScreenBufferInfo(CONSOLE_SCREEN_BUFFER_INFO &csbi) const
Definition: Console.cpp:147
VOID APIENTRY Format(LPCTSTR format,...) const
コンソールに文字を出力します。
Definition: Console.cpp:63
BOOL APIENTRY Clear() const
コンソールをクリアします。
Definition: Console.cpp:115
BOOL APIENTRY SetBufferSize(SHORT x, SHORT y) const
バッファサイズを設定します。
Definition: Console.cpp:97
BOOL APIENTRY SetWindowInfo(SHORT width, SHORT height) const
ウィンドウサイズを設定します。
Definition: Console.cpp:108
CONSOLE_SCREEN_BUFFER_INFO _prevConsoleScreenBufferInfo
コンソールウィンドウ出力情報
Definition: Console.h:141
HANDLEを扱うWindowsAPIを集約した基底クラス
HANDLE _hObject
ハンドルを使用するWindowsAPIで保持するHANDLE値
Definition: DBLibrary.h:12
ForeColor
コンソール出力時の文字色設定
Definition: Console.h:16
BackColor
コンソール出力時の背景色設定
Definition: Console.h:52