扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
以下内容为网络提供,但我自己验证可行,供你参考.
为寿阳等地区用户提供了全套网页设计制作服务,及寿阳网站建设行业解决方案。主营业务为做网站、成都网站建设、寿阳网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
Imports System.IO
Public Class Form1
Public Const WM_DEVICECHANGE = H219
Public Const DBT_DEVICEARRIVAL = H8000
Public Const DBT_CONFIGCHANGECANCELED = H19
Public Const DBT_CONFIGCHANGED = H18
Public Const DBT_CUSTOMEVENT = H8006
Public Const DBT_DEVICEQUERYREMOVE = H8001
Public Const DBT_DEVICEQUERYREMOVEFAILED = H8002
Public Const DBT_DEVICEREMOVECOMPLETE = H8004
Public Const DBT_DEVICEREMOVEPENDING = H8003
Public Const DBT_DEVICETYPESPECIFIC = H8005
Public Const DBT_DEVNODES_CHANGED = H7
Public Const DBT_QUERYCHANGECONFIG = H17
Public Const DBT_USERDEFINED = HFFFF
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_DEVICECHANGE Then
Select Case m.WParam
Case WM_DEVICECHANGE
Case DBT_DEVICEARRIVAL 'U盘插入
ComboBox1.Items.Clear()
Dim s() As DriveInfo = DriveInfo.GetDrives
For Each drive As DriveInfo In s
If drive.DriveType = DriveType.Removable Then
ListBox1.Items.Add("U盘已插入!盘符为:" + drive.Name.ToString())
ComboBox1.Items.Add(drive.Name)
End If
Next
BtnWrite.Enabled = True
BtnRead.Enabled = True
Case DBT_CONFIGCHANGECANCELED
Case DBT_CONFIGCHANGED
Case DBT_CUSTOMEVENT
Case DBT_DEVICEQUERYREMOVE
Case DBT_DEVICEQUERYREMOVEFAILED
Case DBT_DEVICEREMOVECOMPLETE 'U盘卸载
ListBox1.Items.Add("U盘卸载!")
BtnWrite.Enabled = False
BtnRead.Enabled = False
Case DBT_DEVICEREMOVEPENDING
Case DBT_DEVICETYPESPECIFIC
Case DBT_DEVNODES_CHANGED
Case DBT_QUERYCHANGECONFIG
Case DBT_USERDEFINED
End Select
End If
MyBase.WndProc(m)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Add("请您现在插入U盘至USB接口!")
End Sub
Private Sub BtnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnWrite.Click
If ComboBox1.Text = "" Then
MsgBox("请选择U盘盘符!", MsgBoxStyle.Exclamation, "Warn")
Else
Dim Writer As StreamWriter = Nothing
Try
Dim fileName As String = ComboBox1.Text + "Test.txt"
Writer = New StreamWriter(fileName)
Writer.WriteLine(InputBox("老四,请输入要保存的字符串", "输入信息", "Input then Test String! hehe!"))
MsgBox("Write to " + fileName + " Success!")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Write 失败")
Finally
If Writer IsNot Nothing Then Writer.Close()
End Try
End If
End Sub
Private Sub BtnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRead.Click
If ComboBox1.Text = "" Then
MsgBox("请选择U盘盘符!", MsgBoxStyle.Exclamation, "Warn")
Else
Dim Reader As StreamReader = Nothing
Try
Dim fileName As String = ComboBox1.Text + "Test.txt"
Reader = New StreamReader(fileName)
MsgBox("Read from " + fileName + vbCrLf + Reader.ReadToEnd, MsgBoxStyle.Information, "Info")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Read 失败")
Finally
If Reader IsNot Nothing Then Reader.Close()
End Try
End If
End Sub
End Class
这个,timer事件实际上都是时间达到设定的频率(Interval)时触发的,所以,要想达到程序启动立即触发的效果,建议把timer中事件写到一个函数(sub)中,然后在load和timer事件中调用该sub。
该实例有很多实现方法,下面介绍一种不同的给你:
首先要在窗体中载入以下控件:progressbar(进度条),label(用来实时显示文本提示),两个timer(分别控制进度条进度的增减),然后用以下代码就能实现:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Timer1.Interval = 500
Timer2.Interval = 500
ProgressBar1.Value = 0
ProgressBar1.Maximum = 100
ProgressBar1.Minimum = 0
End Sub
'上述过程是设置控件的初始值,也可以在属性面板设置
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
ProgressBar1.Value += 1
Label1.Text = "当前完成" ProgressBar1.Value "%"
If (ProgressBar1.Value = 100) Then
Timer1.Enabled = False
Timer2.Enabled = True
End If
End Sub
'timer1用来控制进度的增加
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
ProgressBar1.Value -= 1
Label1.Text = "当前完成" ProgressBar1.Value "%"
If (ProgressBar1.Value = 0) Then
Timer1.Enabled = True
Timer2.Enabled = False
End If
End Sub
'time2用来控制进度的减少
End Class
1.定义变量记录键是否被按下,按下的累计时间
Dim 按下A键 As Boolean
Dim 按下A键时间 As Integer
2.添加一个TextBox1用于接收键盘事件,或者直接用窗体的键盘事件(确保KeyPreview为True)
在KeyDown事件中添加如下代码:
If e.Keycode = keys.A Then 按下A键 = True
在KeyUp事件中添加如下代码:
If e.Keycode = keys.A Then 按下A键 = False
3.添加一个Timer1控件,调整Timer1的Interval来决定按键频率,记得要把Timer1.Enable开启
在Tick事件中添加如下代码:
If 按下A键 = True
按下A键时间+=1
Call 音量设置(按下A键时间 * 10)
End If
PS: 按下的秒数 = 按下A键时间*Timer1.Interval / 1000
控制台调用Timer和窗体是类似的。首先在项目引用里面加入System.Windows.Forms程序集,然后在代码顶部引入命名空间:
Imports System.Windows.Forms
在控制台的Module中声明一个计时器:
Private WithEvents Timer1 As New Timer()
把计时器的Tick事件静态绑定到处理函数中:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'一些代码
End Sub
在需要开始计时的地方,修改其Interval、Enabled属性:
Timer1.Interval = 1000
Timer1.Enabled = True
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流