www.pudn.com > RPort.rar > RPortDlg.cpp


// RPortDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "RPort.h" 
#include "RPortDlg.h" 
#include "BCGRegistry.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
 
CArray	g_arrPortState; 
CCriticalSection	g_csPortState; 
 
///////////////////////////////////////////////////////////////////////////// 
// 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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CRPortDlg dialog 
 
CRPortDlg::CRPortDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CRPortDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CRPortDlg) 
	m_strDevice = _T(""); 
	m_strState = _T(""); 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
	m_pAboutDlg = NULL; 
} 
 
void CRPortDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CRPortDlg) 
	DDX_Control(pDX, IDC_LIST, m_listInfo); 
	DDX_Text(pDX, IDC_DEVICE, m_strDevice); 
	DDX_Text(pDX, IDC_STATE, m_strState); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CRPortDlg, CDialog) 
	//{{AFX_MSG_MAP(CRPortDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_LBN_SELCHANGE(IDC_LIST, OnSelchangeList) 
	ON_COMMAND(IDC_EXIT, OnExit) 
	ON_COMMAND(IDC_MONITOR, OnMonitor) 
	ON_COMMAND(IDC_ABOUT, OnAbout) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CRPortDlg message handlers 
 
BOOL CRPortDlg::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 
	CString strTilte; 
	strTilte.LoadString(IDS_TITLE); 
	SetWindowText(strTilte); 
 
	int nSize = GetPortCount(); 
 
	CString strItem; 
	g_arrPortState.SetSize(nSize); 
	for(int i= 0; i < nSize; i++) 
	{ 
		strItem.Format("%d.%s", i+1, (LPCTSTR)m_arrComName[i]); 
		m_listInfo.AddString(strItem); 
		g_arrPortState[i].bOpened = 0; 
		g_arrPortState[i].nPort = i + 1; 
	} 
 
	CPortThread* pThread = NULL; 
 
	for(i= 0; i < nSize; i++) 
	{ 
		pThread = (CPortThread*)AfxBeginThread( 
			RUNTIME_CLASS(CPortThread), 
			THREAD_PRIORITY_NORMAL, 
			0, 
			CREATE_SUSPENDED); 
 
		ASSERT(pThread); 
	 
		pThread->m_strPort = m_arrComName[i]; 
		pThread->m_nIndex = i; 
		pThread->m_hWnd = m_hWnd; 
		pThread->ResumeThread(); 
	} 
 
	m_TrayIcon.Create(NULL,RegisterWindowMessage("WM_TRAY_ICON_NOTIFY"), 
		strTilte,m_hIcon,IDR_MENU); 
	m_TrayIcon.SetMenuDefaultItem(0,TRUE);	//default menu item is show-monitor 
	m_TrayIcon.ShowIcon();					//hide icon 
	PostMessage(WM_COMMAND, IDC_MONITOR); 
 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CRPortDlg::OnSysCommand(UINT nID, LPARAM lParam) 
{ 
	if ((nID & 0xFFF0) == IDM_ABOUTBOX) 
	{ 
		OnAbout(); 
	} 
	else 
	{ 
		if(nID == SC_CLOSE || nID == SC_MINIMIZE) 
			ShowWindow(SW_HIDE); 
		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 CRPortDlg::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 CRPortDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CRPortDlg::OnOK()  
{ 
	ShowWindow(SW_HIDE); 
} 
 
void CRPortDlg::OnCancel()  
{ 
	ShowWindow(SW_HIDE); 
} 
 
int CRPortDlg::GetPortCount() 
{ 
	static int nCount = 0; 
 
	if(nCount) 
		return nCount; 
 
	CBCGRegistry reg(TRUE, TRUE); 
	reg.Open(_T("HARDWARE\\DEVICEMAP\\SERIALCOMM")); 
	CStringArray arr; 
	reg.ReadKeyValues(arr); 
 
	int nSize = arr.GetSize(); 
 
	m_arrDeviceName.SetSize(nSize); 
	m_arrComName.SetSize(nSize); 
	CString strComm; 
	 
	for(int i = 0 ; i < nSize; i++) 
	{ 
		reg.Read(arr[i], strComm); 
		m_arrDeviceName[i] = arr[i]; 
		m_arrComName[i] = strComm; 
	} 
 
	return	nSize; 
} 
 
 
void CRPortDlg::OnSelchangeList()  
{ 
	UpdateData(); 
	int nIndex = m_listInfo.GetCurSel(); 
	if(nIndex == LB_ERR) 
	{ 
		m_listInfo.SetCurSel(0); 
		nIndex = 0; 
	} 
 
	m_strDevice = m_arrDeviceName[nIndex]; 
	g_csPortState.Lock(); 
	m_strState = g_arrPortState[nIndex].GetInfo(); 
	g_csPortState.Unlock(); 
 
	UpdateData(FALSE); 
} 
 
BOOL CRPortDlg::OnWndMsg( UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult ) 
{ 
	if(message == WM_USER) 
	{ 
		OnSelchangeList(); 
		return	TRUE; 
	} 
	return CDialog::OnWndMsg(message, wParam, lParam, pResult); 
} 
 
void CRPortDlg::OnExit()  
{ 
	CString strTitle; 
	strTitle.LoadString(IDS_TITLE); 
	CString strMsg; 
	strMsg.Format("Exit %s server?", (LPCTSTR)strTitle); 
	if(IDYES == MessageBox(strMsg, NULL, MB_YESNO|MB_ICONQUESTION)) 
		CDialog::OnCancel(); 
} 
 
void CRPortDlg::OnMonitor()  
{ 
	if(IsWindowVisible()) 
		ShowWindow(SW_HIDE); 
	else 
	{ 
		ShowWindow(SW_SHOW); 
		SetForegroundWindow(); 
	} 
} 
 
void CRPortDlg::OnAbout()  
{ 
	if(m_pAboutDlg)			//no reentrance 
	{ 
		m_pAboutDlg->CenterWindow(); 
		m_pAboutDlg->SetForegroundWindow(); 
		return; 
	} 
	CAboutDlg dlgAbout; 
	m_pAboutDlg = &dlgAbout; 
	dlgAbout.DoModal(); 
	m_pAboutDlg = NULL; 
}