*/ --------------------------------------------------------------------------------------
*/ 出自: 编程中国 http://www.bc-cn.net
*/ 作者: tyc1920 E-mail:tyc.1920@yahoo.com.cn QQ:609670447
*/ 时间: 2007-11-12 编程论坛首发
*/ 声明: 尊重作者劳动,转载请保留本段文字
*/ --------------------------------------------------------------------------------------
//Win32 Application
#include <windows.h>
#include "resource.h"
HINSTANCE hInst;
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
BOOL CALLBACK DlgProc(HWND hdlg,UINT uMsg,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // pointer to command line
int nCmdShow // show state of window
)
{
HWND hwnd;
WNDCLASS wc;
MSG msg;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance=hInstance;
wc.lpfnWndProc=WindowProc;
wc.lpszClassName="MyWndClass";
wc.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1);
wc.style=0;
if(!RegisterClass(&wc))
{
MessageBeep(0);
return FALSE;
}
hwnd=CreateWindow(
"MyWndClass",
"My Dialog!",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
hInst=hInstance;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_COMPUTER:
DialogBox(hInst,MAKEINTRESOURCE(IDD_DIALOG1),hwnd,DlgProc);
break;
case IDM_EXIT:
PostQuitMessage(0);
break;
case IDM_ABOUT:
MessageBox(hwnd,"作者:安师大吴鹏","关于",0);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
BOOL CALLBACK DlgProc(HWND hdlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
static double fNum1=0,fNum2=0,fNum3=0;
static char str1[10],str2[20],str3[30];
static int Op=0;
static BOOL flt=0;
switch(uMsg)
{
case WM_INITDIALOG:
return 1;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_BUTTON1: //清除按钮...
strcpy(str1,"");
strcpy(str2,"");
strcpy(str3,"");
flt=0;
Op=0;
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"");
break;
case IDC_BUTTON18: //0...
if(Op==0)
{
if(str1[0]=='0'&&str1[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str1,"0");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str1);
}
}
else
{
if(str2[0]=='0'&&str2[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str2,"0");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str2);
}
}
break;
case IDC_BUTTON19:
if(Op==0)
{
if(str1[0]=='0'&&str1[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str1,"1");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str1);
}
}
else
{
if(str2[0]=='0'&&str2[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str2,"1");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str2);
}
}
break;
case IDC_BUTTON20:
if(Op==0)
{
if(str1[0]=='0'&&str1[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str1,"2");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str1);
}
}
else
{
if(str2[0]=='0'&&str2[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str2,"2");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str2);
}
}
break;
case IDC_BUTTON21:
if(Op==0)
{
if(str1[0]=='0'&&str1[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str1,"3");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str1);
}
}
else
{
if(str2[0]=='0'&&str2[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str2,"3");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str2);
}
}
break;
case IDC_BUTTON22:
if(Op==0)
{
if(str1[0]=='0'&&str1[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str1,"4");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str1);
}
}
else
{
if(str2[0]=='0'&&str2[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str2,"4");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str2);
}
}
break;
case IDC_BUTTON23:
if(Op==0)
{
if(str1[0]=='0'&&str1[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str1,"5");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str1);
}
}
else
{
if(str2[0]=='0'&&str2[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str2,"5");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str2);
}
}
break;
case IDC_BUTTON24:
if(Op==0)
{
if(str1[0]=='0'&&str1[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str1,"6");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str1);
}
}
else
{
if(str2[0]=='0'&&str2[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str2,"6");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str2);
}
}
break;
case IDC_BUTTON25:
if(Op==0)
{
if(str1[0]=='0'&&str1[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str1,"7");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str1);
}
}
else
{
if(str2[0]=='0'&&str2[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str2,"7");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str2);
}
}
break;
case IDC_BUTTON26:
if(Op==0)
{
if(str1[0]=='0'&&str1[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str1,"8");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str1);
}
}
else
{
if(str2[0]=='0'&&str2[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str2,"8");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str2);
}
}
break;
case IDC_BUTTON27:
if(Op==0)
{
if(str1[0]=='0'&&str1[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str1,"9");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str1);
}
}
else
{
if(str2[0]=='0'&&str2[1]!='.')
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
else
{
strcat(str2,"9");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str2);
}
}
break;
case IDC_BUTTON28: //等于按钮...
if(Op!=0)
{
fNum1=atof(str1);
fNum2=atof(str2);
switch(Op)
{
case 1:
fNum3=fNum1+fNum2;
break;
case 2:
fNum3=fNum1-fNum2;
break;
case 3:
fNum3=fNum1*fNum2;
break;
case 4:
fNum3=fNum1/fNum2;
break;
}
itoa(fNum3,str3,10);
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str3);
Op=0;
strcpy(str1,"");
strcpy(str2,"");
}
break;
case IDC_BUTTON29: //小数点按钮...
if(Op==0)
{
if(flt==0&&str1[0]!='\0')
{
flt=1;
strcat(str1,".");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str1);
}
else SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
}
else
{
if(flt==0&&str2[0]!='\0')
{
flt=1;
strcat(str2,".");
SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),str2);
}
else SetWindowText(GetDlgItem(hdlg,IDC_EDIT1),"输入有误!");
}
break;
case IDC_BUTTON30: //加法...
Op=1;
flt=0;
break;
case IDC_BUTTON31: //减法...
Op=2;
flt=0;
break;
case IDC_BUTTON32: //乘法...
Op=3;
flt=0;
break;
case IDC_BUTTON33: //除法...
Op=4;
flt=0;
break;
}
break;
case WM_CLOSE:
EndDialog(hdlg,0);
return 1;
}
return 0;
}