?? currency calculatordlg.cpp
字號:
// Currency CalculatorDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Currency Calculator.h"
#include "Currency.h"
#include "Currency CalculatorDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CCurrencyCalculatorDlg dialog
CCurrencyCalculatorDlg::CCurrencyCalculatorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCurrencyCalculatorDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCurrencyCalculatorDlg)
m_curr_or_num = -1;
m_amount = _T("");
m_cur_sel = -1;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
last_op = none;
new_num_flag = false;
first_num.make_null();
intermediate_num.make_null();
result.make_null();
}
void CCurrencyCalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCurrencyCalculatorDlg)
DDX_Control(pDX, IDC_CURRENCY, m_radio1);
DDX_Control(pDX, IDC_LIST1, m_list1);
DDX_Radio(pDX, IDC_CURRENCY, m_curr_or_num);
DDX_Text(pDX, IDC_RESULT, m_amount);
DDX_LBIndex(pDX, IDC_LIST1, m_cur_sel);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCurrencyCalculatorDlg, CDialog)
//{{AFX_MSG_MAP(CCurrencyCalculatorDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_CURRENCY, OnCurrency)
ON_BN_CLICKED(IDC_DIVIDE, OnDivide)
ON_BN_CLICKED(IDC_EQUAL, OnEqual)
ON_BN_CLICKED(IDC_MULTIPLY, OnMultiply)
ON_BN_CLICKED(IDC_NUM0, OnNum0)
ON_BN_CLICKED(IDC_NUM1, OnNum1)
ON_BN_CLICKED(IDC_NUM2, OnNum2)
ON_BN_CLICKED(IDC_NUM3, OnNum3)
ON_BN_CLICKED(IDC_NUM4, OnNum4)
ON_BN_CLICKED(IDC_NUM5, OnNum5)
ON_BN_CLICKED(IDC_NUM6, OnNum6)
ON_BN_CLICKED(IDC_NUM7, OnNum7)
ON_BN_CLICKED(IDC_NUM8, OnNum8)
ON_BN_CLICKED(IDC_NUM9, OnNum9)
ON_BN_CLICKED(IDC_NUMBER, OnNumber)
ON_BN_CLICKED(IDC_POINT, OnPoint)
ON_BN_CLICKED(IDC_SUBTRACT, OnSubtract)
ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_BN_CLICKED(IDC_CLEARALL, OnClearall)
ON_BN_CLICKED(IDC_BACKSPACE, OnBackspace)
ON_BN_CLICKED(IDC_About, OnAbout)
ON_BN_CLICKED(IDC_EXIT, OnExit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCurrencyCalculatorDlg message handlers
BOOL CCurrencyCalculatorDlg::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
// set all strings in list box and set default country as initial selection
CString STRINGS[] = { "American Dollar", "Australian Dollar", "Pound Sterling",
"European Euro", "Indian Ruppee", "Japanese Yen", "Pakistani Ruppee",
"Saudi Riyal" };
CListBox *plist1 = (CListBox *)GetDlgItem( IDC_LIST1 );
for( int i=0; i<MAX_COUNTRIES; i++ )
plist1->AddString( STRINGS[i] );
m_cur_sel = DEFAULT_COUNTRY;
plist1->SetCurSel( m_cur_sel );
// set result field to 0
m_amount = "0 ";
m_amount += Currency::get_c_letter( DEFAULT_COUNTRY );
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
m_curr_or_num = IDC_CURRENCY;
CWnd::CheckRadioButton( IDC_CURRENCY, IDC_NUMBER, m_curr_or_num );
return TRUE; // return TRUE unless you set the focus to a control
}
void CCurrencyCalculatorDlg::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 CCurrencyCalculatorDlg::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 CCurrencyCalculatorDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCurrencyCalculatorDlg::OnCurrency()
{ // if not previously checked,
if( m_curr_or_num!=IDC_CURRENCY )
{ // check it and up-date result field with currency letter of selected country
m_curr_or_num = IDC_CURRENCY;
if( m_amount.Find( ' ' )==-1 )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
m_list1.EnableWindow(); // enable list box
}
}
void CCurrencyCalculatorDlg::OnNumber()
{ // if not previously checked,
if( m_curr_or_num!=IDC_NUMBER )
{ // check it and up-date result field by removing currency letter
m_curr_or_num = IDC_NUMBER;
m_amount = remove_c_letter();
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
m_list1.EnableWindow( false ); // disable list box
}
}
void CCurrencyCalculatorDlg::OnSelchangeList1()
{ // save previous selection and up-date currenct selection
int prev_sel = m_cur_sel;
m_cur_sel = m_list1.GetCurSel();
// up-date result field by converting it in currency of current selected country
m_amount = remove_c_letter();
double num = atof( m_amount );
if( num!=0 )
{
Currency temp( num, Currency::get_c_letter( COUNTRIES[prev_sel] ) );
temp = Currency::convert_currency( temp, temp.get_c_letter(),
Currency::get_c_letter( COUNTRIES[m_cur_sel] ) );
m_amount = temp.make_string();
}
else
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
}
void CCurrencyCalculatorDlg::OnNum0()
{ // remove currency letter if it is currency
if( m_amount.Find( ' ' )!=-1 )
m_amount = remove_c_letter();
if( new_num_flag )
{ // if new number flag is ON, write 0 and return
m_amount = '0';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
new_num_flag = false; // make flag OFF
return;
}
// new number flag is OFF, so proceed accordingly
if( m_amount.GetLength()>=MAX_LENGTH )
return; // return if already reached to maximum length
if( m_amount[0]=='0' && m_amount.Find('.')==-1 )
return; // return if first letter is zero and no point exists
m_amount += '0'; // add zero at end
if( m_curr_or_num==IDC_CURRENCY )
{ // add currency letter if it is currency
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
} // up-date result field
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
}
void CCurrencyCalculatorDlg::OnNum1()
{
if( m_amount.Find( ' ' )!=-1 )
m_amount = remove_c_letter();
if( new_num_flag )
{
m_amount = '1';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
new_num_flag = false;
return;
}
if( m_amount.GetLength()>=MAX_LENGTH )
return;
if( m_amount[0]=='0' && m_amount.Find('.')==-1 )
m_amount.Delete( 0, 1 );
m_amount += '1';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
}
void CCurrencyCalculatorDlg::OnNum2()
{
if( m_amount.Find( ' ' )!=-1 )
m_amount = remove_c_letter();
if( new_num_flag )
{
m_amount = '2';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
new_num_flag = false;
return;
}
if( m_amount.GetLength()>=MAX_LENGTH )
return;
if( m_amount[0]=='0' && m_amount.Find('.')==-1 )
m_amount.Delete( 0, 1 );
m_amount += '2';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
}
void CCurrencyCalculatorDlg::OnNum3()
{
if( m_amount.Find( ' ' )!=-1 )
m_amount = remove_c_letter();
if( new_num_flag )
{
m_amount = '3';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
new_num_flag = false;
return;
}
if( m_amount.GetLength()>=MAX_LENGTH )
return;
if( m_amount[0]=='0' && m_amount.Find('.')==-1 )
m_amount.Delete( 0, 1 );
m_amount += '3';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
}
void CCurrencyCalculatorDlg::OnNum4()
{
if( m_amount.Find( ' ' )!=-1 )
m_amount = remove_c_letter();
if( new_num_flag )
{
m_amount = '4';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
new_num_flag = false;
return;
}
if( m_amount.GetLength()>=MAX_LENGTH )
return;
if( m_amount[0]=='0' && m_amount.Find('.')==-1 )
m_amount.Delete( 0, 1 );
m_amount += '4';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
}
void CCurrencyCalculatorDlg::OnNum5()
{
if( m_amount.Find( ' ' )!=-1 )
m_amount = remove_c_letter();
if( new_num_flag )
{
m_amount = '5';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
new_num_flag = false;
return;
}
if( m_amount.GetLength()>=MAX_LENGTH )
return;
if( m_amount[0]=='0' && m_amount.Find('.')==-1 )
m_amount.Delete( 0, 1 );
m_amount += '5';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
}
void CCurrencyCalculatorDlg::OnNum6()
{
if( m_amount.Find( ' ' )!=-1 )
m_amount = remove_c_letter();
if( new_num_flag )
{
m_amount = '6';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
new_num_flag = false;
return;
}
if( m_amount.GetLength()>=MAX_LENGTH )
return;
if( m_amount[0]=='0' && m_amount.Find('.')==-1 )
m_amount.Delete( 0, 1 );
m_amount += '6';
if( m_curr_or_num==IDC_CURRENCY )
{
m_amount += ' ';
m_amount += Currency::get_c_letter( COUNTRIES[m_cur_sel] );
}
CWnd::SetDlgItemText( IDC_RESULT, m_amount );
}
void CCurrencyCalculatorDlg::OnNum7()
{
if( m_amount.Find( ' ' )!=-1 )
m_amount = remove_c_letter();
if( new_num_flag )
{
m_amount = '7';
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -