⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 irdaclientdlg.cpp

📁 WINCE下 红外遥控设备程序
💻 CPP
字号:
// IrDAClientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "IrDAClient.h"
#include "IrDAClientDlg.h"
#include "IrDASocket/IrDASocket.h"
#include <atlconv.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////

#define IRDA_SERVICENAME  "MySampleIrDAService"  // IrDA service name
#define LONG_TIMEOUT      10000                  // = 10 seconds
#define SHORT_TIMEOUT      5000                  // =  5 seconds

/////////////////////////////////////////////////////////////////////////////
// CIrDAClientDlg dialog

CIrDAClientDlg::CIrDAClientDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CIrDAClientDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CIrDAClientDlg)
	m_strMessage = _T("Hi Pocket PC developers!\r\n\r\nDaniel ;-)");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CIrDAClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CIrDAClientDlg)
	DDX_Text(pDX, IDC_EDIT1, m_strMessage);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CIrDAClientDlg, CDialog)
	//{{AFX_MSG_MAP(CIrDAClientDlg)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIrDAClientDlg message handlers

BOOL CIrDAClientDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here

	SHSipPreference(m_hWnd, SIP_UP); // show the currently active input panel window

	return TRUE;  // return TRUE unless you set the focus to a control
}

void CIrDAClientDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here

	UpdateData();
  
	// Send message to server
	UINT uiResult = Send(m_strMessage);
	if (uiResult)
	{
		// Display an error message
		CString strCaption, strText;
		strCaption.LoadString(IDS_ERROR_MESSAGE_SEND);
		strText.LoadString(uiResult);
		MessageBox(strText, strCaption, MB_ICONEXCLAMATION | MB_OK);
	}
}

UINT CIrDAClientDlg::Send(LPCTSTR lpszMessage)
{
	ASSERT(AfxIsValidString(lpszMessage));

	// Check the message length
	if (_tcslen(lpszMessage) <= 0)
		return IDS_ERROR_NO_MESSAGE;
	
	CWaitCursor wc;

	USES_CONVERSION;

	// Make sure the socket layer is active
	CWinSock winSock;

	// Open an IrDA socket
	CIrDASocket sd;
	if (!sd.Open())
		return IDS_ERROR_IRDA_NO_SOCKET;

	// Try several times to find a device
	DEVICELIST deviceList = { 0 };
	for (int nTry = 0; nTry < (LONG_TIMEOUT / 1000); nTry++)
	{
		// Try to find a device
		if (!sd.EnumDevices(&deviceList, sizeof(DEVICELIST)))
			return IDS_ERROR_IRDA_CANNOT_ENUMDEVS;

		// Abort if we have found a device
		if (deviceList.numDevice > 0)
			break;

		// Wait a second
		::Sleep(1000);
	}

	// Check if all retries have been finished
	if (deviceList.numDevice == 0)
		return IDS_ERROR_NO_DEVICES_FOUND;

	// Obtain the devicename (ANSI)
	LPCTSTR lpszDeviceName = A2CT(deviceList.Device[0].irdaDeviceName);

	// Ask the user if recepient is okay
	CString strCaption, strText;
	strCaption.LoadString(IDS_MESSAGE_SEND);
	strText.Format(IDS_MESSAGE_SENDTOHOST, lpszDeviceName);
	if (MessageBox(strText, strCaption, MB_ICONQUESTION | MB_YESNO) != IDYES)
		return 0;

	// Restore the wait cursor
	wc.Restore();

	// A device has been found, so try to connect
	if (!sd.Connect(IRDA_SERVICENAME, deviceList.Device[0].irdaDeviceID, LONG_TIMEOUT))
		return IDS_ERROR_IRDA_CANNOT_CONNECT;

	// Send the length of the message to the server
	u_long ulLength = htonl(_tcslen(lpszMessage));
    
	if (!sd.Send(ulLength, SHORT_TIMEOUT))
		return IDS_ERROR_IRDA_CANNOT_SEND;

	// Send the message to the server
	if (!sd.Send((LPCVOID) T2CA(lpszMessage), _tcslen(lpszMessage), SHORT_TIMEOUT))
		return IDS_ERROR_IRDA_CANNOT_SEND;

	// Return successful
	return 0;
}

void CIrDAClientDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here

	SHSipPreference(m_hWnd, SIP_DOWN); // hide the currently active input panel window
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -