學院搞活動,有個環節是**,要求我幫他們寫乙個電腦搖獎的程式,就像電視的綜藝介面一樣按回車開始,按空格停下來。開始的時候沒有仔細的想,以為沒有什麼難度就欣然的答應了,開始寫的時候才發現,需要在外部通過條件判斷強制的中斷無限的迴圈。腦子裡第乙個想到的就是用多執行緒,可是大家都知道,如果用
vb想使用多執行緒難度是非常大的。如果只用普通的單執行緒就必須找到乙個方法捕捉到鍵盤的響應並中斷迴圈。於是想到了去捕獲訊息,具體的想法就是想試一下在迴圈的過程中看看可不可以捕捉到鍵盤
keydwon
的訊息。按照需求就有兩個
api可以使用乙個是
getmessage
,乙個是
peekmessage
,由於我們只是需要監視是否發生了鍵盤空格鍵
keydown
事件,並不需要攔截訊息,所以這裡選擇
peekmessage
更為適合。
關鍵的問題解決了,其他的都好辦了,備搖的號碼用記事本事先記錄,程式中在窗體
load
事件裡讀取到陣列中,讀取的時候因該打亂號碼的順序,這裡簡單的採用一前一後的方法讀取。為了保證搖到的號碼不會再搖到,所以要記錄被搖到的號碼在陣列中的
index
以便在迴圈的時候跳過。大體的思路就是這個樣子的。具體的**如下:
option explicit
dim data() as string, del() as integer, index as integer
『api
函式宣告
private const pm_remove = &h1
private declare function peekmessage lib "user32" alias "peekmessagea" (lpmsg as msg, byval hwnd as long, byval wmsgfiltermin 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
'按回車開始滾動
if keyascii = 13 then
index = index + 1
redim preserve del(index)
if ubound(del) - 1 >= ubound(data) then
msgbox "
沒有資料可以搖了
"exit sub
end if
dopeekmessage amsg, me.hwnd, 0, 0, pm_remove
'按空格鍵停止滾動
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 j
with 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 = "
恭喜" & data(i) & "獲獎"
lblprice.left = (screen.width - lblprice.width) / 2
end if
end sub
private sub form_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
'讀取資料
private sub readdata()
dim filenumber as integer, i as integer, temp() as string, j as integer
filenumber = freefile
'讀取文字中的資料
do while not eof(filenumber)
i = i + 1
redim preserve temp(i)
input #filenumber, temp(i)
loop
'將讀取中的資料打亂按照
1,10,2,9,3,8,4,7,5,6
的方法排列
redim data(ubound(temp))
'寫入奇數字的資料
for i = 1 to ubound(data) step 2
if i > ubound(data) then exit for
j = j + 1
data(i) = temp(j)
next i
'寫入偶數字的資料
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
使用VB編寫純ASP程式
前幾天大哥對我說,使用asp真麻煩,編譯環境實在是差勁,他總是在vb裡面寫好除錯好 然後在把 貼上到asp程式中,為此,我在這提出乙個解決之道。使用vb接替asp所有的物件,在vb中編寫純粹的asp程式。下面是詳細的步驟 1。在vb中新建乙個activex dll.其中那些命名專案 類的過程我就省略...
使用VB編寫純ASP程式
使用vb編寫純asp程式 前幾天大哥對我說,使用asp真麻煩,編譯環境實在是差勁,他總是在vb裡面寫好除錯好 然後在把 貼上到asp 程式中,為此,我在這提出乙個解決之道。使用vb接替asp所有的物件,在vb中編寫純粹的asp程式。下面是詳細的步驟 1。在vb中新建乙個activex dll.其中那...
中引用vb編寫的
private declare sub make lib makebar.dll ucdata as byte,byval nlen as long,byval szfilename as string,byval nclumn as long,byval nerr as long,byval nh...