VB writing a lottery program

xiaoxiao2021-03-06  19

The college is engaged in activities, there is a circle to draw a lottery, ask me to write a computer lottery program, just like the TV's variety interface, press the space to stop. There is no careful thinking at the beginning, thinking that there is nothing difficult to advice, when you start writing, it will be found that it is necessary to judge the in-array cycle through conditions to the outside. The first thing that thought in the mind is to use multithreading, but everyone knows that if you want to use multithreaded difficulties with VB. If you only use a normal single-thread, you must find a method to capture the response of the keyboard and interrupt the loop. So I thought of capturing the news, the specific idea is to try the message that Keydwon can be captured in the process of cycling. There are two APIs that can be used as a getMessage, one is PeekMessage, because we just need to monitor the keyboard space button KeyDown event, do not need to intercept messages, so choose PeekMessage here.

The key problem is solved, the other is good, the number of the 号 is recorded in advance, and the program is read in the array in the form LOAD event. When reading, the order of chaos, here Simplely read with a previous method. In order to ensure that the number of shaking will not be shaken, it is necessary to record the INDEX that is shaken in the array in order to skip when it is cycled. The general idea is this look. The specific code is as follows:

Option expedition

DIM DATA () AS String, Del () AS Integer, INDEX AS INTEGER

'API function declaration

Private const pm_remove = & h1

Private Declare Function PeekMessage Lib "User32" Alias ​​"PeekMessagea" (LPMSG AS MSG, BYVAL HWND As Long, BYVAL WMSGFILTERMAX As Long, Byval Wremovemsg As long) As long

Private Type PointApi

X as long

Y as long

End Type

Private Type MSG

HWND As Long

Message as long

WPARAM As Long

LPARAM As Long

Time as long

PT As PointApi

End Type

Private Sub Form_KeyPress (Keyascii AS Integer)

DIM I AS INTEGER, TEST AS Long, J AS Integer

DIM AMSG AS MSG

'On Error Resume Next

'Press Enter to start scrolling

IF keyascii = 13 THEN

INDEX = INDEX 1

Redim Preserve Del (INDEX)

IF Ubound (DEL) - 1> = Ubound (data) THEN

Msgbox "No data can be shaken"

EXIT SUB

END IF

DO

PeekMessage Amsg, Me.hwnd, 0, 0, PM_Remove

'Stop scrolling by the space bar

IF amsg.wparam = 32 THEN

Exit do

END IF

Lab:

i = i 1

IF i> Ubound (data) THEN

i = 1

END IF

For j = 0 to Ubound (DEL)

IF i = del (j) THEN

Goto lab

END IF

Next jwith lblno

.Caption = DATA (i)

.Refresh

End with

Loop

Print Space (3) & Data (i);

Del (INDEX) = i

IF INDEX MOD 8 = 0 THEN PRINT

LBLPRICE.CAPTION = "Congratulations" & Data (I) & "Award"

LBLPRICE.LEFT = (Screen.Width - LBLPRICE.WIDTH) / 2

END IF

End Sub

Private sub flow_load ()

LBLName.Left = (Screen.width - lblname.width) / 2

LBLNAME2.LEFT = (Screen.width - lblname2.width) / 2

LBLNO.LEFT = (Screen.Width - LBLNO.WIDTH) / 2

Call readdata

LBLNO.CAPTION = DATA (1)

LBLPRICE.CAPTION = "" "

DIM I as integer

End Sub

'Read data

Private sub readdata ()

DIM FILENUMBER AS INTEGER, I as integer, temp () AS String, J AS Integer

FILENUMBER = FreeFile

Open app.path & "/data.txt" for Input As FileNumber

'Read the data in the text

Do While Not Eof (Filenumber)

i = i 1

Redim Preserve Temp (i)

Input #filenumber, Temp (i)

Loop

'Discharge the data in the reading according to the method of 1, 10, 2, 9, 3, 8, 4, 7, 5, 6

Redim Data (Ubound (Temp))

'Write an odd digital data

For i = 1 to Ubound (data) STEP 2

IF I> Ubound (data) Then Exit for

J = J 1

Data (i) = TEMP (j)

Next I

'Write an even number of data

J = Ubound (TEMP)

For i = 2 to Ubound (data) Step 2

IF I> Ubound (data) Then Exit for

Data (i) = TEMP (j)

J = J - 1

Next I

End Sub

转载请注明原文地址:https://www.9cbs.com/read-42021.html

New Post(0)