コンソールのテキストの色を特定の色に設定し、1 行 (またはそれ以上) を印刷してから、配色を元の色に戻そうとしています。ここに私が持っているものがあります:
Function SetConsoleTextColor(NewColor As UInt16) As UInt16
Declare Function SetConsoleTextAttribute Lib "Kernel32" (hConsole As Integer, attribs As UInt16) As Boolean
Declare Function GetStdHandle Lib "Kernel32" (hIOStreamType As Integer) As Integer
Declare Function GetConsoleScreenBufferInfo Lib "Kernel32" (hConsole As Integer, ByRef buffinfo As CONSOLE_SCREEN_BUFFER_INFO) As Boolean
Declare Sub CloseHandle Lib "Kernel32" (HWND As Integer)
Const STD_OUTPUT_HANDLE = -12
Dim conHandle As Integer = GetStdHandle(STD_OUTPUT_HANDLE)
Dim buffInfo As CONSOLE_SCREEN_BUFFER_INFO //A structure defined elsewhere
If GetConsoleScreenBufferInfo(conHandle, buffInfo) Then
Call SetConsoleTextAttribute(conHandle, NewColor)
CloseHandle(conHandle)
Return buffInfo.Attribute
Else
Return 0
End If
End Function
これは、最初の呼び出しで問題なく機能します。コンソールの新しい出力のテキストの色が変更され、以前の属性が返されます。ただし、これをもう一度呼び出して属性をリセットするGetStdHandle
と、前の呼び出しと同じハンドルが返されますが、これは無効になっています (閉じているため)。
もちろん、ハンドルを使用しようとすると、これによりエラーが発生します。静的変数を作成し、ゼロ (RealBasic の新しい数値変数のデフォルト値) に等しい場合conHandle
にのみ呼び出すと、正しく機能します。GetStdHandle
conHandle
私はいつも自分で掃除するように言われました。このハンドルは開けたままにしておくべきですか?