简单控件的应用(一)—prj计算器

 

源程序下载地址:http://down.qiannao.com/space/file/luowei505050/prj-8ba1-7b97-5668.rar/.page

新建一个对话框程序,插入编辑框,下拉框,按钮,对输入数据进行响应,对数据进行增删除查改,对数据作加减乘除运算,截图:

主程序如下:

#include "stdafx.h"

#include

#include

#include

//#include

#include

#include "resource.h"

#include "MainDlg.h"

#define PI 3.1415926

 

BOOL WINAPI Main_Proc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

{

switch(uMsg)

{

HANDLE_MSG(hWnd, WM_INITDIALOG, Main_OnInitDialog);

HANDLE_MSG(hWnd, WM_COMMAND, Main_OnCommand);

        HANDLE_MSG(hWnd,WM_CLOSE, Main_OnClose);

}

 

return FALSE;

}

 

BOOL Main_OnInitDialog(HWND hwnd, HWND hwndFocus, LPARAM lParam)

{

    //HWND hwndcombo1=GetDlgItem(hwnd,IDC_COMBO1);

    //ComboBox_AddString(hwndcombo1,TEXT("学校"));

    //ComboBox_AddString(hwndcombo1,TEXT("商店"));

    //ComboBox_AddString(hwndcombo1,TEXT("家里"));

    //ComboBox_AddString(hwndcombo1,TEXT("路上"));

    HWND hwndcomboop=(GetDlgItem(hwnd,IDC_COMBOOP));

    ComboBox_InsertString(hwndcomboop,-1,TEXT("+"));

    ComboBox_InsertString(hwndcomboop,-1,TEXT("-"));

    ComboBox_InsertString(hwndcomboop,-1,TEXT("*"));

    ComboBox_InsertString(hwndcomboop,-1,TEXT("/"));

return TRUE;

}

//--------------------------------------------------------------------------

BOOL IsInt(char *str)

{

    TCHAR strtemp[32];

    int i1=atoi(str);

    itoa(i1,strtemp,10);

    if(strcmp(strtemp,str)==0)

    {

        return TRUE;

    }

    else

        return FALSE;

}

 

TCHAR *GetText(HWND hwnd,int id,TCHAR* str)

{

    //TCHAR str[256];

    GetDlgItemText(hwnd,id,str,sizeof(str)+29);

    return str;

}

//==========================================================================

void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)

