- 积分
- 2468
- 明经币
- 个
- 注册时间
- 2004-7-22
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2005-1-6 11:41:00
|
显示全部楼层
33. 用键盘方向键控制COMBOX Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As Long Const CB_SHOWDROPDOWN = &H14F Dim bDrop As Boolean Private isDo As Boolean Private Sub Combo1_Click() If Not isDo Then isDo = True '<----------回置状态 Exit Sub Else: MsgBox "safd" End If End Sub Private Sub Combo1_DropDown() bDrop = True End Sub Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = 40 Then isDo = False SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, 1, 0 ElseIf KeyCode = 38 Then isDo = False If Combo1.ListIndex = 0 Then If bDrop Then bDrop = False SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, 0, 0 End If End If End If End Sub Private Sub Combo1_KeyUp(KeyCode As Integer, Shift As Integer) If Combo1.Text = Combo1.List(0) Then isDo = True End If End Sub Private Sub Form_Load() isDo = True Combo1.AddItem "abcd" Combo1.AddItem "abcd1" Combo1.AddItem "abcd2" Combo1.AddItem "abcd3" End Sub 35.VB下的CRC校验程序 一 计算法 计算法就是依据CRC校验码的产生原理来设计程序。其优点是模块代码少,修改灵活,可移植性好。其缺点为计算量大。为了便于理解,这里假
定了三位数据,而多项式码为A001(hex)。 在窗体上放置一命令按钮Command1,并添加如下代码:
Private Sub Command1_Click() Dim CRC() As Byte Dim d() As Byte '待传输数据 ReDim d(2) As Byte d(0) = 123 d(1) = 112 d(2) = 135 CRC = CRC16(d) '调用CRC16计算函数 'CRC(0)为高位 'CRC(1)为低位 End Sub 注意:在数据传输时CRC的低位可能在前,而高位在后。
Function CRC16(data() As Byte) As String Dim CRC16Lo As Byte, CRC16Hi As Byte 'CRC寄存器 Dim CL As Byte, CH As Byte '多项式码&HA001 Dim SaveHi As Byte, SaveLo As Byte Dim i As Integer Dim Flag As Integer CRC16Lo = &HFF CRC16Hi = &HFF CL = &H1 CH = &HA0 For i = 0 To UBound(data) CRC16Lo = CRC16Lo Xor data(i) '每一个数据与CRC寄存器进行异或 For Flag = 0 To 7 SaveHi = CRC16Hi SaveLo = CRC16Lo CRC16Hi = CRC16Hi \ 2 '高位右移一位 CRC16Lo = CRC16Lo \ 2 '低位右移一位 If ((SaveHi And &H1) = &H1) Then '如果高位字节最后一位为1 CRC16Lo = CRC16Lo Or &H80 '则低位字节右移后前面补1 End If '否则自动补0 If ((SaveLo And &H1) = &H1) Then '如果LSB为1,则与多项式码进行异或 CRC16Hi = CRC16Hi Xor CH CRC16Lo = CRC16Lo Xor CL End If Next Flag Next i Dim ReturnData(1) As Byte ReturnData(0) = CRC16Hi 'CRC高位 ReturnData(1) = CRC16Lo 'CRC低位 CRC16 = ReturnData End Function
2.查表法 查表法的优缺点与计算法的正好相反。为了便于比较,这里所有的假定与计算法的完全相同,都而在窗体上放置一个Command1的按钮,其
代码部分与上面的也完全一致。下面只介绍CRC函数的编写源代码。
Private Function CRC16(data() As Byte) As String Dim CRC16Hi As Byte Dim CRC16Lo As Byte CRC16Hi = &HFF CRC16Lo = &HFF Dim i As Integer Dim iIndex As Long For i = 0 To UBound(data) iIndex = CRC16Lo Xor data(i) CRC16Lo = CRC16Hi Xor GetCRCLo(iIndex) '低位处理 CRC16Hi = GetCRCHi(iIndex) '高位处理 Next i Dim ReturnData(1) As Byte ReturnData(0) = CRC16Hi 'CRC高位 ReturnData(1) = CRC16Lo 'CRC低位 CRC16 = ReturnData End Function
'CRC低位字节值表 Function GetCRCLo(Ind As Long) As Byte GetCRCLo = Choose(Ind + 1, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40,
&H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1,
&H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80,
&H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0,
&HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0,
&H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81,
&H40, &H1, &HC0, &H80, &H41, &H1, &HC0, _ &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80,
&H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0,
&HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1,
&H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81,
&H40, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H0, &HC1, &H81, &H40, &H1,
&HC0, &H80, &H41, &H0, &HC1, &H81, &H40, &H1, &HC0, &H80, &H41, &H1, &HC0, &H80, &H41, &H0, &HC1, &H81, &H40) End Function
'CRC高位字节值表 Function GetCRCHi(Ind As Long) As Byte GetCRCHi = Choose(Ind + 1, &H0, &HC0, &HC1, &H1, &HC3, &H3, &H2, &HC2, &HC6, &H6, &H7, &HC7, &H5, &HC5, &HC4, &H4,
&HCC, &HC, &HD, &HCD, &HF, &HCF, &HCE, &HE, &HA, &HCA, &HCB, &HB, &HC9, &H9, &H8, &HC8, &HD8, &H18, &H19, &HD9, &H1B, &HDB,
&HDA, &H1A, &H1E, &HDE, &HDF, &H1F, &HDD, &H1D, &H1C, &HDC, &H14, &HD4, &HD5, &H15, &HD7, &H17, &H16, &HD6, &HD2, &H12, &H13,
&HD3, &H11, &HD1, &HD0, &H10, &HF0, &H30, &H31, &HF1, &H33, &HF3, &HF2, &H32, &H36, &HF6, &HF7, &H37, &HF5, &H35, &H34, &HF4,
&H3C, &HFC, &HFD, &H3D, &HFF, &H3F, &H3E, &HFE, &HFA, &H3A, &H3B, &HFB, &H39, &HF9, &HF8, &H38, &H28, &HE8, &HE9, &H29, &HEB,
&H2B, &H2A, &HEA, &HEE, &H2E, &H2F, &HEF, &H2D, &HED, &HEC, &H2C, &HE4, &H24, &H25, &HE5, &H27, &HE7, &HE6, &H26, &H22, &HE2,
&HE3, &H23, &HE1, &H21, &H20, &HE0, &HA0, &H60, _ &H61, &HA1, &H63, &HA3, &HA2, &H62, &H66, &HA6, &HA7, &H67, &HA5, &H65, &H64, &HA4, &H6C, &HAC, &HAD, &H6D, &HAF, &H6F, &H6E,
&HAE, &HAA, &H6A, &H6B, &HAB, &H69, &HA9, &HA8, &H68, &H78, &HB8, &HB9, &H79, &HBB, &H7B, &H7A, &HBA, &HBE, &H7E, &H7F, &HBF,
&H7D, &HBD, &HBC, &H7C, &HB4, &H74, &H75, &HB5, &H77, &HB7, &HB6, &H76, &H72, &HB2, &HB3, &H73, &HB1, &H71, &H70, &HB0, &H50,
&H90, &H91, &H51, &H93, &H53, &H52, &H92, &H96, &H56, &H57, &H97, &H55, &H95, &H94, &H54, &H9C, &H5C, &H5D, &H9D, &H5F, &H9F,
&H9E, &H5E, &H5A, &H9A, &H9B, &H5B, &H99, &H59, &H58, &H98, &H88, &H48, &H49, &H89, &H4B, &H8B, &H8A, &H4A, &H4E, &H8E, &H8F,
&H4F, &H8D, &H4D, &H4C, &H8C, &H44, &H84, &H85, &H45, &H87, &H47, &H46, &H86, &H82, &H42, &H43, &H83, &H41, &H81, &H80, &H40) End Function
36.如何打开光驱 Public Declare Function CDdoor Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString
As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long Call CDdoor("set CDAudio door open", 0, 0, 0) '打开光驱 Call CDdoor("set CDAudio door closed", 0, 0, 0) '关闭光驱
|
|