📄 phonelistbox.vb
字号:
'Filename : PhoneListBox.vb
'Part of : Phone Navigator VB.NET example
'Description : Implementation of phone navigator's phone list box
'Version : 3.2
'
'This example is only to be used with PC Connectivity API version 3.2.
'Compability ("as is") with future versions is not quaranteed.
'
'Copyright (c) 2005-2007 Nokia Corporation.
'
'This material, including but not limited to documentation and any related
'computer programs, is protected by intellectual property rights of Nokia
'Corporation and/or its licensors.
'All rights are reserved. Reproducing, modifying, translating, or
'distributing any or all of this material requires the prior written consent
'of Nokia Corporation. Nokia Corporation retains the right to make changes
'to this material at any time without notice. A copyright license is hereby
'granted to download and print a copy of this material for personal use only.
'No other license to any other intellectual property rights is granted. The
'material is provided "as is" without warranty of any kind, either express or
'implied, including without limitation, any warranty of non-infringement,
'merchantability and fitness for a particular purpose. In no event shall
'Nokia Corporation be liable for any direct, indirect, special, incidental,
'or consequential loss or damages, including but not limited to, lost profits
'or revenue,loss of use, cost of substitute program, or loss of data or
'equipment arising out of the use or inability to use the material, even if
'Nokia Corporation has been advised of the likelihood of such damages occurring.
Imports System
Imports System.Runtime.InteropServices
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.IO
'===================================================================
' PhoneListBox
Public Class PhoneListBox
Inherits System.Windows.Forms.ListBox
Private bDisposed As Boolean = False
Private ItemData() As String
Private m_hDMHandle As Integer ' Device management handle
Private m_strSerialNumbers() As String
Private m_strFriendlyNames() As String
'===================================================================
' Constructor
'
'===================================================================
Public Sub New()
Dim ret As Integer
' Initialize Device Management APi
Dim iRet As Integer = DMAPI_Initialize(DMAPI_VERSION_32, vbNullString)
If iRet <> CONA_OK Then ShowErrorMessage("DMAPI_Initialize", iRet)
' Get Device management handle
ret = CONAOpenDM(m_hDMHandle)
If ret <> CONA_OK Then ShowErrorMessage("CONAOpenDM", ret)
' Register device notification callback function
pDeviceCallBack = AddressOf DeviceNotifyCallback
ret = CONARegisterNotifyCallback(m_hDMHandle, API_REGISTER, pDeviceCallBack)
If ret <> CONA_OK Then ShowErrorMessage("CONARegisterNotifyCallback", ret)
End Sub
'===================================================================
' Destructor
'
'===================================================================
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
Dim ret As Integer
If Not bDisposed Then
If m_hDMHandle <> 0 Then
' Unregister device notification callback function
ret = CONARegisterNotifyCallback(m_hDMHandle, API_UNREGISTER, pDeviceCallBack)
If ret <> CONA_OK Then ShowErrorMessage("CONARegisterNotifyCallback", ret)
' Close device management handle
ret = CONACloseDM(m_hDMHandle)
If ret <> CONA_OK Then ShowErrorMessage("CONACloseDM", ret)
End If
' Terminate Device Management APi
Dim iRet As Integer = DMAPI_Terminate(0)
If iRet <> CONA_OK Then ShowErrorMessage("DMAPI_Terminate", iRet)
MyBase.Dispose(disposing)
End If
bDisposed = True
End Sub
'===================================================================
' GetCurrentSN
'
' Returns serial number of currently selected phone
'
'===================================================================
Function GetCurrentSN() As String
GetCurrentSN = ""
Dim index As Integer = SelectedIndex
If index <> -1 Then
' there is a selected item
GetCurrentSN = m_strSerialNumbers(index)
End If
End Function
'===================================================================
' GetCurrentFriendlyName
'
' Returns friendly name of currently selected phone
'
'===================================================================
Function GetCurrentFriendlyName() As String
GetCurrentFriendlyName = ""
Dim index As Integer = SelectedIndex
If index <> -1 Then
' there is a selected item
GetCurrentFriendlyName = m_strFriendlyNames(index)
End If
End Function
'===================================================================
' ListAllPhones
'
' Adds all connected phones to list box
'
'===================================================================
Public Sub ListAllPhones()
On Error Resume Next
Dim i As Short
Dim ret As Integer
Dim piCount As Integer
Dim iPtr As Int64
Dim buffer As IntPtr
Dim ptr As IntPtr
Dim strText As String
Dim pDevices() As CONAPI_DEVICE
MainForm.Timer1.Enabled = False
ResetContent()
' Querying count of connected devices
ret = CONAGetDeviceCount(m_hDMHandle, piCount)
If ret <> CONA_OK Then ShowErrorMessage("CONAGetDeviceCount", ret)
If ret = CONA_OK And piCount > 0 Then
ReDim pDevices(piCount)
ReDim m_strSerialNumbers(piCount)
ReDim m_strFriendlyNames(piCount)
' Allocate memory for buffer
buffer = Marshal.AllocHGlobal(piCount * Marshal.SizeOf(GetType(CONAPI_DEVICE)))
' Get list of currently connected devices
ret = CONAGetDevices(m_hDMHandle, piCount, buffer)
If ret <> CONA_OK Then
ShowErrorMessage("CONAGetDevices", ret)
Else
ReDim ItemData(piCount)
' Add each device to the phone list box
For i = 0 To piCount - 1
' Calculate beginning of CONAPI_DEVICE structure of item 'i'
iPtr = buffer.ToInt64 + i * Marshal.SizeOf(GetType(CONAPI_DEVICE))
' Convert integer to pointer
ptr = IntPtr.op_Explicit(iPtr)
' Copy data from buffer
pDevices(i) = Marshal.PtrToStructure(ptr, GetType(CONAPI_DEVICE))
' Add item to list box
strText = pDevices(i).pstrFriendlyName & " ("
strText &= pDevices(i).pstrManufacturer & " "
strText &= pDevices(i).pstrModel & ")"
Me.Items.Add(strText)
m_strSerialNumbers(i) = pDevices(i).pstrSerialNumber
m_strFriendlyNames(i) = pDevices(i).pstrFriendlyName
Next
' CONAGetDevices allocates memory for the member variables in
' CONAPI_DEVICE and it is callers responsibility to free it...
ret = CONAFreeDeviceStructure(piCount, buffer)
If ret <> CONA_OK Then ShowErrorMessage("CONAFreeDeviceStructure", ret)
End If
Marshal.FreeHGlobal(buffer)
End If
MainForm.Timer1.Enabled = True
End Sub
'===================================================================
' ResetContent
'
' Clear contents of list box
'
'===================================================================
Public Sub ResetContent()
Me.Items.Clear()
End Sub
'===================================================================
' PhoneListBox_DoubleClick
'
' Called when user doubleclicks list item
'
'===================================================================
Private Sub PhoneListBox_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
' currently selected device
Dim strSerial As String = GetCurrentSN()
If strSerial = vbNullString Or strSerial = "" Then
MsgBox("Please select a phone")
Return
End If
Dim infoDlg As FRM_DeviceInfo = New FRM_DeviceInfo
infoDlg.ShowDialog()
End Sub
'===================================================================
' GetItemData
'
' Return item data of list box item
'
'===================================================================
Public Function GetItemData(ByVal index As Integer) As String
GetItemData = ItemData(index)
End Function
'===================================================================
' GetDMHandle
'
' Return device management handle
'
'===================================================================
Public Function GetDMHandle() As Integer
GetDMHandle = m_hDMHandle
End Function
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -