This is my first windows app. I basically have my whole program written except for fixing a few lil problems and writing the code for 2 of my buttons. Could someone help me with the "Hex to Decimal" button code? or give some kind of example? my professor helped with the add button and i did the others, but i cant figure out this one :(
// hexcalcDlg.cpp : implementation file
//
#include "stdafx.h"
#include "hexcalc.h"
#include "hexcalcDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CHexcalcDlg dialog
CHexcalcDlg::CHexcalcDlg(CWnd* pParent /*=NULL*/)
: CDialog(CHexcalcDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CHexcalcDlg)
m_op1 = _T("");
m_op2 = _T("");
m_result = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CHexcalcDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHexcalcDlg)
DDX_Text(pDX, IDC_OP1, m_op1);
DDV_MaxChars(pDX, m_op1, 10);
DDX_Text(pDX, IDC_OP2, m_op2);
DDV_MaxChars(pDX, m_op2, 10);
DDX_Text(pDX, IDC_RESULT, m_result);
DDV_MaxChars(pDX, m_result, 10);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CHexcalcDlg, CDialog)
//{{AFX_MSG_MAP(CHexcalcDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_BN_CLICKED(IDC_DECTOHEX, OnDectohex)
ON_BN_CLICKED(IDC_DIVIDE, OnDivide)
ON_BN_CLICKED(IDC_HEXTODEC, OnHextodec)
ON_BN_CLICKED(IDC_MULTIPLY, OnMultiply)
ON_BN_CLICKED(IDC_SUBTRACT, OnSubtract)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHexcalcDlg message handlers
BOOL CHexcalcDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CHexcalcDlg::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 CHexcalcDlg::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 CHexcalcDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CHexcalcDlg::OnAdd()
{
// TODO: Add your control notification handler code here
GetDlgItemText(IDC_OP1, m_op1);
GetDlgItemText(IDC_OP2, m_op2);
int op1 = atoi((LPCTSTR) m_op1);
int op2 = atoi((LPCTSTR) m_op2);
int result = op1 + op2;
m_result.Format("%d", result);
SetDlgItemText(IDC_RESULT, m_result);
}
void CHexcalcDlg::OnClear()
{
// TODO: Add your control notification handler code here
m_op1 = _T("");
SetDlgItemText(IDC_OP1, m_op1);
m_op2 = _T("");
SetDlgItemText(IDC_OP2, m_op2);
m_result = _T("");
SetDlgItemText(IDC_RESULT, m_result);
}
void CHexcalcDlg::OnDectohex()
{
// TODO: Add your control notification handler code here
}
void CHexcalcDlg::OnDivide()
{
// TODO: Add your control notification handler code here
GetDlgItemText(IDC_OP1, m_op1);
GetDlgItemText(IDC_OP2, m_op2);
int op1 = atoi((LPCTSTR) m_op1);
int op2 = atoi((LPCTSTR) m_op2);
if(op2 == 0)
{
AfxMessageBox("operand 2 is zero!");
m_result = "unknown";
}
else
{
int result = op1 / op2;
m_result.Format("%d", result);
}
SetDlgItemText(IDC_RESULT, m_result);
}
void CHexcalcDlg::OnHextodec()
{
// TODO: Add your control notification handler code here
}
void CHexcalcDlg::OnMultiply()
{
// TODO: Add your control notification handler code here
GetDlgItemText(IDC_OP1, m_op1);
GetDlgItemText(IDC_OP2, m_op2);
int op1 = atoi((LPCTSTR) m_op1);
int op2 = atoi((LPCTSTR) m_op2);
int result = op1 * op2;
m_result.Format("%d", result);
SetDlgItemText(IDC_RESULT, m_result);
}
void CHexcalcDlg::OnSubtract()
{
// TODO: Add your control notification handler code here
GetDlgItemText(IDC_OP1, m_op1);
GetDlgItemText(IDC_OP2, m_op2);
int op1 = atoi((LPCTSTR) m_op1);
int op2 = atoi((LPCTSTR) m_op2);
int result = op1 - op2;
m_result.Format("%d", result);
SetDlgItemText(IDC_RESULT, m_result);
}
void CHexcalcDlg::ConvertDecToHex(CString& str_out, int number_in)
{
int index = 4;
str_out = " ";
char chr;
while(number_in > 0);
{
int remainder = number_in % 16;
number_in /= 16;
switch(remainder)
{
case 0:
chr = '0';
break;
case 1:
chr = '1';
break;
case 2:
chr = '2';
break;
case 3:
chr = '3';
break;
case 4:
chr = '4';
break;
case 5:
chr = '5';
break;
case 6:
chr = '6';
break;
case 7:
chr = '7';
break;
case 8:
chr = '8';
break;
case 9:
chr = '9';
break;
case 10:
chr = 'A';
break;
case 11:
chr = 'B';
break;
case 12:
chr = 'C';
break;
case 13:
chr = 'D';
break;
case 14:
chr = 'E';
break;
case 15:
chr = 'F';
break;
default:
AfxMessageBox("Invalid number");
}
str_out.SetAt(index, chr);
index --;
}
}
int CHexcalcDlg::ConvertHexToDec(CString str)
{
int value;
int number = 0;
int col = 1;
int length = str.GetLength();
for (int i = 0; i < length; i++)
{
char chr = str[length - 1 -i];
switch(chr)
{
case '0':
value = 0;
break;
case '1':
value = 1;
break;
case '2':
value = 2;
break;
case '3':
value = 3;
break;
case '4':
value = 4;
break;
case '5':
value = 5;
break;
case '6':
value = 6;
break;
case '7':
value = 7;
break;
case '8':
value = 8;
break;
case '9':
value = 9;
break;
case 'A':
case 'a':
value = 10;
break;
case 'B':
case 'b':
value = 11;
break;
case 'C':
case 'c':
value = 12;
break;
case 'D':
case 'd':
value = 13;
break;
case 'E':
case 'e':
value = 14;
break;
case 'F':
case 'f':
value = 15;
break;
default:
AfxMessageBox("Invalid character");
}
number += value * col;
col *= 16;
}
return number;
}