Option Explicit '''''D E C L A R A T I O N S''''''''''''''''''''''''''''''''''''Private Declare Function AllocConsole Lib "kernel32" () As LongPrivate Declare Function FreeConsole Lib "kernel32" () As LongPrivate Declare Function GetStdHandle Lib "kernel32" _ (ByVal nStdHandle As Long) As LongPrivate Declare Function ReadConsole Lib "kernel32" Alias _ "ReadConsoleA" (ByVal hConsoleInput As Long, _ ByVal lpBuffer As String, ByVal nNumberOfCharsToRead As Long, _ lpNumberOfCharsRead As Long, lpReserved As Any) As Long'Private Declare Function SetConsoleMode Lib "kernel32" (ByVal _ 'hConsoleOutput As Long, dwMode As Long) As LongPrivate Declare Function SetConsoleTextAttribute Lib _ "kernel32" (ByVal hConsoleOutput As Long, ByVal _ wAttributes As Long) As LongPrivate Declare Function SetConsoleTitle Lib "kernel32" Alias _ "SetConsoleTitleA" (ByVal lpConsoleTitle As String) As LongPrivate Declare Function WriteConsole Lib "kernel32" Alias _ "WriteConsoleA" (ByVal hConsoleOutput As Long, _ ByVal lpBuffer As Any, ByVal nNumberOfCharsToWrite As Long, _ lpNumberOfCharsWritten As Long, lpReserved As Any) As Long''''C O N S T A N T S''''''''''''''''''''''''''''''''''''' 'I/O handlers for the console window. These are much like the 'hWnd handlers to form windows.Private Const STD_INPUT_HANDLE = -10& Private Const STD_OUTPUT_HANDLE = -11& Private Const STD_ERROR_HANDLE = -12&'Color values for SetConsoleTextAttribute. Private Const FOREGROUND_BLUE = &H1 Private Const FOREGROUND_GREEN = &H2 Private Const FOREGROUND_RED = &H4 Private Const FOREGROUND_INTENSITY = &H8 Private Const BACKGROUND_BLUE = &H10 Private Const BACKGROUND_GREEN = &H20 Private Const BACKGROUND_RED = &H40 Private Const BACKGROUND_INTENSITY = &H80'For SetConsoleMode (input) 'Private Const ENABLE_LINE_INPUT = &H2 'Private Const ENABLE_ECHO_INPUT = &H4 'Private Const ENABLE_MOUSE_INPUT = &H10 'Private Const ENABLE_PROCESSED_INPUT = &H1 'Private Const ENABLE_WINDOW_INPUT = &H8 ''For SetConsoleMode (output) 'Private Const ENABLE_PROCESSED_OUTPUT = &H1 'Private Const ENABLE_WRAP_AT_EOL_OUTPUT = &H2'''''G L O B A L S''''''''''''''''''''''''''''''''''' Private hIn As Long 'The console's input handle Private hOut As Long 'The console's output handle Private hErr As Long 'The console's error handle'''''M A I N''''''''''''''''''''''''''''''''''''''''' Private Sub Main() Dim szUserInput As String Dim Flag As Boolean
AllocConsole 'Create a console instance
SetConsoleTitle "VB Palindrome" 'Set the title on the console window 'Get the console's handle hIn = GetStdHandle(STD_INPUT_HANDLE) hOut = GetStdHandle(STD_OUTPUT_HANDLE) hErr = GetStdHandle(STD_ERROR_HANDLE)
'Print the prompt to the user. Use the vbCrLf to get to a new line. SetConsoleTextAttribute hOut, FOREGROUND_RED Or FOREGROUND_GREEN Or FOREGROUND_INTENSITY Or BACKGROUND_BLUE ConsolePrint "Search For Palindrome (C) Dipak Auddy." & vbCrLf & vbCrLf Do SetConsoleTextAttribute hOut, FOREGROUND_RED Or FOREGROUND_GREEN Or FOREGROUND_BLUE ConsolePrint vbCrLf & "Enter Word,Phrase or a Sentence[ENTER to Quit.]--> " 'Get the user's input szUserInput = ConsoleRead() If Not szUserInput = vbNullString Then Flag = IsPalin(szUserInput) If Flag Then SetConsoleTextAttribute hOut, FOREGROUND_GREEN ConsolePrint szUserInput & " -- IS PALINDROME." & vbCrLf Else SetConsoleTextAttribute hOut, FOREGROUND_RED ConsolePrint szUserInput & " -- IS NOT PALINDROME." & vbCrLf End If Else ConsolePrint "Good Bye!" Exit Do End If Loop 'Call ConsoleRead FreeConsole 'Destroy the console End Sub Private Sub ConsolePrint(szOut As String) WriteConsole hOut, szOut, Len(szOut), vbNull, vbNull End SubPrivate Function ConsoleRead() As String Dim sUserInput As String * 256 Call ReadConsole(hIn, sUserInput, Len(sUserInput), vbNull, vbNull) 'Trim off the NULL charactors and the CRLF. ConsoleRead = Left$(sUserInput, InStr(sUserInput, Chr$(0)) - 3) End FunctionFunction IsPalin(ByVal TXT As String) As Boolean Dim a As String 'Remove Space(s) from user input and make it Upper Case TXT = UCase(Replace(TXT, " ", "")) a = StrReverse(TXT) If TXT = a Then IsPalin = True Else IsPalin = False End If End Function 新建空工程,复制上面代码到模块中,并设置启动对象为Sub Main.
'''''D E C L A R A T I O N S''''''''''''''''''''''''''''''''''''Private Declare Function AllocConsole Lib "kernel32" () As LongPrivate Declare Function FreeConsole Lib "kernel32" () As LongPrivate Declare Function GetStdHandle Lib "kernel32" _
(ByVal nStdHandle As Long) As LongPrivate Declare Function ReadConsole Lib "kernel32" Alias _
"ReadConsoleA" (ByVal hConsoleInput As Long, _
ByVal lpBuffer As String, ByVal nNumberOfCharsToRead As Long, _
lpNumberOfCharsRead As Long, lpReserved As Any) As Long'Private Declare Function SetConsoleMode Lib "kernel32" (ByVal _
'hConsoleOutput As Long, dwMode As Long) As LongPrivate Declare Function SetConsoleTextAttribute Lib _
"kernel32" (ByVal hConsoleOutput As Long, ByVal _
wAttributes As Long) As LongPrivate Declare Function SetConsoleTitle Lib "kernel32" Alias _
"SetConsoleTitleA" (ByVal lpConsoleTitle As String) As LongPrivate Declare Function WriteConsole Lib "kernel32" Alias _
"WriteConsoleA" (ByVal hConsoleOutput As Long, _
ByVal lpBuffer As Any, ByVal nNumberOfCharsToWrite As Long, _
lpNumberOfCharsWritten As Long, lpReserved As Any) As Long''''C O N S T A N T S'''''''''''''''''''''''''''''''''''''
'I/O handlers for the console window. These are much like the
'hWnd handlers to form windows.Private Const STD_INPUT_HANDLE = -10&
Private Const STD_OUTPUT_HANDLE = -11&
Private Const STD_ERROR_HANDLE = -12&'Color values for SetConsoleTextAttribute.
Private Const FOREGROUND_BLUE = &H1
Private Const FOREGROUND_GREEN = &H2
Private Const FOREGROUND_RED = &H4
Private Const FOREGROUND_INTENSITY = &H8
Private Const BACKGROUND_BLUE = &H10
Private Const BACKGROUND_GREEN = &H20
Private Const BACKGROUND_RED = &H40
Private Const BACKGROUND_INTENSITY = &H80'For SetConsoleMode (input)
'Private Const ENABLE_LINE_INPUT = &H2
'Private Const ENABLE_ECHO_INPUT = &H4
'Private Const ENABLE_MOUSE_INPUT = &H10
'Private Const ENABLE_PROCESSED_INPUT = &H1
'Private Const ENABLE_WINDOW_INPUT = &H8
''For SetConsoleMode (output)
'Private Const ENABLE_PROCESSED_OUTPUT = &H1
'Private Const ENABLE_WRAP_AT_EOL_OUTPUT = &H2'''''G L O B A L S'''''''''''''''''''''''''''''''''''
Private hIn As Long 'The console's input handle
Private hOut As Long 'The console's output handle
Private hErr As Long 'The console's error handle'''''M A I N'''''''''''''''''''''''''''''''''''''''''
Private Sub Main()
Dim szUserInput As String
Dim Flag As Boolean
AllocConsole 'Create a console instance
SetConsoleTitle "VB Palindrome" 'Set the title on the console window
'Get the console's handle
hIn = GetStdHandle(STD_INPUT_HANDLE)
hOut = GetStdHandle(STD_OUTPUT_HANDLE)
hErr = GetStdHandle(STD_ERROR_HANDLE)
'Print the prompt to the user. Use the vbCrLf to get to a new line.
SetConsoleTextAttribute hOut, FOREGROUND_RED Or FOREGROUND_GREEN Or FOREGROUND_INTENSITY Or BACKGROUND_BLUE
ConsolePrint "Search For Palindrome (C) Dipak Auddy." & vbCrLf & vbCrLf
Do
SetConsoleTextAttribute hOut, FOREGROUND_RED Or FOREGROUND_GREEN Or FOREGROUND_BLUE
ConsolePrint vbCrLf & "Enter Word,Phrase or a Sentence[ENTER to Quit.]--> "
'Get the user's input
szUserInput = ConsoleRead()
If Not szUserInput = vbNullString Then
Flag = IsPalin(szUserInput)
If Flag Then
SetConsoleTextAttribute hOut, FOREGROUND_GREEN
ConsolePrint szUserInput & " -- IS PALINDROME." & vbCrLf
Else
SetConsoleTextAttribute hOut, FOREGROUND_RED
ConsolePrint szUserInput & " -- IS NOT PALINDROME." & vbCrLf
End If
Else
ConsolePrint "Good Bye!"
Exit Do
End If
Loop 'Call ConsoleRead
FreeConsole 'Destroy the console
End Sub
Private Sub ConsolePrint(szOut As String)
WriteConsole hOut, szOut, Len(szOut), vbNull, vbNull
End SubPrivate Function ConsoleRead() As String
Dim sUserInput As String * 256
Call ReadConsole(hIn, sUserInput, Len(sUserInput), vbNull, vbNull)
'Trim off the NULL charactors and the CRLF.
ConsoleRead = Left$(sUserInput, InStr(sUserInput, Chr$(0)) - 3)
End FunctionFunction IsPalin(ByVal TXT As String) As Boolean
Dim a As String 'Remove Space(s) from user input and make it Upper Case
TXT = UCase(Replace(TXT, " ", "")) a = StrReverse(TXT) If TXT = a Then
IsPalin = True
Else
IsPalin = False
End If
End Function
新建空工程,复制上面代码到模块中,并设置启动对象为Sub Main.