This example code show you, how to use the AllocConsole and FreeConsole (and other) API's to create a Console, write Text (in different colors) to it and how to get the user input. An replacement for PBCC in some cases?!?
------------------
e-mail: [email protected]
Code:
$compile exe "console.exe" $include "win32api.inc" DECLARE SUB CPrint (szText AS STRING, chwnd AS LONG) DECLARE FUNCTION RCon (chwnd AS LONG) AS STRING FUNCTION WinMain (BYVAL hInstance AS LONG, _ BYVAL hPrevInstance AS LONG, _ lpCmdLine AS ASCIIZ PTR, _ BYVAL iCmdShow AS LONG) AS LONG LOCAL hConIn AS LONG LOCAL hConOut AS LONG LOCAL hConErr AS LONG call AllocConsole 'create the console SetConsoleTitle "Creating a Console with PB/DLL" 'set the title hConIn = GetStdHandle(%STD_INPUT_HANDLE) 'get the handles hConOut = GetStdHandle(%STD_OUTPUT_HANDLE) hConErr = GetStdHandle(%STD_ERROR_HANDLE) SetConsoleTextAttribute hConOut, %FOREGROUND_RED Or %FOREGROUND_GREEN _ Or %FOREGROUND_BLUE Or %FOREGROUND_INTENSITY Or %BACKGROUND_BLUE CPrint "PB/DLL Console Demo by Sven Blumenstein" + chr$(13,10), hConOut CPrint "Comments to [email protected]" + chr$(13,10), hConOut SetConsoleTextAttribute hConOut, %FOREGROUND_RED Or _ %FOREGROUND_GREEN Or %FOREGROUND_BLUE CPrint "" + chr$(13,10), hConOut CPrint "Please enter your Name!" + chr$(13,10), hConOut yourname$ = RCon(hConIn) 'get the input if trim$(yourname$) = "" then CPrint "Hello, whoever you are!" + chr$(13,10), hConOut else CPrint "Hello, " + yourname$ + "!" + chr$(13,10), hConOut end if CPrint "" + chr$(13,10), hConOut CPrint "Press ENTER to exit!" + chr$(13,10), hConOut call RCon (hConIn) call FreeConsole 'kill the console End Function FUNCTION RCon (chwnd AS LONG) AS STRING DIM sInput AS STRING * 255 call ReadConsole(chwnd, sInput, len(sInput), %null, scrap&) 'get the input FUNCTION = Left$(sInput, InStr(sInput, Chr$(0)) - 3) END FUNCTION SUB CPrint (szText AS STRING, chwnd AS LONG) DIM szTxt AS ASCIIZ * 255 szTxt = szText WriteConsole chwnd, szTxt, len(szTxt), %null, %null 'write text to console END SUB
e-mail: [email protected]