VB.NET中的常用方法


一、如何使用dll庫:

  dll庫是動態鏈接庫,一般是別人提供的,用來做二次開發,相當於別人把一些函數包裝在dll中,已經生成可以鏈接文件,你只能調用,但是不能看到方法的實現。所以給你提供dll的人一般也會提供dll的庫函數說明,說明中會告訴你里面的函數的返回值,參數等一些東西。

  1、要使用dll庫中的方法,首先把dll復制粘貼在你的.net項目中的\bin\Debug路徑下面。

  2、在項目中新建一個模塊Module,然后在里面聲明要用的函數。例如:我新建一個叫API的模塊,然后聲明函數,具體過程如下:

  

Imports System.Text
Module API

    '驅動加載函數
    Public Declare Function RDR_LoadReaderDrivers Lib "rfidlib_reader.dll" (ByVal path As String) As Integer
    Public Declare Function RDR_GetLoadedReaderDriverCount Lib "rfidlib_reader.dll" () As Integer

    '設備連接、斷開函數
    Public Declare Function RDR_Open Lib "rfidlib_reader.dll" (ByVal connstr As String, ByRef hr As UIntPtr) As Integer
    Public Declare Function RDR_Close Lib "rfidlib_reader.dll" (ByVal hr As UIntPtr) As Integer

    '獲取錯誤代碼
    Public Declare Function RDR_GetReaderLastReturnError Lib "rfidlib_reader.dll" (ByVal hr As UIntPtr) As Integer

    '
    Public Declare Function RDR_GetTagDataReportCount Lib "rfidlib_reader.dll" (ByVal hr As UIntPtr) As UInt32



    '以下是讀取讀卡器參數的函數
    Public Declare Function RDR_GetReaderInfor Lib "rfidlib_reader.dll" (ByVal hr As UIntPtr, ByVal Type As Byte, ByRef buffer As StringBuilder, ByRef nSize As UInt32) As Integer

    '以下是盤卡函數
    Public Declare Function RDR_CreateInvenParamSpecList Lib "rfidlib_reader.dll" () As UIntPtr
    Public Declare Function ISO15693_CreateInvenParam Lib "rfidlib_aip_iso15693.dll" (ByVal hInvenParamSpecList As UIntPtr, ByVal AntennaID As Byte, ByVal en_afi As Byte, ByVal afi As Byte, ByVal slot_type As Byte) As UIntPtr
    Public Declare Function RDR_TagInventory Lib "rfidlib_reader.dll" (ByVal hr As UIntPtr, ByVal AIType As Byte, ByVal AntennaCount As Byte, ByRef AntennaIDs As Byte, ByVal InvenParamSpecList As UIntPtr) As Integer 'AntennaIDs傳入數組的第一個元素地址
    Public Declare Function RDR_GetTagDataReport Lib "rfidlib_reader.dll" (ByVal hr As UIntPtr, ByVal Seekpos As Byte) As UIntPtr
    Public Declare Function ISO15693_ParseTagDataReport Lib "rfidlib_aip_iso15693.dll" (ByVal hTagReport As UIntPtr, ByRef aip_id As UInt32, ByRef tag_id As UInt32, ByRef ant_id As UInt32, ByRef dsfid As Byte, ByRef uid As Byte) As Integer


    '以下是銷毀盤點參數列表
    Public Declare Function DNODE_Destroy Lib "rfidlib_reader.dll" (ByVal dn As UIntPtr) As Integer

    '盤點卡的兩個宏定義
    Public Const RFID_SEEK_FIRST = 1
    Public Const RFID_SEEK_NEXT = 2

    '連接卡
    Public Declare Function ISO15693_Connect Lib "rfidlib_aip_iso15693.dll" (ByVal hr As UIntPtr, ByVal tagType As UInt32, ByVal address_mode As Byte, ByVal uid() As Byte, ByRef ht As UIntPtr) As Integer

    '讀卡
    Public Declare Function ISO15693_ReadMultiBlocks Lib "rfidlib_aip_iso15693.dll" (ByVal hr As UIntPtr, ByVal ht As UIntPtr, ByVal readSecSta As Byte, ByVal blkAddr As UInt32, ByVal numOfBlksToRead As UInt32, ByRef numOfBlksRead As UInt32, ByRef bufBlocks As Byte, ByVal nSize As UInt32, ByRef bytesBlkDatRead As UInt32) As Integer

    '寫卡
    Public Declare Function ISO15693_WriteMultipleBlocks Lib "rfidlib_aip_iso15693.dll" (ByVal hr As UIntPtr, ByVal ht As UIntPtr, ByVal blkAddr As UInt32, ByVal numOfBlks As UInt32, ByVal newBlksData() As Byte, ByVal bytesToWrite As UInt32) As Integer









    Public Declare Function LSG_CmdGetReports Lib "rfidlib_LSGate.dll" (ByVal hr As Long, ByVal Flag As Byte, ByVal recordsToGet As Byte) As Integer
    Public Declare Function LSG_ParseSCEventData Lib "rfidlib_LSGate.dll" (ByVal hr As Long, ByVal slData As String, ByVal nSize As Long, ByRef dir As Byte, ByRef m_time As Byte) As Integer
    Public Declare Function LSG_CmdGetCurrentFlowOfPeople Lib "rfidlib_LSGate.dll" (ByVal hr As Long, ByRef inFlow As Long, ByRef outFlow As Long) As Integer
    Public Declare Function LSG_CmdResetFlowOfPeople Lib "rfidlib_LSGate.dll" (ByVal hr As Long, ByVal mFlg As Byte) As Integer
    Public Declare Function LSG_CmdReverseDirection Lib "rfidlib_LSGate.dll" (ByVal hr As Long) As Integer
    Public Declare Function LSG_CmdGetSystemTime Lib "rfidlib_LSGate.dll" (ByVal hr As Long, ByRef year As Long, ByRef month As Byte, ByRef day As Byte, ByRef hour As Byte, ByRef minute As Byte, ByRef second As Byte) As Integer
    Public Declare Function LSG_CmdSetSystemTime Lib "rfidlib_LSGate.dll" (ByVal hr As Long, ByVal year As Long, ByVal month As Byte, ByVal day As Byte, ByVal hour As Byte, ByVal minute As Byte, ByVal second As Byte) As Integer


    Public Declare Function MultiByteToWideChar Lib "kernel32 " (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
    Public Declare Function WideCharToMultiByte Lib "kernel32 " (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
    Public Const CP_ACP = 0 ' default to ANSI code page
    Public Const CP_UTF8 = 65001 ' default to UTF-8 code page

End Module

 

聲明完的函數我就可以在項目中調用了,例如:

     '設備連接串
        Dim connStr = "RDType=RD201;CommType=COM;COMName=" + CmboxcommCanBeUse.SelectedItem + ";BaudRate=38400;Frame=8E1;BusAddr=255"
        Dim connectOK As Integer = 99
        connectOK = RDR_Open(connStr, hr)

以上代碼中的RDR_Open()函數就是在API模塊中聲明的,函數所在的位置是rfidlib_reader.dll中。

 

二、常用的一些方法:

Now()     獲取當前日期時間,

Format() 格式化日期時間函數:

 Format(Now(), "yyyy-MM-dd-hh-mm-ss")

 

DataGridView用法:

GoodDataGridView.Rows.Add()為添加一行

GoodDataGridView.Rows.RemoveAt(1) 為刪除第一行

GoodDataGridView(0, 0).Value = "序號" 為給某列某行的單元格設置值。其中(0,0)中前面的數字代表第幾列,后面的數字代表第幾行。
     GoodDataGridView.Rows.Add()
        GoodDataGridView(0, 0).Value = "序號"
        GoodDataGridView(1, 0).Value = "商品編碼"
        GoodDataGridView(2, 0).Value = "商品名稱"
        GoodDataGridView(3, 0).Value = "商品價格"

 

定義數組並初始化:

      '定義一組商品的屬性的二進制編碼,用來保存當前連接的標簽的內容
        Dim goodsNumb() As Byte = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        Dim goodsName() As Byte = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        Dim goodsPric() As Byte = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

 

 UTF8 編碼與解碼:

Imports System.Text

Module UTF8

    'UTF8編碼函數
    Public Function StringToUTF8Bytes(ByVal strData As String) As Byte()

        Dim bytes() As Byte
        bytes = Encoding.UTF8.GetBytes(strData)
        Return bytes

    End Function

    'UTF8解碼函數
    Public Function UTF8BytesToString(ByVal strData() As Byte) As String

        Dim retstring As String
        retstring = Encoding.UTF8.GetString(strData)
        Return retstring

    End Function

End Module

 

datagridview隔行顯示不同的顏色:

datagridview1.RowsDefaultCellStyle.BackColor = Color.Bisque

datagridview1.AlternatingRowsDefaultCellStyle.BackColor =Color.Beige

 

datagridview設置標題字體樣式:

設置ColumnHeaderDefaultCellStyle的Font屬性
或者編程
datagridview.Columns[index].DefaultCellStyle.Font.Size=size

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM