计算器编辑

来源:互联网 发布:游戏编程入门第四版 编辑:程序博客网 时间:2024/06/14 02:05

#include <windows.h>#include "resource.h"int num_a;int num_b;int result;char op;int flag = 0;BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam){//char textA[10], textB[10], text[20];//int a, b, result;int num[10];char text[30], edit[30];char tmp[30];char op_tmp[10];switch(Message){case WM_COMMAND:// MessageBox(hwnd, TEXT("收到"), NULL, MB_OK);// 获取数字键if (wParam >= 1003 && wParam <= 1019){if(flag){SetDlgItemText(hwnd, ID_EDIT, "");flag = 0;}// MessageBox(hwnd, TEXT("数字键"), NULL, MB_OK);GetDlgItemText(hwnd, wParam, num, sizeof(num));GetDlgItemText(hwnd, ID_EDIT, edit, sizeof(edit));// sprintf(text, "获取到了数字【%s】", num);sprintf(edit, "%s%s", edit, num);SetDlgItemText(hwnd, ID_EDIT, edit);// MessageBox(hwnd, text, NULL, MB_OK);}// 获取运算符if(wParam >= 1020 && wParam <= 1023){// 把前一个edit中的数字拿出来保存GetDlgItemText(hwnd, ID_EDIT, tmp, sizeof(tmp));num_a = atoi(tmp);// 获取到运算符GetDlgItemText(hwnd, wParam, op_tmp, sizeof(op_tmp));op = op_tmp[0];SetDlgItemText(hwnd, ID_EDIT, "");}switch(wParam){//1019 IDC_BUTTON11/*case IDC_BUTTON11:MessageBox(hwnd,TEXT("数字键【0】!"), NULL, MB_OK);break;*/case IDOK://1.点出来一个数字//2.点一个操作符//3.点出来一个数字//4.点【确定】出来一个结果GetDlgItemText(hwnd, ID_EDIT, tmp, sizeof(tmp));num_b = atoi(tmp);switch(op){case '+':result = num_a + num_b;break;case '-':result = num_a - num_b;break;case '*':result = num_a * num_b;break;case '/':result = num_a / num_b;break;}sprintf(text, "%d", result);SetDlgItemText(hwnd, ID_EDIT, text);flag = 1;//MessageBox(hwnd, TEXT("确定按钮被按下"), TEXT("标题"), MB_OK);//GetDlgItemText(hwnd, ID_A, textA, sizeof(textA));//GetDlgItemText(hwnd, ID_B, textB, sizeof(textB));//a = atoi(textA);//b = atoi(textB);//result = a+b;//sprintf(text, "相加结果是 %d", result);//MessageBox(hwnd, text, NULL, MB_OK);break;case IDCANCEL:MessageBox(hwnd, TEXT("【取消】按钮被按下"), TEXT("标题"), MB_OK);break;}break;case WM_CLOSE:DestroyWindow(hwnd);break;}return 0;}int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){DialogBox(hInstance,MAKEINTRESOURCE(ID_MAIN),NULL,DlgProc);return 0;}

编辑简单的计算器


0 0
原创粉丝点击