Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / Visual Basic
Article

COM Calculator

Rate me:
Please Sign up or sign in to vote.
1.12/5 (18 votes)
31 Jul 20042 min read 42.4K   1.6K   18   3
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 :

VC++
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

VC++
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 :

VC++
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 :

VC++
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 :

VC++
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


Written By
Web Developer
India India
I am baladevan programmer in VC++ ATL COM for Software Company based in Trichy, India.
I am B.E (Comp Sci.)

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey28-Feb-12 18:08
professionalManoj Kumar Choubey28-Feb-12 18:08 
GeneralI don't see the images on this site Pin
Hoornet9328-Jul-04 22:31
Hoornet9328-Jul-04 22:31 
GeneralDejavu Pin
Vadim Tabakman11-Jul-04 11:24
Vadim Tabakman11-Jul-04 11:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.