重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
见下面代码,放置定时器,按钮,标签各一个
创新互联是一家网站设计制作、成都网站建设,提供网页设计,网站设计,网站制作,建网站,按需设计网站,网站开发公司,从2013年成立是互联行业建设者,服务者。以提升客户品牌价值为核心业务,全程参与项目的网站策划设计制作,前端开发,后台程序制作以及后期项目运营并提出专业建议和思路。
Public Class Form1
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick '定时器
If Label1.Right Me.Width Then
Label1.Left += 10'移动距离
Else
Label1.Left = 0
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click '开启或者关闭定时器
Timer1.Interval = 1000 '1秒
If Timer1.Enabled = False Then
Timer1.Start() '开定时器
Else
Timer1.Stop() '关定时器
End If
End Sub
End Class
Dim i As Integer
For i = 0 To Me.Width - Button1.Width 'me为容器,典型的是form1
Button1.Left += 1
Windows.Forms.Application.DoEvents()
Next
原理:不断地变化button1的X轴,使其向右移,同样,只要控制好其x,y轴,就可以变化button的变化
如果是做小游戏,建议弄成类吧,把其中的左(右)移或对角其它移动弄成其接口,以便操作。
由于涉及内容太多,不再详述。原理都一样
窗体是指由两个列表框(ListBox1、ListBox2)和4个命令按钮(Button1“”按钮,Button2“”按钮,Button3“”按钮,Button4“”按钮)所构成的界面,代码:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.Text = "选项移动"
ListBox1.SelectionMode = SelectionMode.MultiSimple
ListBox2.SelectionMode = SelectionMode.One
For i = 1 To 10
ListBox1.Items.Add(Chr(Asc("a") + i - 1))
Next
For i = 1 To 10
ListBox2.Items.Add(i.ToString)
Next
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If ListBox1.SelectedItems Is Nothing Then Exit Sub
Dim b As ListBox.ObjectCollection
For i = 0 To Me.ListBox1.SelectedItems.Count - 1
Me.ListBox2.Items.Add(Me.ListBox1.SelectedItems(0))
Me.ListBox1.Items.RemoveAt(Me.ListBox1.SelectedIndices(0))
Next
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
If ListBox2.SelectedItems IsNot Nothing Then
ListBox1.Items.Add(ListBox2.SelectedItem)
ListBox2.Items.Remove(ListBox2.SelectedItem)
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
For Each itm As Object In ListBox1.Items
ListBox2.Items.Add(itm)
Next
ListBox1.Items.Clear()
End Sub
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
For Each itm As Object In ListBox2.Items
ListBox1.Items.Add(itm)
Next
ListBox2.Items.Clear()
End Sub
End Class
1左移10位,1后面加10个0
结果就是:10000000000
五、按位运算符
注意不能在VB.Net中直接使用以下二进制数,使用时需转换为相应的十六或十进制数字。
1. 表示按位左移,空缺处补0。如:101101 2 结果为 110100
2. 表示按位右移,如:101101 2 结果为 001011
VB程序中实现字节移位操作
在利用VB系统开发数据采集或工业控制软件,或对文件进行低级操作时,常需要对字节进行移位操作,但VB系统中没有提供字节移位操作的指令和函数,只提供了And(与)、Or(或)、Xor(异或)、Equ(同或)、Not(非)等几个逻辑运算指令。笔者在用VB系统开发工业控制软件的过程中,就遇到了这个问题,于是利用VB中已有的逻辑运算指令,模拟汇编语言的字节移位指令,编制了七个字节移位函数:逻辑左移、逻辑右移、算术右移、循环左移、循环右移、进位循环左移和进位循环右移。
在汇编语言指令中,逻辑左移的功能相当于乘2, 逻辑右移的功能相当于除2,利用这个特性,在VB程序中用乘2和除2方法实现位的左移和右移,然后再用And(与)和Or(或)逻辑运算指令,判断移位过程中是否有进位发生,将进行标志置位。
程序清单
CF是进位标志,采用Boolean类型逻辑变量,如果CF为True表示有进位,为False则表示无进位。
Public CF As Boolean '进位标志
'1.逻辑左移
Public Function SHL(OPR As Byte, n As Integer) As Byte
Dim BD As Byte
Dim I As Integer
BD = OPR
For i = 1 To n - 1
BD = (BD And H7F) * 2 '将D7位屏蔽左移,防止字节溢出
Next i
CF = BD And H80 '判断D7位是否进位
SHL = (BD And H7F) * 2
End Function
'2.逻辑右移
Public Function SHR(OPR As Byte, n As Integer) As Byte
Dim BD As Byte
Dim I As Integer
BD = OPR
For i = 1 To n - 1
BD = BD \ 2 '右移
Next i
CF = BD And 1 '判断D0位是否进位
SHR = BD \ 2
End Function
'3.算术右移
Public Function SAR(OPR As Byte, n As Integer) As Byte
Dim BD As Byte
Dim I As Integer
Dim Fg1 As Byte
BD = OPR
Fg1 = BD And H80
For i = 1 To n - 1
BD = BD \ 2 '右移
Next i
CF = BD And 1 '判断D0位是否进位
BD = BD \ 2 '右移
SAR = BD Or Fg1
End Function
'4.循环左移
Public Function ROL(OPR As Byte, n As Integer) As Byte
Dim BD As Byte
Dim I As Integer
Dim Fg1 As Byte
BD = OPR
For i = 1 To n
Fg1 = (BD And H80) \ 128 '判断D7位是否进位
BD = ((BD And H7F) * 2) Or Fg1 '带进位左移
Next i
CF = Fg1
ROL = BD
End Function
'5.循环右移
Public Function ROR(OPR As Byte, n As Integer) As Byte
Dim BD As Byte
Dim I As Integer
Dim Fg1 As Byte
Dim Fg2 As Byte
BD = OPR
For i = 1 To n
Fg1 = (BD And 1) * 128 '判断D0位是否进位
BD = (BD \ 2) Or Fg1 '带进位右移
Next i
CF = Fg1
ROR = BD
End Function
'6.进位循环左移
Public Function RCL(OPR As Byte, n As Integer) As Byte
Dim BD As Byte
Dim I As Integer
Dim Fg1 As Byte
Dim Fg2 As Byte
BD = OPR
Fg2 = CF And 1
For i = 1 To n
Fg1 = (BD And H80) \ 128 '判断D7位是否进位
BD = ((BD And H7F) * 2) Or Fg2 '带进位左移
Fg2 = Fg1
Next i
CF = Fg1
RCL = BD
End Function
'7.进位循环右移
Public Function RCR(OPR As Byte, n As Integer) As Byte
Dim BD As Byte
Dim I As Integer
Dim Fg1 As Byte
Dim Fg2 As Byte
BD = OPR
Fg2 = CF And 128
For i = 1 To n
Fg1 = (BD And 1) * 128 '判断D0位是否进位
BD = (BD \ 2) Or Fg2 '带进位右移
Fg2 = Fg1
Next i
CF = Fg1
RCR = BD
End Function
结束语
以上七个字节移位操作函数的功能和用法同宏汇编语言的移位操作指令基本相同,不过只对单字节进行操作,但上述程序稍加改动后,就可对双字节Integer类型和四字节Long类型进行移位操作。