编写简单的windows桌面计算器程序

时间:2021-10-03 11:10:20

编译环境:VS2017

主文件为:

 #include "stdafx.h"
#include "WindowsProject5.h"
#include "Resource.h"
#define NULL 0 //回调函数
BOOL CALLBACK MainProc(
HWND hwndDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
//以下三行为调试语句,可去除
char s[];
wsprintf((LPWSTR)s,L"uMsg=%d,wParam=%d,lParam=%d\n", uMsg, wParam, (int)lParam);
OutputDebugStringW((LPWSTR)s); //对于菜单、加速键来说,点击后发送WM_COMMAND消息
if (WM_COMMAND == uMsg)
{
//如果点击取消按钮,关闭对话框
if (LOWORD(wParam) == IDCANCEL)
{
EndDialog(hwndDlg, IDCANCEL);
return TRUE;
};
//如果点击计算按钮,进行加法计算,得出结果
if (LOWORD(wParam) == IDOK)
{
int nLeft = GetDlgItemInt(hwndDlg, IDC_LEFT, NULL, TRUE);
int nRight = GetDlgItemInt(hwndDlg, IDC_RIGHT, NULL, TRUE);
int nResult = nLeft + nRight;
SetDlgItemInt(hwndDlg,IDC_RESULT,nResult,TRUE);
}
}
return FALSE;
} //win主函数
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{ DialogBox(hInstance,(LPWSTR)IDD_DIALOG1,,(DLGPROC)MainProc);
return ;
}

资源文件:

 #define IDI_ICON2                       131
#define IDD_DIALOG1 133
#define IDC_RESULT 1004
#define IDC_RIGHT 1005
#define IDC_LEFT 1006
#define IDC_STATIC -1

对话框截图:

编写简单的windows桌面计算器程序

运行结果:

编写简单的windows桌面计算器程序