📄 phonelistbox.cs
字号:
//Filename : PhoneListBox.cs
//Part of : Phone Navigator C# 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.
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
namespace CSPhoneNavigator.NET
{
using PCCSErrors;
using PCCAPIUtils;
using CONADefinitions;
using CONADeviceManagement;
//===================================================================
// PhoneListBox
public class PhoneListBox : System.Windows.Forms.ListBox
{
private bool bDisposed = false;
private string[] ItemData;
// Device management handle
private int m_hDMHandle;
private string[] m_strSerialNumbers;
private string[] m_strFriendlyNames;
//===================================================================
// Constructor
//
//===================================================================
public PhoneListBox()
{
int ret;
// Initialize Device Management APi
int iRet = CONADeviceManagement.DMAPI_Initialize(CONADeviceManagement.DMAPI_VERSION_32, 0);
if (iRet != PCCSErrors.CONA_OK) PCCAPIUtils.ShowErrorMessage("DMAPI_Initialize", iRet);
// Get Device management handle
ret = CONADeviceManagement.CONAOpenDM(ref m_hDMHandle);
if (ret != PCCSErrors.CONA_OK) PCCAPIUtils.ShowErrorMessage("CONAOpenDM", ret);
// Register device notification callback function
Common.pDeviceCallBack = Common.DeviceNotifyCallback;
ret = CONADeviceManagement.CONARegisterNotifyCallback(m_hDMHandle, CONADefinitions.API_REGISTER, Common.pDeviceCallBack);
if (ret != PCCSErrors.CONA_OK) PCCAPIUtils.ShowErrorMessage("CONARegisterNotifyCallback", ret);
}
//===================================================================
// Destructor
//
//===================================================================
protected override void Dispose(bool disposing)
{
int ret;
if (!bDisposed)
{
if (m_hDMHandle != 0)
{
// Unregister device notification callback function
ret = CONADeviceManagement.CONARegisterNotifyCallback(m_hDMHandle, CONADefinitions.API_UNREGISTER, Common.pDeviceCallBack);
if (ret != PCCSErrors.CONA_OK) PCCAPIUtils.ShowErrorMessage("CONARegisterNotifyCallback", ret);
// Close device management handle
ret = CONADeviceManagement.CONACloseDM(m_hDMHandle);
if (ret != PCCSErrors.CONA_OK) PCCAPIUtils.ShowErrorMessage("CONACloseDM", ret);
}
// Terminate Device Management APi
int iRet = CONADeviceManagement.DMAPI_Terminate(0);
if (iRet != PCCSErrors.CONA_OK) PCCAPIUtils.ShowErrorMessage("DMAPI_Terminate", iRet);
base.Dispose(disposing);
}
bDisposed = true;
}
//===================================================================
// GetCurrentSN
//
// Returns serial number of currently selected phone
//
//===================================================================
public string GetCurrentSN()
{
string functionReturnValue = null;
functionReturnValue = "";
int index = SelectedIndex;
if (index != -1)
{
// there is a selected item
functionReturnValue = m_strSerialNumbers[index];
}
return functionReturnValue;
}
//===================================================================
// GetCurrentFriendlyName
//
// Returns friendly name of currently selected phone
//
//===================================================================
public string GetCurrentFriendlyName()
{
string functionReturnValue = null;
functionReturnValue = "";
int index = SelectedIndex;
if (index != -1)
{
// there is a selected item
functionReturnValue = m_strFriendlyNames[index];
}
return functionReturnValue;
}
//===================================================================
// ListAllPhones
//
// Adds all connected phones to list box
//
//===================================================================
public void ListAllPhones()
{
try
{
short i = 0;
int ret = 0;
int piCount = 0;
Int64 iPtr = 0;
IntPtr buffer = IntPtr.Zero;
IntPtr ptr = IntPtr.Zero;
string strText = "";
CONADefinitions.CONAPI_DEVICE[] pDevices = null;
Common.MainForm.Timer1.Enabled = false;
ResetContent();
// Querying count of connected devices
ret = CONADeviceManagement.CONAGetDeviceCount(m_hDMHandle, ref piCount);
if (ret != PCCSErrors.CONA_OK) PCCAPIUtils.ShowErrorMessage("CONAGetDeviceCount", ret);
if (ret == PCCSErrors.CONA_OK & piCount > 0)
{
//pDevices = null;
//pDevices = new CONADefinitions.CONAPI_DEVICE[piCount];
m_strSerialNumbers = null;
m_strSerialNumbers = new string[piCount];
m_strFriendlyNames = null;
m_strFriendlyNames = new string[piCount];
// Allocate memory for buffer
buffer = Marshal.AllocHGlobal(piCount * Marshal.SizeOf(typeof(CONADefinitions.CONAPI_DEVICE)));
// Get list of currently connected devices
ret = CONADeviceManagement.CONAGetDevices(m_hDMHandle, ref piCount, buffer);
if (ret != PCCSErrors.CONA_OK)
{
PCCAPIUtils.ShowErrorMessage("CONAGetDevices", ret);
}
else
{
pDevices = null;
pDevices = new CONADefinitions.CONAPI_DEVICE[piCount];
// Add each device to the phone list box
for (i = 0; i < piCount; i++)
{
// Calculate beginning of CONAPI_DEVICE structure of item 'i'
iPtr = buffer.ToInt64() + i * Marshal.SizeOf(typeof(CONADefinitions.CONAPI_DEVICE));
// Convert integer to pointer
ptr = new IntPtr(iPtr);
// Copy data from buffer
pDevices[i] = (CONADefinitions.CONAPI_DEVICE)Marshal.PtrToStructure(ptr, typeof(CONADefinitions.CONAPI_DEVICE));
// Add item to list box
strText = pDevices[i].pstrFriendlyName + " (";
strText += pDevices[i].pstrManufacturer + " ";
strText += pDevices[i].pstrModel + ")";
this.Items.Add(strText);
m_strSerialNumbers[i] = pDevices[i].pstrSerialNumber;
m_strFriendlyNames[i] = pDevices[i].pstrFriendlyName;
}
// CONAGetDevices allocates memory for the member variables in
// CONAPI_DEVICE and it is callers responsibility to free it...
ret = CONADeviceManagement.CONAFreeDeviceStructure(piCount, buffer);
if (ret != PCCSErrors.CONA_OK) PCCAPIUtils.ShowErrorMessage("CONAFreeDeviceStructure", ret);
}
Marshal.FreeHGlobal(buffer);
}
Common.MainForm.Timer1.Enabled = true;
}
catch
{
Common.MainForm.Timer1.Enabled = true;
}
}
//===================================================================
// ResetContent
//
// Clear contents of list box
//
//===================================================================
public void ResetContent()
{
this.Items.Clear();
}
//===================================================================
// PhoneListBox_DoubleClick
//
// Called when user doubleclicks list item
//
//===================================================================
//private void PhoneListBox_DoubleClick(object sender, System.EventArgs e)
protected override void OnDoubleClick (EventArgs e )
{
// currently selected device
string strSerial = GetCurrentSN();
if (strSerial == null || strSerial == "")
{
MessageBox.Show("Please select a phone");
return;
}
FRM_DeviceInfo infoDlg = new FRM_DeviceInfo();
infoDlg.ShowDialog();
}
//===================================================================
// GetItemData
//
// Return item data of list box item
//
//===================================================================
public string GetItemData(int index)
{
return ItemData[index];
}
//===================================================================
// GetDMHandle
//
// Return device management handle
//
//===================================================================
public int GetDMHandle()
{
return m_hDMHandle;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -