見下面代碼,放置定時(shí)器,按鈕,標(biāo)簽各一個(gè)
創(chuàng)新互聯(lián)是一家網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè),提供網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),網(wǎng)站制作,建網(wǎng)站,按需設(shè)計(jì)網(wǎng)站,網(wǎng)站開發(fā)公司,從2013年成立是互聯(lián)行業(yè)建設(shè)者,服務(wù)者。以提升客戶品牌價(jià)值為核心業(yè)務(wù),全程參與項(xiàng)目的網(wǎng)站策劃設(shè)計(jì)制作,前端開發(fā),后臺程序制作以及后期項(xiàng)目運(yùn)營并提出專業(yè)建議和思路。
Public?Class?Form1
Private?Sub?Timer1_Tick(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Timer1.Tick?'定時(shí)器
If?Label1.Right??Me.Width?Then
Label1.Left?+=?10'移動(dòng)距離
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?'開啟或者關(guān)閉定時(shí)器
Timer1.Interval?=?1000?'1秒
If?Timer1.Enabled?=?False?Then
Timer1.Start()?'開定時(shí)器
Else
Timer1.Stop()?'關(guān)定時(shí)器
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的變化
如果是做小游戲,建議弄成類吧,把其中的左(右)移或?qū)瞧渌苿?dòng)弄成其接口,以便操作。
由于涉及內(nèi)容太多,不再詳述。原理都一樣
窗體是指由兩個(gè)列表框(ListBox1、ListBox2)和4個(gè)命令按鈕(Button1“”按鈕,Button2“”按鈕,Button3“”按鈕,Button4“”按鈕)所構(gòu)成的界面,代碼:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.Text = "選項(xiàng)移動(dòng)"
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個(gè)0
結(jié)果就是:10000000000
五、按位運(yùn)算符
注意不能在VB.Net中直接使用以下二進(jìn)制數(shù),使用時(shí)需轉(zhuǎn)換為相應(yīng)的十六或十進(jìn)制數(shù)字。
1. 表示按位左移,空缺處補(bǔ)0。如:101101 2 結(jié)果為 110100
2. 表示按位右移,如:101101 2 結(jié)果為 001011
VB程序中實(shí)現(xiàn)字節(jié)移位操作
在利用VB系統(tǒng)開發(fā)數(shù)據(jù)采集或工業(yè)控制軟件,或?qū)ξ募M(jìn)行低級操作時(shí),常需要對字節(jié)進(jìn)行移位操作,但VB系統(tǒng)中沒有提供字節(jié)移位操作的指令和函數(shù),只提供了And(與)、Or(或)、Xor(異或)、Equ(同或)、Not(非)等幾個(gè)邏輯運(yùn)算指令。筆者在用VB系統(tǒng)開發(fā)工業(yè)控制軟件的過程中,就遇到了這個(gè)問題,于是利用VB中已有的邏輯運(yùn)算指令,模擬匯編語言的字節(jié)移位指令,編制了七個(gè)字節(jié)移位函數(shù):邏輯左移、邏輯右移、算術(shù)右移、循環(huán)左移、循環(huán)右移、進(jìn)位循環(huán)左移和進(jìn)位循環(huán)右移。
在匯編語言指令中,邏輯左移的功能相當(dāng)于乘2, 邏輯右移的功能相當(dāng)于除2,利用這個(gè)特性,在VB程序中用乘2和除2方法實(shí)現(xiàn)位的左移和右移,然后再用And(與)和Or(或)邏輯運(yùn)算指令,判斷移位過程中是否有進(jìn)位發(fā)生,將進(jìn)行標(biāo)志置位。
程序清單
CF是進(jìn)位標(biāo)志,采用Boolean類型邏輯變量,如果CF為True表示有進(jìn)位,為False則表示無進(jìn)位。
Public CF As Boolean '進(jìn)位標(biāo)志
'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位屏蔽左移,防止字節(jié)溢出
Next i
CF = BD And H80 '判斷D7位是否進(jìn)位
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位是否進(jìn)位
SHR = BD \ 2
End Function
'3.算術(shù)右移
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位是否進(jìn)位
BD = BD \ 2 '右移
SAR = BD Or Fg1
End Function
'4.循環(huán)左移
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位是否進(jìn)位
BD = ((BD And H7F) * 2) Or Fg1 '帶進(jìn)位左移
Next i
CF = Fg1
ROL = BD
End Function
'5.循環(huán)右移
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位是否進(jìn)位
BD = (BD \ 2) Or Fg1 '帶進(jìn)位右移
Next i
CF = Fg1
ROR = BD
End Function
'6.進(jìn)位循環(huán)左移
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位是否進(jìn)位
BD = ((BD And H7F) * 2) Or Fg2 '帶進(jìn)位左移
Fg2 = Fg1
Next i
CF = Fg1
RCL = BD
End Function
'7.進(jìn)位循環(huán)右移
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位是否進(jìn)位
BD = (BD \ 2) Or Fg2 '帶進(jìn)位右移
Fg2 = Fg1
Next i
CF = Fg1
RCR = BD
End Function
結(jié)束語
以上七個(gè)字節(jié)移位操作函數(shù)的功能和用法同宏匯編語言的移位操作指令基本相同,不過只對單字節(jié)進(jìn)行操作,但上述程序稍加改動(dòng)后,就可對雙字節(jié)Integer類型和四字節(jié)Long類型進(jìn)行移位操作。