WIN32 API应用开发——简易计算器设计
前言: 由于项目需求,最近接触到C++应用程序开发。对倾向于界面设计的应用程序开发来说,QT是目前主流的选择,无论是这个工具本身,还是相关生态的繁荣程度,都是非常适合开发者去学习使用。在学习QT之前,有必要研究一下较之来说更基础的平台接口类的应用程序开发,从而帮助我们更好理解应用程序的运行原理。
Windows API 是用于创建 Windows 应用程序的基于 C 语言的框架。 它诞生自 20 世纪 80 年代,几十年来一直用于创建 Windows 应用程序。 在 Windows API 的基础上,构建了更高级、更易于编程的框架。 例如,MFC、ATL、.NET 框架。它是Windows系统与Windows应用程序间的标准程序接口,可以为应用程序提供windows系统特殊函数及数据结构。
一、设计目标
基于VS2022设计一个桌面程序(.exe),可以实现简单的“加、减、乘、除、方幂、方根”六种运算。
操作步骤:
- 在简易计算器对话窗口的两个输入编辑框里输入数据
- 通过单选按钮选择运算法则
- 点击运算按钮
- 在结果编辑框呈现计算结果
二、工程创建
使用Visual Studio 2022开发实现。
- 左上角
文件
-新建
-项目
。 - 筛选语言为
C++
,平台为Windows
,项目类型为桌面
,选择Windows桌面向导
,下一步。
![](https://img-blog.csdnimg.cn/f27a538cac30411f92514e588efc8cd8.png#pic_center)
Windows桌面向导
- 对项目命名,选定项目路径,创建。
- 在应用程序类型中选择
桌面应用程序(.exe)
,勾选空项目,确定。
![](https://img-blog.csdnimg.cn/f3f89d917a7042239310dbf2dce1d105.png#pic_center)
应用程序类型
- 在解决方案资源管理器窗口中,选中创建的项目,右击,
添加
-新建项
,选择C++文件(.cpp)
,改名,添加。
![](https://img-blog.csdnimg.cn/ae9c8368adae435da74da94a6c6f1a13.png#pic_center)
添加C++文件
三、创建对话窗口(Dialog)
- 在解决方案资源管理器窗口中,选中项目,右击,
添加
-资源
,选择资源类型为Dialog
,新建
。
![](https://img-blog.csdnimg.cn/21f80fff7d654ecda1dd238413cb7de7.png#pic_center)
添加资源
- 创建好的对话窗口
![](https://img-blog.csdnimg.cn/80f22c6e373d4541a63502376230b68f.png#pic_center)
新建的Dialog
- 删除不用的按钮控件
确定
、取消
,选中控件,键盘按下delete
。 - 打开左侧
工具箱
,选中控件Button
、Edit Control
、Radio Button
、Static Text
,拖动至对话窗口中。
工具箱
- 点击上方菜单栏的网格键,切换为网格模式,调整好各个控件的位置。拖拽控件边缘,调整控件大小。
![](https://img-blog.csdnimg.cn/29244872905245a198b6e613dbf1f8b8.png#pic_center)
网格模式
- 改名
-
Button
控件:选中控件,右击菜单,点击属性。在弹出的属性栏中,找到描述文字
栏,修改按钮名称。
属性 -
Static Text
控件:在界面中,在适当的位置进行相关文字描述。操作同上。 -
对话窗口
Dialog
:改为简易计算器
。选中Dialog
外框,操作同上。
-
- 最终效果
我设计的对话窗口
![](https://img-blog.csdnimg.cn/2b2c2541ed0543218d862c25a2ffb30c.png#pic_center)
预览对话窗口
四、程序设计
仅一个CPP文件
全部代码:
#include <windows.h>
#include <stdio.h>
#include "resource.h"
#include <atlstr.h> //tstof 、CString 所需
#include <windowsx.h>
/* 开发时间:20221130 人员:ZZH CSDN博客:https://blog.csdn.net/qq_46290137?type=blog*/
HINSTANCE Hinst;
INT_PTR CALLBACK DialogProc(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_EDIT1://编辑框1。控件属性中,可以查看到控件ID
{}
break;
case IDC_EDIT2://编辑框2
{}
break;
case IDC_RADIO1://单选按钮1
{}
break;
case IDC_RADIO2://单选按钮2
{}
break;
case IDC_RADIO3://单选按钮3
{}
break;
case IDC_RADIO4://单选按钮4
{}
break;
case IDC_RADIO5://单选按钮5
{}
break;
case IDC_RADIO6://单选按钮6
{}
break;
case IDC_BUTTON1:
{
//获取数1
//获取控件句柄
HWND Num1 = GetDlgItem(hdlg, IDC_EDIT1);
CHAR str1[50];
int n = GetWindowText(Num1, str1, 40);
//将wchar转换为in
int num1 = (n > 0) ? _tstof(str1) : 0;
//获取数2
HWND Num2 = GetDlgItem(hdlg, IDC_EDIT2);
CHAR str2[50];
int l = GetWindowText(Num2, str2, 40);
int num2 = (l > 0) ? _tstof(str2) : 0;
//获取选项,确定运算法则
bool r1 = 0;
bool r2 = 0;
bool r3 = 0;
bool r4 = 0;
bool r5 = 0;
bool r6 = 0;
HWND R1 = GetDlgItem(hdlg, IDC_RADIO1);// +
r1 = Button_GetCheck(R1);
HWND R2 = GetDlgItem(hdlg, IDC_RADIO2);// -
r2 = Button_GetCheck(R2);
HWND R3 = GetDlgItem(hdlg, IDC_RADIO3);// *
r3 = Button_GetCheck(R3);
HWND R4 = GetDlgItem(hdlg, IDC_RADIO4);// /
r4 = Button_GetCheck(R4);
HWND R5 = GetDlgItem(hdlg, IDC_RADIO5);// ^
r5 = Button_GetCheck(R5);
HWND R6 = GetDlgItem(hdlg, IDC_RADIO6);// sqrt
r6 = Button_GetCheck(R6);
//按所选法则运算
float resultf ; //小数结果
long long int resultl; //超长整数结果
if (r1 == 1) //加法运算
{
resultl = num1 + num2;
HWND Result = GetDlgItem(hdlg, IDC_EDIT3);
//转格式
CString str;
str.Format(_T("%d"), resultl);
//显示
SetWindowText(Result, str);
}
if (r2 == 1)
{
resultl = num1 - num2;
HWND Result = GetDlgItem(hdlg, IDC_EDIT3);
//转格式
CString str;
str.Format(_T("%d"), resultl);
//显示
SetWindowText(Result, str);
}
if (r3 == 1)
{
resultl = num1 * num2;
HWND Result = GetDlgItem(hdlg, IDC_EDIT3);
//转格式
CString str;
str.Format(_T("%d"), resultl);
//显示
SetWindowText(Result, str);
}
if (r4 == 1)
{
resultf = float(num1) / float(num2);
HWND Result = GetDlgItem(hdlg, IDC_EDIT3);
//转格式
CString str;
str.Format(_T("%f"), resultf);
//显示
SetWindowText(Result, str);
}
if (r5 == 1)
{
resultl = pow(num1 ,num2); //pow函数,计算方幂,第一个参数为底,第二个参数为幂. 有数据类型int的大小限制
HWND Result = GetDlgItem(hdlg, IDC_EDIT3);
//转格式
CString str;
str.Format(_T("%d"), resultl);
//显示
SetWindowText(Result, str);
}
if (r6 == 1)
{
resultf = pow(num1, 1/float(num2)); //此处1/num2,不要忘记转其为float或double,否则num2>1时,函数的第二个参数都会被置为0,从而让运算结果一直为“数1的0次方”
HWND Result = GetDlgItem(hdlg, IDC_EDIT3);
//转格式
CString str;
str.Format(_T("%f"), resultf);
//显示
SetWindowText(Result, str);
}
}
break;
}
}
break;
case WM_SYSCOMMAND:
{
//关闭对话框时关闭主窗口
if (wParam == SC_CLOSE) {
PostQuitMessage(0);
}
}
break;
}
return (INT_PTR)FALSE;
}
int CALLBACK WinMain(_In_ HINSTANCE hInstance,_In_ HINSTANCE hPrevInstance,_In_ LPSTR lpCmdLine,
_In_ int nCmdShow)
{
Hinst = hInstance;
//创建对话框
HWND hDialog = CreateDialogA(Hinst, MAKEINTRESOURCE(IDD_DIALOG1), GetDesktopWindow(), (DLGPROC)DialogProc);
ShowWindow(hDialog, SW_SHOW);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
五、运行效果
![](https://img-blog.csdnimg.cn/deda6cc1e2ee404dac63689e9fe7d0a9.gif#pic_center)
运行结果