VB調(diào)用API函數(shù)創(chuàng)建控制臺(tái)窗口
發(fā)表時(shí)間:2023-08-04 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]隨著軟件的界面設(shè)計(jì)的發(fā)展,人機(jī)交互的方式同過(guò)去也有了很大的不同,圖形用戶 界面,鼠標(biāo)操作甚至語(yǔ)音等早已經(jīng)率見(jiàn)不先了。但是在有一些程序中,還是要使用到 象過(guò)去那種老式的主機(jī)——終端那樣的字符型控制臺(tái)窗...
隨著軟件的界面設(shè)計(jì)的發(fā)展,人機(jī)交互的方式同過(guò)去也有了很大的不同,圖形用戶
界面,鼠標(biāo)操作甚至語(yǔ)音等早已經(jīng)率見(jiàn)不先了。但是在有一些程序中,還是要使用到
象過(guò)去那種老式的主機(jī)——終端那樣的字符型控制臺(tái)窗口式樣的界面。而實(shí)際上,在
Windows中也保留了這樣的一系列控制臺(tái)函數(shù),下面的范例演示了如何建立控制臺(tái)窗口以及讓用戶在其中輸入字符同計(jì)算機(jī)進(jìn)行交互對(duì)話。
首先在選VB菜單中的 Project Module 項(xiàng)向工程文件中加入一個(gè)模塊,然后在
這個(gè)Module中加入以下代碼:
Option Explicit
Private Declare Function AllocConsole Lib "kernel32" () As Long
Private Declare Function FreeConsole Lib "kernel32" () As Long
Private Declare Function GetStdHandle Lib "kernel32" _
(ByVal nStdHandle As Long) As Long
Private 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 Long
Private Declare Function SetConsoleTextAttribute Lib _
"kernel32" (ByVal hConsoleOutput As Long, ByVal _
wAttributes As Long) As Long
Private Declare Function SetConsoleTitle Lib "kernel32" Alias _
"SetConsoleTitleA" (ByVal lpConsoleTitle As String) As Long
Private 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
Private Const STD_INPUT_HANDLE = -10&
Private Const STD_OUTPUT_HANDLE = -11&
Private Const STD_ERROR_HANDLE = -12&
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 hConsoleIn As Long ' The console's input handle
Private hConsoleOut As Long ' The console's output handle
Private hConsoleErr As Long ' The console's error handle
'''''M A I N'''''''''''''''''''''''''''''''''''''''''
Private Sub Main()
Dim szUserInput As String
AllocConsole '建立一個(gè)控制臺(tái)窗口
SetConsoleTitle "VB Console Example" '設(shè)置窗口標(biāo)題
'獲得控制窗口的句柄
hConsoleIn = GetStdHandle(STD_INPUT_HANDLE)
hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE)
hConsoleErr = GetStdHandle(STD_ERROR_HANDLE)
SetConsoleTextAttribute hConsoleOut, _
FOREGROUND_RED Or FOREGROUND_GREEN _
Or FOREGROUND_BLUE Or FOREGROUND_INTENSITY _
Or BACKGROUND_BLUE
ConsolePrint "VB Console Example" & vbCrLf
SetConsoleTextAttribute hConsoleOut, _
FOREGROUND_RED Or FOREGROUND_GREEN _
Or FOREGROUND_BLUE
ConsolePrint "Please Enter Your Name Here--> "
'獲得用戶名
szUserInput = ConsoleRead()
If Not szUserInput = vbNullString Then
ConsolePrint "Hello, " & szUserInput & "!" & vbCrLf
Else
ConsolePrint "Hello,But who are you?" & vbCrLf
End If
ConsolePrint "Press Enter To Close The Console"
Call ConsoleRead
FreeConsole ' Destroy the console
End Sub
Private Sub ConsolePrint(szOut As String)
WriteConsole hConsoleOut, szOut, Len(szOut), vbNull, vbNull
End Sub
Private Function ConsoleRead() As String
Dim sUserInput As String * 256
Call ReadConsole(hConsoleIn, sUserInput, Len(sUserInput), vbNull, vbNull)
'Trim off the NULL charactors and the CRLF.
ConsoleRead = Left$(sUserInput, InStr(sUserInput, Chr$(0)) - 3)
End Function
選VB菜單中的 Project Project1 Properties項(xiàng),將Startup Object改變?yōu)镾ub Main,然后
運(yùn)行程序,程序就會(huì)彈出一個(gè)控制臺(tái)窗口,用戶可以根據(jù)控制臺(tái)窗口中的提示信息與程序進(jìn)行交互
對(duì)話。
上面的程序在Win98、VB6下運(yùn)行通過(guò)。