www.pudn.com > LanyaTest.rar > LanyaTestDlg.cpp, change:2007-07-16,size:9953b


// LanyaTestDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "LanyaTest.h" 
#include "LanyaTestDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CAboutDlg dialog used for App About 
 
class CAboutDlg : public CDialog 
{ 
public: 
	CAboutDlg(); 
 
// Dialog Data 
	//{{AFX_DATA(CAboutDlg) 
	enum { IDD = IDD_ABOUTBOX }; 
	//}}AFX_DATA 
 
	// ClassWizard generated virtual function overrides 
	//{{AFX_VIRTUAL(CAboutDlg) 
	protected: 
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support 
	//}}AFX_VIRTUAL 
 
// Implementation 
protected: 
	//{{AFX_MSG(CAboutDlg) 
	//}}AFX_MSG 
	DECLARE_MESSAGE_MAP() 
}; 
 
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) 
{ 
	//{{AFX_DATA_INIT(CAboutDlg) 
	//}}AFX_DATA_INIT 
} 
 
void CAboutDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CAboutDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) 
	//{{AFX_MSG_MAP(CAboutDlg) 
		// No message handlers 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CLanyaTestDlg dialog 
 
CLanyaTestDlg::CLanyaTestDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CLanyaTestDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CLanyaTestDlg) 
	m_message = _T(""); 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CLanyaTestDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CLanyaTestDlg) 
	DDX_Control(pDX, IDC_LIST, m_list); 
	DDX_Text(pDX, IDC_MESSAGE, m_message); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CLanyaTestDlg, CDialog) 
	//{{AFX_MSG_MAP(CLanyaTestDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_WM_TIMER() 
	ON_BN_CLICKED(IDConnect, OnConnect) 
	ON_BN_CLICKED(IDDisConnect, OnDisConnect) 
	ON_BN_CLICKED(IDSearch, OnSearch) 
	ON_BN_CLICKED(IDSearchName, OnSearchName) 
	ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST, OnColumnclickList) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CLanyaTestDlg message handlers 
 
BOOL CLanyaTestDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	// Add "About..." menu item to system menu. 
 
	// IDM_ABOUTBOX must be in the system command range. 
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 
	ASSERT(IDM_ABOUTBOX  0xF000); 
 
	CMenu* pSysMenu = GetSystemMenu(FALSE); 
	if (pSysMenu != NULL) 
	{ 
		CString strAboutMenu; 
		strAboutMenu.LoadString(IDS_ABOUTBOX); 
		if (!strAboutMenu.IsEmpty()) 
		{ 
			pSysMenu->AppendMenu(MF_SEPARATOR); 
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 
		} 
	} 
 
	// Set the icon for this dialog.  The framework does this automatically 
	//  when the application's main window is not a dialog 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization here 
	SetTimer(101,100,0); 
	ListView_SetExtendedListViewStyle 
		(m_list.m_hWnd, //LVS_EX_CHECKBOXES| //使用检查框 
		 LVS_EX_FULLROWSELECT //选择整行 
		|LVS_EX_GRIDLINES //在REPORT中画出分隔线  
		//|LVS_EX_HEADERDRAGDROP|LVS_REPORT //LVS_REPORT时可以利用drag-and-drop重新排序  
		//|LVS_EX_SUBITEMIMAGES //允许在子项中显示image  
		//|LVS_EX_TRACKSELECT //当鼠标指到某一项时便自动选择该项 
		);	// TODO: Add extra initialization here 
    LV_COLUMN lvcol; 
	lvcol.mask=LVCF_FMT|LVCF_SUBITEM|LVCF_TEXT|LVCF_WIDTH; 
    lvcol.fmt=LVCFMT_CENTER;//居中 
 
