A set of functions for creating a console and reading/writing it. Example code included.
Functions
AllocConsole - Creates the console.
FreeConsole - Destroys the console.
ConsoleInput - Gets a line of input entered in the console. Waits until the return key is pressed.
WriteConsole - Writes a string of text to the console. Supports \n and \r for newline and carriage return, respectively.
SetConsoleCursorPosition - Sets the cursor position. The coordinates of the top left of the console are (0, 0).
ClearConsole - Erases everything in the console and resets the cursor position.
SetConsoleTitle - Sets the console's title.
ProcessText - Used by WriteConsole to process \n and \r.
Language:
Dark Basic Pro
Code:
` Use a constant for the DLL number #constant KERNEL32_DLL 255 ` Global variables to store screen buffer handles and other stuff global CONSOLE_INPUT_BUFFER as dword global CONSOLE_OUTPUT_BUFFER as dword global consoleRunning as boolean = 1 ` Change to windowed mode if necessary and then hide it set window on hide window ` Create the console and change its title AllocConsole() SetConsoleTitle("Console Example") ` Main processing loop repeat ` This is the equivalent of DBP's CLS ClearConsole() ` Give the user options and wait for input WriteConsole("Please enter an option:\n\n") WriteConsole(" Display a random number (1)\n") WriteConsole(" Close console (2)\n") WriteConsole(" Quit application (3)\n\n") option = val(left$(ConsoleInput(), 1)) ` Handle option select option ` (1) Random number case 1 WriteConsole("Enter a maximum value for the random number: ") maxValue = val(ConsoleInput()) WriteConsole("Random number: "+str$(rnd(maxValue))+"\n") ` Wait for user to hit enter ConsoleInput() endcase ` (2) Close console case 2 consoleRunning = 0 endcase ` (3) Quit application case 3 WriteConsole("Quiting...\n") wait 1000 end endcase ` (?) case default print "Unknown option." ConsoleInput() endcase endselect until not consoleRunning ` Destroy the console and show window FreeConsole() show window print "Console destroyed. Press any key to exit." wait key end function AllocConsole ` Creates the console if not dll exist(KERNEL32_DLL) then load dll "kernel32.dll", KERNEL32_DLL call dll KERNEL32_DLL, "AllocConsole" CONSOLE_INPUT_BUFFER = call dll(KERNEL32_DLL, "GetStdHandle", -10) CONSOLE_OUTPUT_BUFFER = call dll(KERNEL32_DLL, "GetStdHandle", -11) endfunction function FreeConsole ` Destroys the console call dll KERNEL32_DLL, "FreeConsole" endfunction function ConsoleInput() ` Gets a line of text input local entryData as string local pBytesRead as dword entryData = space$(255) pBytesRead = make memory(4) call dll KERNEL32_DLL, "ReadConsoleA", CONSOLE_INPUT_BUFFER, entryData, 255, pBytesRead, 0 ` Strip off remaining spaces and CRLF entryData = left$(entryData, (*pBytesRead)-2) delete memory pBytesRead endfunction entryData function WriteConsole(strOutput as string) ` Writes a string to the console strOutput = ProcessText(strOutput) call dll KERNEL32_DLL, "WriteConsoleA", CONSOLE_OUTPUT_BUFFER, strOutput, len(strOutput), 0, 0 endfunction function SetConsoleCursorPosition(x as word, y as word) ` Sets the cursor position call dll KERNEL32_DLL, "SetConsoleCursorPosition", CONSOLE_OUTPUT_BUFFER, x, y endfunction function ClearConsole() ` Erases the entire console window and resets cursor position local tempPtr as dword local width as word local height as word tempPtr = make memory(22) ` Get screen buffer information if not call dll(KERNEL32_DLL, "GetConsoleScreenBufferInfo", CONSOLE_OUTPUT_BUFFER, tempPtr) delete memory tempPtr exitfunction endif ` Get screen buffer dimensions width = (*tempPtr) && 0xFF height = ((*tempPtr) && 0xFFFF0000) >> 16 ` Fill the console with blanks call dll KERNEL32_DLL, "FillConsoleOutputCharacterA", CONSOLE_OUTPUT_BUFFER, 0, width*height, 0, 0, tempPtr ` Reset cursor position SetConsoleCursorPosition(0, 0) delete memory tempPtr endfunction function SetConsoleTitle(title as string) ` Sets the console title call dll KERNEL32_DLL, "SetConsoleTitleA", title endfunction function ProcessText(strText as string) ` Processes \n and \r strLen = len(strText) processIt = 0 for x = 1 to strLen char$ = mid$(strText, x) if processIt = 0 and char$ = "\" processIt = 1 else if processIt select char$ case "n" output$ = output$ + chr$(13) + chr$(10) endcase case "r" output$ = output$ + chr$(13) endcase endselect processIt = 0 else output$ = output$ + char$ endif endif next x endfunction output$
(1 vote)