www.pudn.com > agsm2-1.2_src.zip > agsm2Dlg.cpp
// agsm2Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "agsm2.h"
#include "agsm2Dlg.h"
#include "ScListCSV.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()
/////////////////////////////////////////////////////////////////////////////
// CAgsm2Dlg dialog
CAgsm2Dlg::CAgsm2Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CAgsm2Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAgsm2Dlg)
m_CardStatus = _T("");
m_globalStatusStr = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pFindDialog = NULL;
m_bDone = FALSE;
m_hEventDead = CreateEvent(NULL, FALSE, FALSE, NULL);
}
CAgsm2Dlg::~CAgsm2Dlg()
{
if(m_pScLndPage)
delete m_pScLndPage;
}
void CAgsm2Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAgsm2Dlg)
DDX_Control(pDX, IDC_STATIC_STATUS, m_globalStatus);
DDX_Control(pDX, IDOK, m_readButton);
DDX_Control(pDX, IDC_BUTTON_WRITE, m_writeButton);
DDX_Control(pDX, IDC_PROGRESS_READ, m_RWProgress);
DDX_Control(pDX, IDC_COMBO_READERS, m_ComboReaders);
DDX_Text(pDX, IDC_STATIC_SMART, m_CardStatus);
DDX_Text(pDX, IDC_STATIC_STATUS, m_globalStatusStr);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAgsm2Dlg, CDialog)
//{{AFX_MSG_MAP(CAgsm2Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, OnButtonRead)
ON_BN_CLICKED(IDC_BUTTON_WRITE, OnButtonWrite)
ON_BN_CLICKED(IDC_BUTTON_IMPORT, OnButtonImport)
ON_BN_CLICKED(IDC_BUTTON_EXPORT, OnButtonExport)
ON_CBN_SELCHANGE(IDC_COMBO_READERS, OnSelchangeComboReaders)
ON_COMMAND(ID_MENUITEM_COPY, OnMenuitemCopy)
ON_COMMAND(ID_MENUITEM_PASTE, OnMenuitemPaste)
ON_COMMAND(ID_MENUITEM_EDIT, OnMenuitemEdit)
ON_COMMAND(ID_MENUITEM_DELETE, OnMenuitemDelete)
ON_COMMAND(ID_MENUITEM_CUT, OnMenuitemCut)
ON_BN_CLICKED(IDC_BUTTON_UP, OnButtonUp)
ON_BN_CLICKED(IDC_BUTTON_DOWN, OnButtonDown)
ON_BN_CLICKED(IDC_BUTTON_FIND, OnButtonFind)
ON_WM_CLOSE()
ON_CBN_DROPDOWN(IDC_COMBO_READERS, OnDropdownComboReaders)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
// TO Insert your own handlers here...
ON_COMMAND(ID_MENU_WRITE_ALL, OnButtonWrite)
ON_COMMAND(ID_MENU_WRITE_SELECTED, OnWriteSelected)
ON_CONTROL(1234,IDC_BUTTON_WRITE, OnUpdateMenuitemWrite)
ON_CONTROL(1234,IDOK, OnUpdateMenuitemRead)
ON_COMMAND(ID_MENU_READ_ALL, OnButtonRead)
ON_COMMAND(ID_MENU_READ_SELECTED, OnReadSelected)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAgsm2Dlg message handlers
BOOL CAgsm2Dlg::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
m_writeButton.AddMenuItem(ID_MENU_WRITE_ALL,(LPCSTR)IDS_STRING_WRITE_ALL,0);
m_writeButton.AddMenuItem(ID_MENU_WRITE_SELECTED,(LPCSTR)IDS_STRING_WRITE_SELECTED,MF_DISABLED|MF_GRAYED);
m_readButton.AddMenuItem(ID_MENU_READ_ALL,(LPCSTR)IDS_STRING_READ_ALL,0);
m_readButton.AddMenuItem(ID_MENU_READ_SELECTED,(LPCSTR)IDS_STRING_READ_SELECTED,MF_DISABLED|MF_GRAYED);
CreatePropSheet();
// UpdateComboReaders();
// UpdateCardStatus();
m_RWProgress.ShowWindow(SW_HIDE);
// m_globalStatus.ShowWindow(SW_HIDE);
// Load Accelerators
HINSTANCE hInst = AfxFindResourceHandle(MAKEINTRESOURCE(IDR_ACCELERATOR1), RT_ACCELERATOR);
m_hAccelTable = ::LoadAccelerators(hInst, MAKEINTRESOURCE(IDR_ACCELERATOR1));
//Create a thread tracking the reader and card
m_pTrackingThread = AfxBeginThread(TrackingReaderAndCardThreadProc, this);
return TRUE; // return TRUE unless you set the focus to a control
}
void CAgsm2Dlg::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 CAgsm2Dlg::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 CAgsm2Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CAgsm2Dlg::CreatePropSheet()
{
m_pScLndPage = new CScAdnPage(IDS_STRING_LND);
m_pScLndPage->SetFileId(0x6f44);
m_ScAdnPage.SetFileId(0x6f3a);
m_ScPropSheet.AddPage(&m_ScAdnPage);
m_ScPropSheet.AddPage(m_pScLndPage);
m_ScPropSheet.AddPage(&m_ScSmsPage);
m_ScPropSheet.AddPage(&m_ScCardInfo);
m_ScPropSheet.AddPage(&m_ScPinPage);
CRect rcSheet;
GetDlgItem( IDC_SC_PROPSHEET )->GetWindowRect( &rcSheet );
ScreenToClient( &rcSheet );
//http://www.codeguru.com/Cpp/controls/propertysheet/article.php/c583/
m_ScPropSheet.Create(this, WS_CHILD | WS_VISIBLE, 0);
m_ScPropSheet.ModifyStyleEx (0, WS_EX_CONTROLPARENT);
m_ScPropSheet.ModifyStyle( 0, WS_TABSTOP );
m_ScPropSheet.SetWindowPos(NULL, rcSheet.left, rcSheet.top,
rcSheet.Width(), rcSheet.Height(),
SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE );
m_ScPropSheet.ShowWindow(SW_NORMAL);
}
void CAgsm2Dlg::UpdateComboReaders()
{
int oldSel = m_ComboReaders.GetCurSel();
if(oldSel == CB_ERR )
oldSel = 0;
m_ComboReaders.ResetContent();
LONG nReaders = m_SCReader.ListReaders();
if(nReaders)
{
for(int i=0; iGetTextExtent(str);
if (sz.cx > dx)
dx = sz.cx;
}
m_ComboReaders.ReleaseDC(pDC);
// Set the width of the list box so that every item is completely visible.
m_ComboReaders.SetDroppedWidth(dx);
//OnSelchangeComboReaders();
}
void CAgsm2Dlg::OnButtonRead()
{
// TODO: Add your control notification handler code here
TRACE("OnButtonRead\n");
CScPropertyPage *activePage = (CScPropertyPage *)m_ScPropSheet.GetActivePage();
activePage->ReadRecord();
}
void CAgsm2Dlg::OnReadSelected()
{
// TODO: Add your control notification handler code here
TRACE("OnWriteAll\n");
CScPropertyPage *activePage = (CScPropertyPage *)m_ScPropSheet.GetActivePage();
activePage->ReadRecord(-2);
}
void CAgsm2Dlg::OnButtonWrite()
{
// TODO: Add your control notification handler code here
TRACE("OnWriteAll\n");
CScPropertyPage *activePage = (CScPropertyPage *)m_ScPropSheet.GetActivePage();
activePage->WriteRecord();
}
void CAgsm2Dlg::OnWriteSelected()
{
TRACE("OnWriteSelected\n");
CScPropertyPage *activePage = (CScPropertyPage *)m_ScPropSheet.GetActivePage();
activePage->WriteRecord(-2);
}
void CAgsm2Dlg::OnButtonImport()
{
// TODO: Add your control notification handler code here
CScPropertyPage *activePage = (CScPropertyPage *)m_ScPropSheet.GetActivePage();
CScListCtrl *listCtrl = activePage->GetListCtrl();
if(listCtrl)
{
CScListImportCSV dlg;
dlg.ImportScList(listCtrl);
dlg.DoModal();
}
}
void CAgsm2Dlg::OnButtonExport()
{
// TODO: Add your control notification handler code here
CScPropertyPage *activePage = (CScPropertyPage *)m_ScPropSheet.GetActivePage();
CScListCtrl *listCtrl = activePage->GetListCtrl();
if(listCtrl)
{
CScListExportCSV dlg;
dlg.ExportScList(listCtrl);
dlg.DoModal();
}
}
void CAgsm2Dlg::OnSelchangeComboReaders()
{
// TODO: Add your control notification handler code here
LONG lRet = m_SCReader.SetCurReader(m_ComboReaders.GetCurSel());
//wait for card power on
//Sleep(1000);
CButton *readButton = (CButton *)this->GetDlgItem(IDOK);
if(lRet == 0)
{
readButton->EnableWindow(TRUE);
m_writeButton.EnableWindow(TRUE);
//AfxMessageBox(_T("ÇëÊäÈëpinÂë:"));
CPinDialog dlg;
if(dlg.RequirePIN())
dlg.DoModal();
}else{
readButton->EnableWindow(FALSE);
m_writeButton.EnableWindow(FALSE);
}
}
void CAgsm2Dlg::OnMenuitemCopy()
{
// TODO: Add your command handler code here
TRACE("OnMenuitemCopy\n");
CScPropertyPage *activePage = (CScPropertyPage *)m_ScPropSheet.GetActivePage();
CScListCtrl *listCtrl = activePage->GetListCtrl();
if(listCtrl)
listCtrl->CopySelectedItems();
}
void CAgsm2Dlg::OnMenuitemPaste()
{
// TODO: Add your command handler code here
TRACE("OnMenuitemPaste\n");
CPropertyPage *activePage = m_ScPropSheet.GetActivePage();
if(activePage->IsKindOf(RUNTIME_CLASS(CScAdnPage)))
{
TRACE("CScAdnPage\n");
((CScAdnPage *)activePage)->Paste();
}else if(activePage->IsKindOf(RUNTIME_CLASS(CScSmsPage)))
{
TRACE("CScSmsPage\n");
((CScSmsPage *)activePage)->Paste();
}
}
void CAgsm2Dlg::OnMenuitemEdit()
{
// TODO: Add your command handler code here
TRACE("OnMenuitemEdit\n");
CPropertyPage *activePage = m_ScPropSheet.GetActivePage();
if(activePage->IsKindOf(RUNTIME_CLASS(CScAdnPage)))
{
TRACE("CScAdnPage\n");
((CScAdnPage *)activePage)->EditRecord();
}else if(activePage->IsKindOf(RUNTIME_CLASS(CScSmsPage)))
{
TRACE("CScSmsPage\n");
((CScSmsPage *)activePage)->EditRecord();
}
}
void CAgsm2Dlg::OnMenuitemDelete()
{
// TODO: Add your command handler code here
TRACE("OnMenuitemDelete\n");
CScPropertyPage *activePage = (CScPropertyPage *)m_ScPropSheet.GetActivePage();
CScListCtrl *listCtrl = activePage->GetListCtrl();
if(listCtrl)
listCtrl->Delete();
}
void CAgsm2Dlg::OnMenuitemCut()
{
// TODO: Add your command handler code here
OnMenuitemCopy();
OnMenuitemDelete();
}
void CAgsm2Dlg::OnButtonUp()
{
// TODO: Add your control notification handler code here
CScPropertyPage *activePage = (CScPropertyPage *)m_ScPropSheet.GetActivePage();
CScListCtrl *listCtrl = activePage->GetListCtrl();
if(listCtrl)
listCtrl->MoveUp();
}
void CAgsm2Dlg::OnButtonDown()
{
// TODO: Add your control notification handler code here
CScPropertyPage *activePage = (CScPropertyPage *)m_ScPropSheet.GetActivePage();
CScListCtrl *listCtrl = activePage->GetListCtrl();
if(listCtrl)
listCtrl->MoveDown();
}
void CAgsm2Dlg::OnButtonFind()
{
// TODO: Add your control notification handler code here
if(m_pFindDialog == NULL)
{
m_pFindDialog = new CFindDialog(this);
m_pFindDialog->Create(IDD_DIALOG_FIND,this);
m_pFindDialog->ShowWindow(SW_SHOW);
CRect rect,rect1;
GetWindowRect(&rect);
m_pFindDialog->GetWindowRect(&rect1);
m_pFindDialog->SetWindowPos(NULL,rect.left + ((rect.Width() - rect1.Width()) / 2),
rect.top + ((rect.Height() - rect1.Height()) / 2),
0,0,
SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
}else
m_pFindDialog->SetActiveWindow();
}
UINT CAgsm2Dlg::TrackingReaderAndCardThreadProc(LPVOID pParam)
{
CAgsm2Dlg *pAgsm2Dlg = (CAgsm2Dlg *)pParam;
CPcScCtrl &m_SCReader = pAgsm2Dlg->m_SCReader;
int i=0;
DWORD dwCurrentState = SCARD_STATE_UNAWARE;
pAgsm2Dlg->UpdateComboReaders();
while(!pAgsm2Dlg->m_bDone)
{
dwCurrentState = m_SCReader.TrackingCard(dwCurrentState);
TRACE("dwCurrentState=%x\n",dwCurrentState);
if(dwCurrentState & SCARD_STATE_PRESENT)
{
TRACE("SCARD_STATE_PRESENT\n");
if(!pAgsm2Dlg->m_bDone)
{
CString str((LPCSTR)IDS_STRING_PRESENT);
pAgsm2Dlg->UpdateCardStatus(str);
if((dwCurrentState & 0xffff) == 0x22//PRESENT
|| (dwCurrentState & 0xffff) == 0x422//UNPOWERD
)
//m_SCReader.ConnectCurCard();
pAgsm2Dlg->OnSelchangeComboReaders();
}
}else if(dwCurrentState & SCARD_STATE_EMPTY)
{
TRACE("SCARD_STATE_EMPTY\n");
if(!pAgsm2Dlg->m_bDone)
{
CString str((LPCSTR)IDS_STRING_ABSENT);
pAgsm2Dlg->UpdateCardStatus(str);
//m_SCReader.DisconnectCurCard();
pAgsm2Dlg->OnSelchangeComboReaders();
}
}else if(dwCurrentState & SCARD_STATE_UNKNOWN)
{
TRACE("Reader gone\n");
if(!pAgsm2Dlg->m_bDone)
{
CString str((LPCSTR)IDS_STRING_UNKNOWN);
pAgsm2Dlg->UpdateCardStatus(str);
pAgsm2Dlg->UpdateComboReaders();
Sleep(1000);
}
}
}
// SetEvent(pAgsm2Dlg->m_hEventDead);
// AfxEndThread(0);
return 0;
}
void CAgsm2Dlg::UpdateCardStatus(LPCTSTR lpsz)
{
CStatic *pStatic = (CStatic *)GetDlgItem(IDC_STATIC_SMART);
//CString s = m_SCReader.GetCardState();
if(pStatic)
pStatic->SetWindowText(lpsz);
}
void CAgsm2Dlg::OnClose()
{
// TODO: Add your message handler code here and/or call default
if (m_pTrackingThread != NULL)
{
m_bDone = TRUE;
m_pTrackingThread = NULL;
}
//WaitForSingleObject(m_hEventDead, INFINITE);
CDialog::OnClose();
}
void CAgsm2Dlg::OnDropdownComboReaders()
{
// TODO: Add your control notification handler code here
TRACE("OnDropdownComboReaders");
//UpdateComboReaders();
//UpdateCardStatus();
}
void CAgsm2Dlg::OnUpdateMenuitemWrite()
{
// TODO: Add your command update UI handler code here
TRACE("OnUpdateMenuitemWrite\n");
CScPropertyPage *activePage = (CScPropertyPage *)m_ScPropSheet.GetActivePage();
CScListCtrl *listCtrl = activePage->GetListCtrl();
if(listCtrl)
{
if(listCtrl->GetSelectedCount())
m_writeButton.EnableMenuItem(ID_MENU_WRITE_SELECTED,MF_ENABLED);
else
m_writeButton.EnableMenuItem(ID_MENU_WRITE_SELECTED,MF_GRAYED);
}
}
void CAgsm2Dlg::OnUpdateMenuitemRead()
{
// TODO: Add your command update UI handler code here
TRACE("OnUpdateMenuitemRead\n");
CScPropertyPage *activePage = (CScPropertyPage *)m_ScPropSheet.GetActivePage();
CScListCtrl *listCtrl = activePage->GetListCtrl();
if(listCtrl)
{
if(listCtrl->GetSelectedCount())
m_readButton.EnableMenuItem(ID_MENU_READ_SELECTED,MF_ENABLED);
else
m_readButton.EnableMenuItem(ID_MENU_READ_SELECTED,MF_GRAYED);
}
}