//	CDoorDoc *pdoc=get_doc(); 
	int i=0; 
	lvcol.pszText="序号"; 
    lvcol.iSubItem=i; 
    lvcol.cx=40; 
    m_list.InsertColumn(i++,&lvcol); 
	lvcol.pszText="设备名称"; 
    lvcol.iSubItem=i; 
    lvcol.cx=80; 
    m_list.InsertColumn(i++,&lvcol); 
	lvcol.pszText="蓝牙地址"; 
    lvcol.iSubItem=i; 
    lvcol.cx=100; 
    m_list.InsertColumn(i++,&lvcol); 
	lvcol.pszText="设备地址"; 
    lvcol.iSubItem=i; 
    lvcol.cx=100; 
    m_list.InsertColumn(i++,&lvcol); 
	lvcol.pszText="成功次数"; 
    lvcol.iSubItem=i; 
    lvcol.cx=80; 
    m_list.InsertColumn(i++,&lvcol); 
 
	StartMornitorBsol(); 
	g_hForceExit = CreateEvent(NULL, FALSE, FALSE, NULL); 
 
	SDK_Init(); 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CLanyaTestDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	} 
	else 
	{ 
		CDialog::OnSysCommand(nID, lParam); 
	} 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CLanyaTestDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CLanyaTestDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CLanyaTestDlg::OnTimer(UINT nIDEvent)  
{ 
	// TODO: Add your message handler code here and/or call default 
	if(m_list.GetItemCount()!=m_list_ptr.GetSize()) 
		Redraw(); 
	CDialog::OnTimer(nIDEvent); 
} 
void CLanyaTestDlg::Redraw() 
{ 
	struct list_struct *plist; 
	LV_ITEM lvitem; 
	lvitem.mask=LVIF_TEXT; 
	int index,iPos; 
	char txt[20]; 
	index=m_list_ptr.GetSize(); 
	m_list.SetRedraw(FALSE); 
	m_list.DeleteAllItems(); 
	for(int i=0;i<index;i++) 
	{ 
		plist=(struct list_struct*)m_list_ptr.GetAt(i); 
		lvitem.iItem=0; 
		lvitem.iSubItem=0; 
		sprintf(txt,"%03d",i); 
		lvitem.pszText=txt; 
		iPos=m_list.InsertItem(&lvitem);//返回表项插入后的索引号 
		lvitem.iItem=iPos; 
		 
		lvitem.iSubItem=1; 
		lvitem.pszText=plist->name; 
		m_list.SetItem(&lvitem); 
 
		lvitem.iSubItem=2; 
		sprintf(txt,"%02x%02x%02x%02x%02x%02x",plist->address[5],plist->address[4],plist->address[3],plist->address[2],plist->address[1],plist->address[0]); 
		lvitem.pszText=txt; 
		m_list.SetItem(&lvitem); 
 
		lvitem.iSubItem=3; 
		lvitem.pszText=plist->kzq_add; 
		m_list.SetItem(&lvitem); 
 
		lvitem.iSubItem=4; 
		lvitem.pszText=plist->times; 
		m_list.SetItem(&lvitem); 
	} 
	m_list.SetRedraw(TRUE); 
} 
BOOL CLanyaTestDlg::Connect(int num) 
{ 
	struct list_struct *plist; 
	plist=(struct list_struct *)m_list_ptr.GetAt(num); 
	memcpy(g_targetBdAddr,plist->address,6); 
	BLUETOOTH_DEVICE_INFO devInfo={0}; 
	devInfo.dwSize = sizeof(BLUETOOTH_DEVICE_INFO); 
	memcpy(devInfo.address, g_targetBdAddr, 6); 
	 
	GENERAL_SERVICE_INFO pClsidList[MAX_SERVICE_COUNT]={0}; 
	DWORD ClsidListLen = MAX_SERVICE_COUNT*sizeof(GENERAL_SERVICE_INFO); 
	BOOL bBrowseAllService = TRUE; 
	GENERAL_SERVICE_INFO g_svcList={0}; 
	DWORD dwResult; 
	BOOL rn=0; 
	 
	dwResult = BT_BrowseServices(&devInfo, bBrowseAllService, &ClsidListLen, pClsidList); 
	g_svcList.wServiceClassUuid16 = pClsidList[0].wServiceClassUuid16; 
	g_svcList.dwServiceHandle = pClsidList[0].dwServiceHandle; 
	g_svcList.dwSize = pClsidList[0].dwSize; 
	BYTE abyConnParam[512]; 
	DWORD dwParamLen = 512; 
	if(ConfigConnectParams(g_svcList.wServiceClassUuid16,abyConnParam,&dwParamLen)) 
	{ 
		DWORD dwHandle; 
		devInfo.classOfDevice[0]=0; 
		devInfo.classOfDevice[1]=0; 
		devInfo.classOfDevice[2]=0; 
		devInfo.szName[0]=0; 
		devInfo.bPaired=0; 
		dwResult = BT_ConnectService(&devInfo, &g_svcList, dwParamLen==0 ? NULL : abyConnParam, &dwHandle); 
		if(dwResult==BTSTATUS_SUCCESS) 
			rn=1; 
	} 
	return rn; 
} 
void CLanyaTestDlg::OnConnect()  
{ 
	// TODO: Add your control notification handler code here 
	char txt[50]; 
	int nIndex = m_list.GetNextItem(-1, LVNI_ALL | LVNI_SELECTED); 
	if(nIndex>=0) 
	{ 
		m_list.GetItemText(nIndex,0,txt,50); 
		int num=atoi(txt); 
		if(num<m_list_ptr.GetSize()) 
		{ 
			DisplayMessage("开始连接..."); 
			if(Connect(num)) 
				DisplayMessage("连接成功!"); 
			else 
				DisplayMessage("连接失败!"); 
			 
		} 
	} 
} 
void CLanyaTestDlg::DisplayMessage(char *msg) 
{ 
	m_message=msg; 
	UpdateData(0); 
} 
void CLanyaTestDlg::OnDisConnect()  
{ 
	// TODO: Add your control notification handler code here 
	DisplayMessage("开始取消连接..."); 
	if(SDK_BtDisConnectService()) 
		DisplayMessage("取消连接成功!"); 
	else 
		DisplayMessage("取消连接失败!"); 
} 
 
