Use VB to write console-based programs

Source: Internet
Author: User
Document directory
  • ① Allocconsole
  • ② Setconsoletitle
  • ③ Getstdhandle
  • ④ Setconsoletextattribute
  • ⑤ Freeconsole
  • ⑥ Writeconsole
  • 7. readconsole
Design Concept

(1) Allocate the memory resources of the console program.

(2) set relevant properties of the console.

(3) output text content on the console.

(4) receive interaction information and continue output.

(5) release the memory of the console program and close the program.

Steps

(1) Start VB, create a project, delete the default "from1" form, add a module, and enter the definition code of constants and global variables in the module.

Note: For API function declaration, refer to "Current API function declaration ":

'Define handle constants for standard input, output, and errors

Private const std_input_handle =-10 &

Private const std_output_handle =-11 &

Private const std_error_handle =-12 &

'Define the console program text attribute constant

Private const foreground_blue = & H1

Private const foreground_green = & H2

Private const foreground_red = & h4

Private const foreground_inten

Sity = & H8

Private const background_blue = & H10

Private const background_green = & H20

Private const background_red = & h40

Private const background_inten

Sity = & h80

'Define the console input mode constant

Private const enable_line_input = & H2

Private const enable_echo_input = & h4

Private const enable_mouse_input = & H10

Private const enable_processed_ I

Nput = & H1

Private const enable_window_indium

Ut = & H8

'Define the console output mode constant

Private const enable_processed_output = & H1

Private const enable_wrap_at_eol_output = & H2

'Define the console input handle variable

Private hconsolein as long

'Define the console output handle variable

Private hconsoleout as long

'Define the console error handle variable

Private hconsoleerr as long

(2) enter the code of the main program and other function subroutines and functions in the module:

'Main Program

Private sub main ()

'Define the accept variable for receiving input

Dim szusername as string

'Allocate the memory of the console program to complete the design (1). Refer to API function declaration ①.

Allocconsole

'Set the Form title. Refer to API function declaration ②.

Setconsoletitle "VB Console window"

'The get a series of handles of the control window, refer to API function declaration ③, and here to complete the design idea (2)

Hconsolein = getstdhandle (std_input_handle)

Hconsoleout = getstdhandle (std_output_handle)

Hconsoleerr = getstdhandle (std_error_handle)

'Set the text attributes output by the console. Refer to API function declaration ④

Setconsoletextattribute hconsoleout, foreground_red or foreground_green or foreground_blue or foreground_intensity or background_green

'Output text and design ideas (3)

Printconsole "VB console Demo" & vbcrlf

Setconsoletextattribute hconsoleout,

Eground_red or foreground_green or foreground_blue

'Output text prompting the user

Printconsole "Please enter User name :"

'Read the input from the console, that is, get the user name

Szusername = consoleread ()

'Output different content based on input and design ideas (4)

If not szusername = vbnullstring then

'If the user name entered is correct (assuming "user ")

If szusername = "user" then

'Output the correct input prompt

Printconsole "Welcome," & szusername &"! "& Vbcrlf

Else

'Warning of incorrect user name input

Printconsole "error! "& Vbcrlf

End if

Else

'The user is prompted to enter the user name

Printconsole "press enter user name! "& Vbcrlf

End if

'The user is prompted to press any key to close the form

Printconsole "press enter to close the demo"

Call consoleread

'Release the memory and close the console program. Refer to API function declaration ⑤ For design ideas (5)

Freeconsole

End sub

'Subprograms that output content to the console

Private sub printconsole (szout as string)

Writeconsole hconsoleout, szout, Len (szout), vbnull, vbnull' refer to API function declaration 6

End sub

'Function for reading input content from the console

Private function consoleread () as string

Dim szuserinput as string * 256

Call readconsole (hconsolein, szuserinput, Len (szuserinput), vbnull, vbnull) 'refer to API function declaration 7

'Remove empty characters and carriage returns

Consoleread = left $ (szuserinput, instr (szuserinput, CHR $ (0)-3)

End Function

Note: This example has been tested in Windows2000 and VB6 environments.

Related API function declaration ① allocconsole

Note: allocate the memory required for the console window.

Return Value: long type.

VB Declaration: Private declare function allocconsole lib "Kernel32" () as long

② Setconsoletitle

Note: Set the title of the console form.

Return Value: long type.

Parameters:

Lpconsoletitle: string type, indicating the title content of the console form to be set.

VB Declaration: Private declare function setconsoletitle lib "Kernel32" alias "setconsoletitlea" (byval lpconsoletitle as string) as long

③ Getstdhandle

Obtain the handle of the console program.

Return Value: long type.

Parameters:

Nstdhandle: long type, which indicates the window for obtaining the handle.

VB Declaration: Private declare function getstdhandle lib "Kernel32" (byval nstdhandle as long) as long

④ Setconsoletextattribute

Description: sets the console text attributes.

Return Value: long type.

Parameters:

Hconsoleoutput: long type, indicating the console window handle of the content to be output.

Wattributes: long type, indicating the output text attribute.

VB Declaration: Private declare function setconsoletextattribute lib "Kernel32" (byval hconsoleoutput as long, byval wattributes as long) as long

⑤ Freeconsole

Note: Release the memory occupied by the console window.

Return Value: long type.

VB Declaration: Private declare function freeconsole lib "Kernel32" () as long

⑥ Writeconsole

Description: Output content to the console.

Return Value: long type.

Parameters:

Hconsoleoutput: long type, indicating the console window handle of the content to be output.

Lpbuffer: any type, indicating the buffer area for storing output content.

Nnumberofcharstowrite: long type, indicating the length of the output content.

Lpnumberofcharswritten: long type, indicating the actual length of the output content.

Lpreserved: any type. This parameter is retained and is not used for the moment.

VB Declaration: Private declare function writeconsole lib "Kernel32" alias "Write

Consolea "(byval hconsoleoutput as long, byval lpbuffer as any, byval nnumberofcharstowrite as long, lpnumberofcharswritten as long, lpreserved as any) as long

7. readconsole

Description: reads the content entered in the console.

Return Value: long type.

Parameters:

Hconsoleinput: long type, indicating the console window handle for receiving input content.

Lpbuffer: string type, indicating the buffer area for receiving input content.

Nnumberofcharstoread: long type, indicating the length of the received input content.

Lpnumberofcharsread: long type, indicating the length of the input content actually received.

Lpreserved: any type. This parameter is retained and is not used for the moment.

VB Declaration: Private declare function readconsole lib "Kernel32" alias "readconsolea" (byval hconsoleinput as long, byval lpbuffer as string, byval encoded as long, lpnumberofcharsread as long, lpreserved as any) as long

Collected by barenx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.