用VB 6.0編寫電腦抽獎(jiǎng)程序
發(fā)表時(shí)間:2023-07-14 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]近年來在娛樂節(jié)目之中常常見到利用電腦來抽獎(jiǎng),筆者對(duì)其發(fā)生了興趣遂自己動(dòng)手用VB編了一個(gè)小程序來實(shí)現(xiàn)電腦抽獎(jiǎng)的小功能,其原理如下: 主要利用VB中的Rnd函數(shù),來實(shí)現(xiàn)隨機(jī)查找和打亂排序的功能,從而實(shí)...
近年來在娛樂節(jié)目之中常常見到利用電腦來抽獎(jiǎng),筆者對(duì)其發(fā)生了興趣遂自己動(dòng)手用VB編了一個(gè)小程序來實(shí)現(xiàn)電腦抽獎(jiǎng)的小功能,其原理如下:
主要利用VB中的Rnd函數(shù),來實(shí)現(xiàn)隨機(jī)查找和打亂排序的功能,從而實(shí)現(xiàn)隨機(jī)抽獎(jiǎng)的目的。Rnd函數(shù)的語法結(jié)構(gòu)是Rnd[(number)],可選的number參數(shù)是 single或任何有效的數(shù)值表達(dá)式。Rnd函數(shù)返回小于1但大于或等于0的值。number 的值決定了 Rnd 生成隨機(jī)數(shù)的方式。為了生成某個(gè)范圍內(nèi)的隨機(jī)整數(shù),可使用以下公式:
Int((upperbound - lowerbound + 1) × Rnd + lowerbound)
這里,upperbound 是隨機(jī)數(shù)范圍的上限,而 lowerbound 則是隨機(jī)數(shù)范圍的下限。
另外,程序中還使用了INI文件,Windows INI文件,可解釋為Windows初始化文件。它是一種專門用來保存應(yīng)用程序初始化信息和運(yùn)行環(huán)境信息的文本文件。ini文件是一種文本文件,它可以通過Notepad等文本編輯器進(jìn)行編輯。ini文件具有特定的格式。一個(gè)INI文件是由若干個(gè)段(section)組成的,每個(gè)段中包含若干關(guān)鍵字(key)及相應(yīng)的值(value)。創(chuàng)建應(yīng)用程序自己的INI文件,通過INI文件保存應(yīng)用程序的一些運(yùn)行環(huán)境信息,然后在程序中讀取INI文件中的設(shè)置信息并據(jù)以處理。一旦程序的運(yùn)行環(huán)境需要變更,則可以通過直接修改INI文件,或在程序中提供專門的界面間接地修改INI文件來保證程序的可用性。
源程序及注釋如下:
'窗體源程序
Option Explicit
Dim m_strNameArray() As MyName
Dim m_bIsStart As Boolean
Dim m_nNameIndex As Integer
Dim MAX_INDEX As Integer
Dim m_nSelectNum As Integer
'被選定數(shù)
Dim nScrollStep As Integer
Dim nScrollWidth As Integer
Dim bScrollState As Boolean
Dim nEnableSecond As Integer
Dim m_strTitle As String
Dim m_strAppTitle As String
Dim m_strScrollTitleLeft As String
Dim m_strScrollTitleRight As String
Private Sub Command_Start_Stop_Click()
If m_bIsStart = True Then
'按停止鈕
m_bIsStart = False
Command_Start_Stop.Caption =
“開始"
Label_FlashName.Visible = True
Timer_FlashName.Enabled = True
Timer_ScrollName.Enabled = False
Label_FlashName =
m_strNameArray(m_nNameIndex).strName + “中獎(jiǎng)了!"
m_strNameArray(m_nNameIndex).bIsSelect = True
m_nSelectNum = m_nSelectNum + 1
Dim Temp As MyName
Temp =m_strNameArray(MAX_INDEX)
m_str Name Array(MAX-INDEX) = m_strNameArray(m_nNameIndex)
m_strNameArray(m_nNameIndex) =Temp
MAX_INDEX = MAX_INDEX - 1
If MAX_INDEX = 0 Then
MsgBox “非常感謝您使用本軟件"
End If
Else '按開始鈕
m_bIsStart = True
Command_Start_Stop.Caption = “停止"
Command_Start_Stop.Enabled = False
Timer_ScrollName.Enabled = True
Timer_FlashName.Enabled = False
Label_FlashName.Caption = “"
End If
End Sub
Private Sub Form_Load()
Form_Bouns.ScaleMode = 3
m_nNameIndex = 0
m_bIsStart = False
Timer_ScrollName.Enabled = True
Timer_ScrollTitle.Enabled = True
Label_FlashName.Visible = False
Label_ScrollName.Caption = “"
nEnableSecond = 0
'定義起始秒數(shù)
ReDimNameArray
'獲得文本中的名字和打亂名字順序
nScrollStep = 5 '設(shè)定滾動(dòng)字的步長(zhǎng)
nScrollWidth = Label_Congruation.Left
'設(shè)定title的移動(dòng)寬度
bScrollState = False
'設(shè)定缺省的開始滾動(dòng)方向?yàn)橄蜃?br> m_nSelectNum = 0
'初始化被選定數(shù)為0
Init
'初始化本程序的界面
End Sub
Private Sub Timer_FlashName_Timer() '閃動(dòng)中獎(jiǎng)?wù)咝彰?br> If Label_FlashName.Visible = True Then
Label_FlashName.Visible = False
Else
Label_FlashName.Visible = True
End If
End Sub
Private Sub Timer_ScrollName_Timer() '滾動(dòng)出現(xiàn)名字
If m_bIsStart = True Then
If m_nNameIndex >= MAX_INDEX Then
m_nNameIndex = 0
End If
m_nNameIndex =m_nNameIndex + 1
If m_strNameArray(m_nNameIndex).bIsSelect = True Then
If m_nNameIndex < MAX-INDEX Then
m_nNameIndex =
m_nNameIndex + 1
Else
m_nNameIndex = 0
End If
End If
Label_ScrollName.Caption = m_str
NameArray(m_nNameIndex).strName
'End If
End If
End Sub
Private Sub Timer_ScrollTitle_Timer() '滾動(dòng)“恭喜發(fā)財(cái)"字樣
If bScrollState = False Then '向左滾
nScrollStep = 10
Label_Congruation.Caption = m_strScrollTitleLeft
If nScrollWidth > 0 Then
nScrollWidth =
nScrollWidth - nScrollStep
Else
bScrollState = True
End If
Else '向右滾
nScrollStep = -10
Label_Congruation.Caption =
m_strScrollTitleRight
If nScrollWidth < Form_Bouns.ScaleWidth -
Label_Congruation.Width Then
nScrollWidth =
nScrollWidth - nScrollStep
Else
bScrollState = False
End If
End If
Label_Congruation.Left = nScrollWidth
'以下為8秒鐘內(nèi)使“停止"按鈕有效
If nEnableSecond <= 49 Then
If m_bIsStart = True Then
nEnableSecond =nEnableSecond + 1
End If
Else
If m_bIsStart = True Then
Command_Start_Stop.Enabled = True
nEnableSecond = 0
End If
End If
End Sub
'動(dòng)態(tài)定義數(shù)組
Private Sub ReDimNameArray()
Dim nMaxIndex As Integer
Dim strMaxIndex As String
Dim nIndex As Integer
Dim bIsBegin As Boolean
bIsBegin = False
nIndex = 0
Open App.Path + “\name.txt" For Input As #1 '讀文件
Do Until EOF(1)
If bIsBegin = False Then
Line Input #1, strMaxIndex
nMaxIndex = Val(strMaxIndex)
MAX_INDEX = nMaxIndex - 1
ReDim m_strNameArray(0 To nMaxIndex - 1)
bIsBegin = True
Else
Line Input #1, m_strNameArray(nIndex).strName
m_strNameArray(nIndex).bIsSelect = False
nIndex = nIndex + 1
End If
Loop
'以下為打亂人員順序10次
Dim i As Integer
Dim j As Integer
Dim Temp As String
Dim nRandomNum As Integer
For j = 0 To 10
For i = 0 To nMaxIndex - 1
nRandomNum = ((nMaxIndex - 1) × Rnd) '利用Rnd函數(shù)
Temp = m_strNameArray(i).strName
m_strNameArray(i).strName = m_strNameArray(nRandomNum).strName
m_strNameArray(nRandomNum).strName = Temp
Next i
Next j
End Sub
Private Sub Init() '讀取INI文件
Dim X As Long
Dim lpFileName
Dim Temp As String × 50
lpFileName = App.Path + “\Sortition.ini"
X = GetPrivateProfileString(“SYSTEM",“AppTitle",“抽獎(jiǎng)程序", Temp, Len(Temp), lpFileName)
m_strAppTitle = Trim(Temp)
Temp =“"
X = GetPrivateProfileString(“SYSTEM", "Title", "歡迎使用抽獎(jiǎng)程序", Temp, Len(Temp), lpFileName)
m_strTitle = Trim(Temp)
Temp = “"
X = GetPrivateProfileString(“SYSTEM",“ScrollTitleRight", “恭喜發(fā)財(cái)!!!", Temp, Len(Temp), lpFileName)
m_strScrollTitleRight = Trim(Temp)
X = GetPrivateProfileString(“SYSTEM",“ScrollTitleLeft", “龍年大發(fā)!!!", Temp, Len(Temp), lpFileName)
m_strScrollTitleLeft = Trim(Temp)
Form_Bouns.Caption = m_strAppTitle
Label_CompanyTitle.Caption = m_strTitle
End Sub
模塊源程序:
'用于讀取ini文件的API函數(shù)
Declare Function GetPrivateProfileString Lib “kernel32" Alias “GetPrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Type MyName
strName As String
bIsSelect As Boolean
End Type
由于程序利用的windows ini文件保存一些標(biāo)題信息,因而可以方便的修改使用環(huán)境,及標(biāo)題內(nèi)容。
見ini文件內(nèi)容:
[SYSTEM]
;應(yīng)用程序的form名稱
AppTitle=“風(fēng)云電腦抽獎(jiǎng)Test"
;窗口的內(nèi)的標(biāo)題(限9個(gè)字)
Title=“大抽獎(jiǎng)"
;右滾動(dòng)的文字(僅能為如下格式:XXXX!!!)
ScrollTitleRight=“恭喜發(fā)財(cái)!!!"
;左滾動(dòng)的文字(僅能為如下格式:XXXX!!!)
ScrollTitleLeft=“祝您好運(yùn)!!!"
如此一個(gè)小小的電腦抽獎(jiǎng)程序便完成了。
以上程序在VB6.0 Windows98環(huán)境下編譯通過