vb.net抽奖程序 怎么用vb做抽奖软件
什么是多线程技术?在VB 中如何使用?
vb6可以实现多线程,不过比较麻烦,vb.net完全支持多线程,请参见msdn.
在兴县等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站制作、网站设计 网站设计制作按需网站制作,公司网站建设,企业网站建设,成都品牌网站建设,营销型网站建设,成都外贸网站制作,兴县网站建设费用合理。
至于vb6实现多线程请看CreateThread这个API函数.以下有详细方案:
用VB写多线程程序用到的第一个API函数是CreateThread,这个函数的声明如下:
Private Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadId As Long)As Long
这个函数的返回值是线程的句柄,它的参数含义如下:
1.lpThreadAttributes:这个参数表明函数的返回句柄是否可被子进程继承,如果可被继承,则指向一个SECURITY_ATTRIBUTES的结构,否则设为vbnull。
2.dwStackSize:这个参数设置线程的堆栈大小。
3.lpStartAddress:这个参数指明这个线程函数的起始地址。
4.lpParameter:这个参数是传给线程函数的参数。
5.dwCreationFlags:这个参数设置当线程创建时的初始状态,挂起,运行等等。
6.lpThreadId:这个参数是待创建线程的ID号。
假设创建一个管理线程的类clsThreads,用类的一个公用函数Initialize来初始化线程,用ResumeThread和SuspendThread函数来激活线程,实现代码如下:
Private Type udtThread
Handle As Long
Enabled As Boolean
End Type
Private uThread As udtThread
Private Const CREATE_SUSPENDED As Long = H4
Private Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long
Private Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long
Private Declare Function SuspendThread Lib "kernel32" (ByVal hThread As Long) As Long
Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
Public Sub Initialize(ByVal lpfnBasFunc As Long) '初始化线程
Dim lStackSize As Long, lCreationFlags As Long, lpThreadId As Long, lNull As Long
On Error Resume Next
lNull = 0 '创建一个空指针
lStackSize = 0 '0表示用exe的stack size
lCreationFlags = CREATE_SUSPENDED '表示初始化后先不激活,让别人来激活
uThread.Handle = CreateThread(lNull, lStackSize, lpfnBasFunc, lNull, lCreationFlags, lpThreadId)
If uThread.Handle = lNull Then MsgBox "Create thread failed!"
End Sub
Public Property Get Enabled() As Boolean
On Error Resume Next
Enabled = uThread.Enabled
End Property
Public Property Let Enabled(ByVal vNewValue As Boolean)
On Error Resume Next
If vNewValue And (Not uThread.Enabled) Then
ResumeThread uThread.Handle '激活线程
uThread.Enabled = True
ElseIf uThread.Enabled Then
SuspendThread uThread.Handle
uThread.Enabled = False
End If
End Property
Private Sub Class_Terminate() '终止线程
On Error Resume Next
Call TerminateThread(uThread.Handle, 0)
End Sub
以上内容在类模块clsThreads中,有了它就可以在窗体中实现多线程了。下面以一个简单的例子来说明如何创建多线程(以2个线程为例)。
在form1中添加picture1和picture2两个图片框,以及一个command1按钮,在工程中添加一个bas模块,该模块的代码如下:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Public Sub FlickerTop()
Static BgColor As Long
Dim lTick As Long, lCounter As Long
On Error Resume Next
For lCounter = 0 To 60000
BgColor = lCounter Mod 256
Form1.Picture1.BackColor = RGB(BgColor, 0, 0) '变化图片框的颜色
lTick = GetTickCount
While GetTickCount - lTick 10 '延迟10个毫秒时间
Wend
Next
End Sub
Public Sub FlickerBottom()
Static BgColor As Long
Dim lTick As Long, lCounter As Long
On Error Resume Next
For lCounter = 0 To 60000
BgColor = lCounter Mod 256
Form1.Picture2.BackColor = RGB(0, BgColor, 0)
lTick = GetTickCount
While GetTickCount - lTick 10
Wend
Next
End Sub
最后在form1中用下面的代码来创建两个线程,
Option Explicit
Public myThreadTop As New clsThreads, myThreadBottom As New clsThreads
Private Sub Command1_Click()
On Error Resume Next
With myThreadTop
.Initialize AddressOf FlickerTop
.Enabled = True
End With
With myThreadBottom
.Initialize AddressOf FlickerBottom
.Enabled = True
End With
MsgBox "看看会有什么..."
Set myThreadTop = Nothing
Set myThreadBottom = Nothing
End Sub
VBNET编写抽奖软件程序
把你的代码发出来。
你这种方式的程序只能弄一等奖吧,为什么会有二等奖三等奖?
获奖次数跟电话号码什么关系,为什么不能重复?同一个号码不能多次获奖?那就不是重复的问题,你的提问有些不清不楚。
暂定的解决方案——
把随机生成的电话号码,赋值给公共数组,如果公共数组中存在新生成的电话号码,那么重新生成电话号码,如果电话号码不在数组中存在,那么将电话号码加入到数组中。重复以上过程,直至喊停。
vb.net 多次滚动抽奖的程序代码
我用多线程写的,你可以参考下。
系统界面图如下:(注:重置功能为清除已经中奖号码,回归1-100都可以中奖状态。)
代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
System.Threading.Thread g_Thread = null;
Listint g_list = new Listint();
int g_Num = 0;
delegate void g_delegate();
/// summary
/// 开始
/// /summary
/// param name="sender"/param
/// param name="e"/param
private void button2_Click(object sender, EventArgs e)
{
if(g_list.Count==100)
{
MessageBox.Show("所有数字已使用,请重置!");
return;
}
button3.Enabled = true;
Random r = new Random();
g_delegate dele= ChangeTxt;
g_Thread = new System.Threading.Thread(() =
{
while(this.IsDisposed==false)
{
g_Num = r.Next(1, 100);
if(g_list.Contains(g_Num))
{
g_Num = 1;
}
while(g_list.Contains(g_Num))
{
g_Num++;
}
this.Invoke(dele);
System.Threading.Thread.Sleep(50);
}
});
g_Thread.Start();
}
void ChangeTxt()
{
label2.Text = g_Num.ToString();
}
/// summary
/// 停止
/// /summary
/// param name="sender"/param
/// param name="e"/param
private void button3_Click(object sender, EventArgs e)
{
//if(g_Thread.ThreadState==System.Threading.ThreadState.Running)
{
g_Thread.Abort();
g_list.Add(Convert.ToInt32(label2.Text));
}
button3.Enabled = false;
}
/// summary
/// 重置
/// /summary
/// param name="sender"/param
/// param name="e"/param
private void button1_Click(object sender, EventArgs e)
{
g_list.Clear();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
System.Environment.Exit(0);
}
}
请教vb源代码,抽奖程序,从100个人中抽取三等奖3个,二等奖2个,一等奖1个,每次抽奖不重复。
Public Sub Main()
Dim allLeft As New List(Of Integer)()
For i As Integer = 0 To 99
allLeft.Add(i + 1)
Next
Dim r As New Random()
Dim [next] As Integer
Dim level As String
For i As Integer = 0 To 5
[next] = r.[Next]() Mod allLeft.Count
If i 3 Then
level = "Third"
ElseIf i 5 Then
level = "Second"
Else
level = "First"
End If
Console.WriteLine(level " level prize is: " allLeft([next]).ToString())
allLeft.RemoveAt([next])
Next
End Sub
当前题目:vb.net抽奖程序 怎么用vb做抽奖软件
标题路径:http://scjbc.cn/article/dosepsd.html