void CLanyaTestDlg::OnSearch()  
{ 
	// TODO: Add your control notification handler code here 
	DisplayMessage("正在搜索蓝牙设备..."); 
	SDK_BtInquiry(); 
	DisplayMessage("蓝牙设备搜索完成!"); 
} 
 
void CLanyaTestDlg::OnSearchName()  
{ 
	// TODO: Add your control notification handler code here 
	struct list_struct *plist; 
	for(m_list_num=0;m_list_num<m_list_ptr.GetSize();m_list_num++) 
	{ 
		plist=(struct list_struct *)m_list_ptr.GetAt(m_list_num); 
		if(plist->name[0]==0) 
		SDK_BtGetRemoteInfo(); 
	} 
} 
 
void CLanyaTestDlg::OnColumnclickList(NMHDR* pNMHDR, LRESULT* pResult)  
{ 
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR; 
	// TODO: Add your control notification handler code here 
	if( pNMListView->iSubItem == m_list.m_nSortedCol ) 
		m_list.m_fAsc = !m_list.m_fAsc; 
	else 
	{ 
		m_list.m_fAsc = TRUE; 
		m_list.m_nSortedCol = pNMListView->iSubItem; 
	} 
	int n=m_list.GetItemCount();   //得到一共的行数 
	for(int i=0;i<n;i++) 
		m_list.SetItemData(i,i); 
	m_list.SortItems( ListCompare, (DWORD)&m_list );         
	for( i=0;i<n;i++) 
		m_list.SetItemData(i,i); 
	*pResult = 0; 
}