如何建立控制台窗口以及同窗口交互

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 technofantasy 的 blog

如何建立控制台窗口
以及让用户在其中输入字符同计算机进行交互对话。
    首先在选vb菜单中的 project | module 项向工程文件中加入一个模块,然后在
这个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    '建立一个控制台窗口
    setconsoletitle "vb console example" '设置窗口标题

    '获得控制窗口的句柄
    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项,将startup object改变为sub main,然后
运行程序,程序就会弹出一个控制台窗口,用户可以根据控制台窗口中的提示信息与程序进行交互
对话。
   

www.applevb.com

本文关键:如何建立控制台窗口以及同窗口交互
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top