{

switch(id)

{

case IDC_OK:

        {

            //char name[256];

            TCHAR name[256];

            GetDlgItemText(hwnd,IDC_EDITNAME,name,sizeof(name));

            MessageBox(hwnd,name,name,MB_OK);

        }

break;

 

        case IDC_OK2:

        {

            char name[256];

            TCHAR msg[256];

            GetDlgItemText(hwnd,IDC_EDITNAME,name,sizeof(name));

            //sprintf(msg,"%s你好!",name);

            wsprintf(msg,"%s你好!",name);

            MessageBox(hwnd,msg,msg,MB_OK);

        }

        break;

case IDC_SET:

            {

                SetDlgItemText(hwnd,IDC_EDITNAME2,"新年好!!");

            }

break;

//=============================================================================

case IDC_ADD:

        {

            TCHAR str1[32];

            TCHAR str2[32];

            TCHAR str11[32];

            TCHAR str22[32];

            TCHAR str3[32];

            GetDlgItemText(hwnd,IDC_EDIT1,str1,sizeof(str1));

            GetDlgItemText(hwnd,IDC_EDIT2,str2,sizeof(str2));

            int i1=atoi(str1);

            int i2=atoi(str2);

            itoa(i1,str11,10);

            itoa(i2,str22,10);

            if(strcmp(str1,str11)!=0||strcmp(str2,str22)!=0)

            {

                MessageBox(hwnd,TEXT("请输入整数!"),TEXT("提示"),MB_OK|MB_ICONHAND);

                break;

            }

            int i3=i1+i2;

            itoa(i3,str3,10);

            //wsprintf(str3,"%i",i3);

            SetDlgItemText(hwnd,IDC_EDIT3,str3);

        }

        break;

case IDC_SUB:

        {

            TCHAR str1[32];

            TCHAR str2[32];

            TCHAR str3[32];

            GetDlgItemText(hwnd,IDC_EDIT4,str1,sizeof(str1));

            GetDlgItemText(hwnd,IDC_EDIT5,str2,sizeof(str2));

            int i1=atoi(str1);

            int i2=atoi(str2);

            if(!IsInt(str1))

            {

                MessageBox(hwnd,TEXT("第一个输入框中的数不是整数!"),TEXT("提示"),MB_OK|MB_ICONHAND);

                break;

            }

            if(!IsInt(str2))

            {

                MessageBox(hwnd,TEXT("第二个输入框中的数不是整数!"),TEXT("提示"),MB_OK|MB_ICONHAND);

                break;

            }

            int i3=i1-i2;

            itoa(i3,str3,10);

            //wsprintf(str3,"%i",i3);

            SetDlgItemText(hwnd,IDC_EDIT6,str3);

        }

        break;

 

        case IDC_NEW:

        {

            //if() //有重复的

 

            HWND hwndcombo1=GetDlgItem(hwnd,IDC_COMBO1);

            ComboBox_AddString(hwndcombo1,TEXT("学校"));

            ComboBox_AddString(hwndcombo1,TEXT("商店"));

            ComboBox_AddString(hwndcombo1,TEXT("家里"));

            ComboBox_AddString(hwndcombo1,TEXT("路上"));

        }

        break;

 

        //void Main_OnCommand2(HWND hwndcombo1, int id, HWND hwndCtl, UINT codeNotify)

        //{

        case IDC_COMBOBACK:

        {

            HWND hwndcombo1=GetDlgItem(hwnd,IDC_COMBO1);

            int cursel=ComboBox_GetCurSel(hwndcombo1);

            switch(cursel)

            {

                case 3:

                {

                    MessageBox(hwnd,TEXT("你选择的是 学校"),TEXT("回复"),MB_OK);

                }break;

                case 2:

                {

                    MessageBox(hwnd,TEXT("你选择的是 商店"),TEXT("回复"),MB_OK);

                }break;

                case 0:

                {

                    MessageBox(hwnd,TEXT("你选择的是 家里"),TEXT("回复"),MB_OK);

                }break;

                case 1:

                {

                    MessageBox(hwnd,TEXT("你选择的是 路上"),TEXT("回复"),MB_OK);

                }break;

 

            }

        }

        //}

        break;

 

        case IDC_COMBODELETE:

        {

            HWND hwndcombo1=GetDlgItem(hwnd,IDC_COMBO1);

            ComboBox_DeleteString(hwndcombo1,3);

            ComboBox_DeleteString(hwndcombo1,2);

            ComboBox_DeleteString(hwndcombo1,1);

            ComboBox_DeleteString(hwndcombo1,0);

        }

        break;

 

        case IDC_COMBOCOUNT:

        {

            HWND hwndcombo1=GetDlgItem(hwnd,IDC_COMBO1);

            int count=ComboBox_GetCount(hwndcombo1);

            TCHAR msg[256];

            wsprintf(msg,"这个下拉列表中一共有 %d 个下拉选项",count);

            MessageBox(hwnd,msg,TEXT("统计下拉项数"),MB_OK);

        }break;

 

        case IDC_COMBOSETCURSEL:

        {

            HWND hwndcombo1=GetDlgItem(hwnd,IDC_COMBO1);

            ComboBox_SetCurSel(hwndcombo1,2);

            int se=ComboBox_GetCount(hwndcombo1);

            if(!se)

            {

                MessageBox(hwnd,TEXT("列表中没有选项,无法进行选择!"),TEXT("提示"),MB_OK);

            }

            else

            MessageBox(hwnd,TEXT("你选择了商店"),TEXT("选择"),MB_OK);

        }break;

 

        case IDC_GETLBTEXT:

        {

            HWND hwndcombo1=GetDlgItem(hwnd,IDC_COMBO1);

            TCHAR str[32];

            ComboBox_GetLBText(hwndcombo1,1,str);

            int se=ComboBox_GetCount(hwndcombo1);

            if(!se)

            {

                MessageBox(hwnd,TEXT("列表中没有选项,无法进行选择!"),TEXT("提示"),MB_OK);

            }

            else

                MessageBox(hwnd,TEXT(str),TEXT("操作结果"),MB_OK);

        }break;

        case IDC_COMBOADDITEM:

        {

            HWND hwndcombo1=GetDlgItem(hwnd,IDC_COMBO1);

            TCHAR str1[32];

            TCHAR *str2;

            str2=GetText(hwnd,IDC_COMBOTEXT,str1);

            ComboBox_AddString(hwndcombo1,str2);

        }break;

//=========================================================

        case IDC_OP:

        {

            TCHAR str1[32];

            TCHAR str2[32];

            TCHAR str3[32];

            GetDlgItemText(hwnd,IDC_INPUT1,str1,sizeof(str1));

            GetDlgItemText(hwnd,IDC_INPUT2,str2,sizeof(str2));

            if(!IsInt(str1))

            {MessageBox(hwnd,TEXT("第一个输入框中输入的不是整数,不符合要求"),TEXT("提示"),MB_OK);break;}

            if(!IsInt(str2))

            {MessageBox(hwnd,TEXT("第二个输入框中输入的不是整数,不符合要求"),TEXT("提示"),MB_OK);break;}

            

            int i1=atoi(str1);

            int i2=atoi(str2);

            int i3=0;

            HWND hwndcomboop=GetDlgItem(hwnd,IDC_COMBOOP);

            int curselIndex=ComboBox_GetCurSel(hwndcomboop);

            switch(curselIndex)

            {

            case 0:

                {

                    i3=i1+i2;

                }break;

            case 1:

                {

                    i3=i1-i2;

                }break;

            case 2:

                {

                    i3=i1*i2;

                }break;

            case 3:

                {

                    i3=i1/i2;

                }break;

            default:

                {

                    MessageBox(hwnd,TEXT("请选择运算符号"),TEXT("提示"),MB_OK);

                }break;

            }

            itoa(i3,str3,10);

            SetDlgItemText(hwnd,IDC_OUTPUT,str3);

 

        }break;

//==============================================================================

        case IDC_CIRCLE:

        {

            TCHAR str1[32];

            TCHAR str2[32];

            GetDlgItemText(hwnd,IDC_R,str1,sizeof(str1));

            float i1=atof(str1);

            long float i2=PI*i1*i1;

            ltoa(i2,str2,10);

            //wsprintf(str2,"%lf",i2);

            SetDlgItemText(hwnd,IDC_A,str2);

        }

        break;

        case IDC_CONVERT:

        {

            TCHAR str1[32];

            TCHAR str2[32];

            GetDlgItemText(hwnd,IDC_10,str1,sizeof(str1));

            int i1=atoi(str1);

            itoa(i1,str2,2);

            //wsprintf(str2,"%lf",i2);

            SetDlgItemText(hwnd,IDC_2,str2);

        }

        break;

default:

        break;

}

}

 

void Main_OnClose(HWND hwnd)

{

EndDialog(hwnd, 0);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

罗哥分享

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值