📄 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
Const g_strPhoneRoot As String = "\\" ' "\\" means phone root
Private bDisposed As Boolean = False
Private m_wState As Integer
Private m_strCurrentFolder As String
Private m_strCurrentSN As String
Private m_strCurrentFriendlyName As String
Private ItemData() As String
Private m_hDMHandle As Integer ' Device management handle
Private pfnCallBack As DeviceNotifyCallbackDelegate
Private m_strSerialNumbers() As String
Private m_strFriendlyNames() As String
<MarshalAs(UnmanagedType.LPWStr)> Dim pstrMemoryTypes 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)
m_strCurrentFolder = ""
' 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
'===================================================================
' PhoneItemDblClicked
'
' Called, when user has doubleclicked phone item
' If item is [..], shows parent folder
' If item is [foldername], shows subfolder
'
'===================================================================
Sub PhoneItemDblClicked()
Dim strSelectedTxt, strCurrentFolder As String
Dim index As Integer = SelectedIndex
If index <> -1 Then
strSelectedTxt = Items(SelectedIndex).ToString()
If strSelectedTxt = "[..]" Then
' go up in folder tree
strCurrentFolder = GetSelectedFolder()
If strCurrentFolder = g_strPhoneRoot Then
' root folder --> show phone list
ResetContent()
m_strCurrentFolder = ""
m_wState = PHONELIST_STATE_PHONELIST
ListAllPhones()
Else
' getting parent folder of strCurrentFolder:
If strCurrentFolder <> g_strPhoneRoot Then
strCurrentFolder = strCurrentFolder.Substring(0, strCurrentFolder.LastIndexOf("\"))
If strCurrentFolder = "\" Then
' move to root folder
strCurrentFolder = g_strPhoneRoot
End If
End If
ShowPhoneFolder(strCurrentFolder)
End If
ElseIf strSelectedTxt.Substring(0, 1) = "[" Then
' selected item is folder
ShowPhoneFolder(GetSelectedFolder())
Else
' selected item is file
Trace.WriteLine("PhoneListBox::PhoneItemDblClicked(): Double clicked file %s --> ignoring...\n", strSelectedTxt)
End If
End If
End Sub
'===================================================================
' ShowFolders
'
' Adds all found folders to listbox by using CONAFindNextFolder.
'
'===================================================================
Sub ShowFolders(ByVal hFindHandle As Integer)
Dim FolderInfo As CONAPI_FOLDER_INFO
Dim iResult As Integer = 0
' Allocate memory for buffer
Dim Buffer As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(CONAPI_FOLDER_INFO)))
iResult = CONAFindNextFolder(hFindHandle, Buffer)
While iResult = CONA_OK
' Copy data from buffer
FolderInfo = Marshal.PtrToStructure(Buffer, GetType(CONAPI_FOLDER_INFO))
Dim index As Integer = Me.Items.Add("[" + FolderInfo.pstrName + "]")
' Setting folder name as itemdata
ReDim Preserve ItemData(Me.Items.Count + 1)
ItemData(index) = FolderInfo.pstrName
iResult = CONAFreeFolderInfoStructure(Buffer)
If iResult <> CONA_OK Then
ShowErrorMessage("PhoneListBox::ShowFolders(): CONAFreeFolderInfoStructure failed", iResult)
End If
iResult = CONAFindNextFolder(hFindHandle, Buffer)
End While
If iResult <> ECONA_ALL_LISTED And iResult <> CONA_OK Then
ShowErrorMessage("PhoneListBox::ShowFolders(): CONAFindNextFolder failed!", iResult)
End If
End Sub
'===================================================================
' ShowFiles
'
' Adds all found files to listbox by using CONAFindNextFile.
'
'===================================================================
Sub ShowFiles(ByVal hFindHandle As Integer)
Dim FileInfo As CONAPI_FILE_INFO
Dim iResult As Integer
' Allocate memory for buffer
Dim Buffer As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(CONAPI_FILE_INFO)))
iResult = CONAFindNextFile(hFindHandle, Buffer)
While iResult = CONA_OK
' Copy data from buffer
FileInfo = Marshal.PtrToStructure(Buffer, GetType(CONAPI_FILE_INFO))
Dim index As Integer = Me.Items.Add(FileInfo.pstrName)
' Setting file name as itemdata
ReDim Preserve ItemData(Me.Items.Count + 1)
ItemData(index) = FileInfo.pstrName
iResult = CONAFreeFileInfoStructure(Buffer)
If iResult <> CONA_OK Then
ShowErrorMessage("PhoneListBox::ShowFiles(): CONAFreeFileInfoStructure failed!", iResult)
End If
iResult = CONAFindNextFile(hFindHandle, Buffer)
End While
If iResult <> ECONA_ALL_LISTED And iResult <> CONA_OK Then
ShowErrorMessage("PhoneListBox::ShowFiles(): CONAFindNextFile failed!", iResult)
End If
End Sub
'===================================================================
' ShowPhoneFolder
'
' Adds all files and folders to listbox by using
' functions CONAFindBegin and CONAFindEnd.
'
'===================================================================
Sub ShowPhoneFolder(ByVal strFolder As String)
Dim hFS As Integer = 0
Dim hFind As Integer = 0
Dim iMedia As Integer = API_MEDIA_ALL
Dim iDeviceID As Integer = 0
Dim iResult As Integer = CONAOpenFS(m_strCurrentSN, iMedia, hFS, iDeviceID)
If iResult = CONA_OK Then
Dim iFindOptions As Integer = 0
If MainForm.CheckBoxUseCache.Checked Then
iFindOptions = iFindOptions Or CONA_FIND_USE_CACHE
End If
iResult = CONAFindBegin(hFS, iFindOptions, hFind, strFolder)
If iResult = CONA_OK Then
ResetContent()
Me.Items.Add("[..]")
ShowFolders(hFind)
ShowFiles(hFind)
m_strCurrentFolder = strFolder
iResult = CONAFindEnd(hFind)
If iResult <> CONA_OK Then
ShowErrorMessage("PhoneListBox::ShowPhoneFolder(): CONAFindEnd failed!", iResult)
End If
Else
ShowErrorMessage("PhoneListBox::ShowPhoneFolder(): CONAFindBegin() failed!", iResult)
ListAllPhones()
End If
iResult = CONACloseFS(hFS)
If iResult <> CONA_OK Then
ShowErrorMessage("PhoneListBox::ShowPhoneFolder(): CONACloseFS failed!", iResult)
End If
Else
ShowErrorMessage("PhoneListBox::ShowPhoneFolder(): CONAOpenFS failed!", iResult)
End If
Dim Device As CONAPI_DEVICE
' Allocate memory for buffer
Dim Buffer As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(CONAPI_DEVICE)))
iResult = CONAGetDevice(m_hDMHandle, m_strCurrentSN, Buffer)
If iResult = CONA_OK Then
' Copy data from buffer
Device = Marshal.PtrToStructure(Buffer, GetType(CONAPI_DEVICE))
Dim mFN As String = Device.pstrFriendlyName
MainForm.LBL_PhoneFiles.Text = mFN + ":" + strFolder
iResult = CONAFreeDeviceStructure(1, Buffer)
If iResult <> CONA_OK Then
ShowErrorMessage("PhoneListBox::ShowPhoneFolder(): CONAFreeDeviceStructure failed!", iResult)
End If
End If
End Sub
'===================================================================
' GetCurrentSN
'
' Returns serial number of currently selected phone
'
'===================================================================
Function GetCurrentSN() As String
GetCurrentSN = ""
If m_wState = PHONELIST_STATE_PHONELIST Then
' phone list is shown
Dim index As Integer = SelectedIndex
If index <> -1 Then
' there is a selected item
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -