Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

COM Calculator

0.00/5 (No votes)
31 Jul 2004 1  
This simple program, which I develop for my understanding. This may help you for understanding the com coding and using it VB

IMPORTANT

This article holds information about how to write Com program in VC++ and using it VB. Know the importance of building COM components, a program written in one language should be usable through another. That was an elusive dream in a not too distant part. Then Microsoft unveiled COM. And the once elusive dream started becoming a reality. COM revolutionizes the way software is built. COM is powerful and if you want to remain on the cutting edge of technology, COM is the Way to go. This simple program, This may help you for understanding the COM coding and using it in VB

Introduction

To make it easy programming in COM. Just go through Inside COM and take a look of this program.

The component which I had designed for sample calculator in VC++. In which we can able to do the simple calculation, I had include Binary and Decimal number conversion.

Source code Guidelines

After creating to the ATL object in COM, you insert ATL Dialog and design it. Add the Clicked event to the Button and write the code part of the program.

Coding for Bin numbers :

LRESULT OnClickedButton_Bin(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
int r[10]={0},i=0,tot=0; 
int ip=GetDlgItemInt(IDC_EDIT1);

do
{
r[i++]=ip%2;
ip=ip/2;
tot++;
}while(ip>=2);
r[i]=ip;

int n=0;
for(int x=tot;x>=0;x--)
{
n=(n*10)+r[x];
}

SetDlgItemInt(IDC_EDIT1,n);
return 0;
}

Sample coding for the display number One

LRESULT OnClickedButton1(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
int n1=GetDlgItemInt(IDC_EDIT1);
    n1=(n1*10)+1;
SetDlgItemInt(IDC_EDIT1,n1);
return 0;
}

Coding for after pressing the equal button :

LRESULT OnClickedButton_Eqa(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
int n2=GetDlgItemInt(IDC_EDIT1);
switch(opt)
{
    case '+':
        r=n1+n2;
        break;
    case '-':
        r=n1-n2;
        break;
    case '*':
        r=n1*n2;
        break;
    case '/':
        r=n1/n2;
        break;
    case '^':
        r=pow(n1,n2);
        break;
    default:
        SetDlgItemInt(IDC_EDIT1,0);
 } 
SetDlgItemInt(IDC_EDIT1,r);
return 0;
}

Coding for 1/X calculation :

LRESULT OnClickedButton_OneByX(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  {
    n1=GetDlgItemInt(IDC_EDIT1);
    s.Format("%f",(1.0/n1));
    SetDlgItemText(IDC_EDIT1,s);
    return 0;
    }

Coding for Dec number :

LRESULT OnClickedButton_Dec(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
char bin[100];
int dec=0,temp,cont=0;

n1=GetDlgItemInt(IDC_EDIT1);
s.Format("%d",n1);
strcpy(bin,s); 

int totlen=strlen(bin)-1;
 while(totlen>=0)
{
if(bin[totlen]=='0')
     temp=pow(2,cont)*0;
else if(bin[totlen]=='1') 
    temp=pow(2,cont)*1;

dec=dec+temp;
totlen--;
cont++;
}
SetDlgItemInt(IDC_EDIT1,dec);
return 0;
}

After completing the coding part just Build the program. The component is created. After that open the VB insert completing in the program where your need.

1st Step in VB

2nd Step in VB

Without any coding part for calculator your can able to the proper output in the form of VB program.

This is first step which I kept in COM. which lead me to learn more in this subject. So keep trying until you succeed in COM

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here