Windows SDK(九)登录框和计算器练习

这节课我们分别开始讲解登录框和计算机的实现

登录框实现

我们以上节课所学,自行创建一个对话框,ID为IDD_DIALOG1并将他编辑为一个登录框的样式。其中我们将账户的编辑框ID设置为IDC_ENIT_USERNAME,密码的编辑框ID设置为IDC_ENIT_PASSWORD。创建样式如下

完成以上步骤以后,我们便开始创建对话框并将该对话框设置为界面主页面

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 将实例句柄存储在全局变量中
   HWND hWnd = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), NULL, Login);//创建对话框。此时对话框出现时,默认在左上角位置
   if (!hWnd)
   {
      return FALSE;
   }
//此处开始将其移动到屏幕中间
   CRect rect; //注意包含头文件atltypes.h,该结构体用于表示一个矩形区域
   GetWindowRect(hWnd, &rect);//将登录框的左上角坐标(相对于系统屏幕讲)以及右下角坐标填充到rect中
   int nX = GetSystemMetrics(SM_CXFULLSCREEN);//获取系统桌面的x轴长度
   int nY = GetSystemMetrics(SM_CYFULLSCREEN);//获取系统桌面的y轴长度
   int nW = rect.Width();//获取对话框的宽
   int nH = rect.Height();//获取对话框的高
   MoveWindow(hWnd, (nX - nW) / 2, (nY - nH) / 2, nW, nH, TRUE);//移动对话框到指定位置
   //完成以后步骤以后,运行程序,对话框便来到了桌面中央位置
   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);
   return TRUE;
}

此时我们再为该对话框创建一个窗口过程,并实现相关功能

INT_PTR CALLBACK Login(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		switch (HIWORD(wParam))
		{
		case BN_CLICKED:
		{
			if (LOWORD(wParam) == IDC_BUTTON_LOGIN)
			{
				//句柄(USERNAME PASSWORD)
				HWND hUserWnd = GetDlgItem(hDlg, IDC_EDIT_USERNAME);
				HWND hPassWnd = GetDlgItem(hDlg, IDC_EDIT_PASSWORD);
				//获取控件上文本的长度
				DWORD dwUserLength = GetWindowTextLength(hUserWnd);
				DWORD dwPassLength = GetWindowTextLength(hPassWnd);
				//申请内存
				WCHAR * szUserBuffer = new WCHAR[dwUserLength + 1];
				WCHAR * szPassBuffer = new WCHAR[dwPassLength + 1];
				//获取控件文本
				GetWindowText(hUserWnd, szUserBuffer, dwUserLength + 1);
				GetWindowText(hPassWnd, szPassBuffer, dwPassLength + 1);
				if (wcscmp(szUserBuffer,L"rkvir") == 0 && wcscmp(szPassBuffer, L"123456") == 0)
				{
					//弹出新的对话框
					MessageBox(NULL, L"Login Success!", L"Msg", MB_OK);
				}
				else
				{
					MessageBox(NULL, L"Login Failed!", L"Msg", MB_OK);
				}
			}
			break;
		}
		default:
			break;
		}
		break;

	case WM_CLOSE:
	{
		EndDialog(hDlg, 0);
		PostQuitMessage(0);
		return TRUE;
	}
	}

	return (INT_PTR)FALSE;
}

如上便是一个登录框的实现。在之前章节中,我们同样也实现了一个登录框,只不过此处的实现因为对话框的原因不需要我们创建各种窗口控件,直接进行应用

接下来我们将实现一个计算器

计算器实现

计算器的实现,我们同样利用对话框进行实现。其中计算器名叫做calc,ID为IDD_DLALOG_CALC。

计算器中相关ID如下:

我们创建的计算器样式如下,其可以实现整数的加减乘除功能

其中将显示面板属性修改为如下形式:

完成以上步骤以后,我们便开始创建对话框并将该对话框设置为界面主页面

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 将实例句柄存储在全局变量中
   HWND hWnd = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG_CALC), NULL, CalcDialog);//创建对话框。此时对话框出现时,默认在左上角位置
   if (!hWnd)
   {
      return FALSE;
   }
//此处开始将其移动到屏幕中间
   CRect rect; //注意包含头文件atltypes.h,该结构体用于表示一个矩形区域
   GetWindowRect(hWnd, &rect);//将登录框的左上角坐标(相对于系统屏幕讲)以及右下角坐标填充到rect中
   int nX = GetSystemMetrics(SM_CXFULLSCREEN);//获取系统桌面的x轴长度
   int nY = GetSystemMetrics(SM_CYFULLSCREEN);//获取系统桌面的y轴长度
   int nW = rect.Width();//获取对话框的宽
   int nH = rect.Height();//获取对话框的高
   MoveWindow(hWnd, (nX - nW) / 2, (nY - nH) / 2, nW, nH, TRUE);//移动对话框到指定位置
   //完成以后步骤以后,运行程序,对话框便来到了桌面中央位置
   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);
   return TRUE;
}

此时,运行程序计算器在桌面中间显示。接下来我们以代码的形式,进行讲解并完成计算机的功能

WCHAR szTextBuffer[256] = { 0 };//显示缓冲区
int Value1 = 0;
int Value2 = 0; //参与运算的两个数
int TempValue1[8] = { 0 };
int TempValue2[8] = { 0 };//临时存放每个数的数组
int nFlag1 = 0; 
int nFlag2 = 0; //每个数组元素索引和数组有多长的标记
int nSwitch = 0; //切换第一个数和第二个数的标记
int nOperation = 0; //运算符标记(+:1 -:2 *:3 /:4 )
int nRet = 0; //结果值


//宏定义
#define OPERATION_ADD 1
#define OPERATION_SUB 2
#define OPERATION_IMUL 3
#define OPERATION_IDIV 4
INT_PTR CALLBACK CalcDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;
	case WM_COMMAND:// 菜单项、按钮或工具栏等控件应用的信息
	{
		HWND hEditText = GetDlgItem(hDlg, IDC_EDIT_TEXT);//获取显示屏句柄
		switch (HIWORD(wParam)) //通过信息通知码判断对控件是何种应用
		{
		case BN_CLICKED: //点击时的通知码
		{
			switch (LOWORD(wParam)) //判断点击的是哪个控件
			{
			case IDC_BUTTON_0://点击按键0时
			{
				if (nSwitch == 0) //点击时,该数字是第一个数
				{
					//在数组中存入当前选中的数字
					TempValue1[nFlag1] = 0;
					//后移索引
					nFlag1++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"0");//使用该函数,编译器会认为函数危险,我们只需要在项目属性代码生成以及预处理器中添加提示设置即可
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				//第二个数
				else
				{
					//在数组中存入当前选中的数字
					TempValue2[nFlag2] = 0;
					//后移索引
					nFlag2++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"0");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				break;
			}
			case IDC_BUTTON_1:
			{
				//第一个数
				if (nSwitch == 0)
				{
					//在数组中存入当前选中的数字
					TempValue1[nFlag1] = 1;
					//后移索引
					nFlag1++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"1");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				//第二个数
				else
				{
					//在数组中存入当前选中的数字
					TempValue2[nFlag2] = 1;
					//后移索引
					nFlag2++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"1");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				break;
			}
			case IDC_BUTTON_2:
			{
				//第一个数
				if (nSwitch == 0)
				{
					//在数组中存入当前选中的数字
					TempValue1[nFlag1] = 2;
					//后移索引
					nFlag1++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"2");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				//第二个数
				else
				{
					//在数组中存入当前选中的数字
					TempValue2[nFlag2] = 2;
					//后移索引
					nFlag2++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"2");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				break;
			}
			case IDC_BUTTON_3:
			{
				//第一个数
				if (nSwitch == 0)
				{
					//在数组中存入当前选中的数字
					TempValue1[nFlag1] = 3;
					//后移索引
					nFlag1++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"3");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				//第二个数
				else
				{
					//在数组中存入当前选中的数字
					TempValue2[nFlag2] = 3;
					//后移索引
					nFlag2++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"3");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				break;
			}
			case IDC_BUTTON_4:
			{
				//第一个数
				if (nSwitch == 0)
				{
					//在数组中存入当前选中的数字
					TempValue1[nFlag1] = 4;
					//后移索引
					nFlag1++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"4");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				//第二个数
				else
				{
					//在数组中存入当前选中的数字
					TempValue2[nFlag2] = 4;
					//后移索引
					nFlag2++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"4");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				break;
			}
			case IDC_BUTTON_5:
			{
				//第一个数
				if (nSwitch == 0)
				{
					//在数组中存入当前选中的数字
					TempValue1[nFlag1] = 5;
					//后移索引
					nFlag1++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"5");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				//第二个数
				else
				{
					//在数组中存入当前选中的数字
					TempValue2[nFlag2] = 5;
					//后移索引
					nFlag2++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"5");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				break;
			}
			case IDC_BUTTON_6:
			{
				//第一个数
				if (nSwitch == 0)
				{
					//在数组中存入当前选中的数字
					TempValue1[nFlag1] = 6;
					//后移索引
					nFlag1++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"6");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				//第二个数
				else
				{
					//在数组中存入当前选中的数字
					TempValue2[nFlag2] = 6;
					//后移索引
					nFlag2++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"6");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				break;
			}
			case IDC_BUTTON_7:
			{
				//第一个数
				if (nSwitch == 0)
				{
					//在数组中存入当前选中的数字
					TempValue1[nFlag1] = 7;
					//后移索引
					nFlag1++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"7");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				//第二个数
				else
				{
					//在数组中存入当前选中的数字
					TempValue2[nFlag2] = 7;
					//后移索引
					nFlag2++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"7");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				break;
			}
			case IDC_BUTTON_8:
			{
				//第一个数
				if (nSwitch == 0)
				{
					//在数组中存入当前选中的数字
					TempValue1[nFlag1] = 8;
					//后移索引
					nFlag1++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"8");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				//第二个数
				else
				{
					//在数组中存入当前选中的数字
					TempValue2[nFlag2] = 8;
					//后移索引
					nFlag2++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"8");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				break;
			}
			case IDC_BUTTON_9:
			{
				//第一个数
				if (nSwitch == 0)
				{
					//在数组中存入当前选中的数字
					TempValue1[nFlag1] = 9;
					//后移索引
					nFlag1++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"9");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				//第二个数
				else
				{
					//在数组中存入当前选中的数字
					TempValue2[nFlag2] = 9;
					//后移索引
					nFlag2++;
					//显示内容的拼接
					wcscat(szTextBuffer, L"9");
					//发送消息显示到EDIT
					SetWindowText(hEditText, szTextBuffer);
				}
				break;
			}
			case IDC_BUTTON_DEL: //运算完成以后清除所有缓存
			{
				Value1 = 0;
				Value2 = 0;
				TempValue1[8] = { 0 };
				TempValue2[8] = { 0 };
				nFlag1 = 0;
				nFlag2 = 0;
				nSwitch = 0;
				nOperation = 0;
				nRet = 0;
				memset(szTextBuffer, 0, 256);
				SetWindowText(hEditText, szTextBuffer);
				break;
			}
			case IDC_BUTTON_RET:
			{
				//数组输入的数字决定要*10几倍,得到最终的数。123 = 3 * 1 + 2 * 10 + 1 * 100
				int nCoefficient = 1;
				for (size_t i = nFlag1 - 1; i >= 0; i--)
				{
					Value1 += (TempValue1[i] * nCoefficient);
					//系数增长
					nCoefficient *= 10;
					//退出机制,当循环至个位数时运算后退出
					if (i == 0)
					{
						break;
					}
				}
				nCoefficient = 1;
				for (size_t i = nFlag2 - 1; i >= 0; i--)
				{
					//123 3 * 1 2 * 10 1 * 100
					Value2 += (TempValue2[i] * nCoefficient);
					//系数增长
					nCoefficient *= 10;
					//退出机制
					if (i == 0)
					{
						break;
					}
				}
				switch (nOperation)
				{
				case OPERATION_ADD:
				{
					nRet = Value1 + Value2;
					break;
				}
				case OPERATION_SUB:
				{
					nRet = Value1 - Value2;
					break;
				}
				case OPERATION_IMUL:
				{
					nRet = Value1 * Value2;
					break;
				}
				case OPERATION_IDIV:
				{
					nRet = Value1 / Value2;
					break;
				}
				}
				WCHAR szRetBuffer[50] = { 0 }; //设置运算结果缓冲区
				wsprintf(szRetBuffer, L"=%d", nRet);//将运算结果放入缓冲区中
				wcscat(szTextBuffer, szRetBuffer);//拼接运算数和运算结果
				SetWindowText(hEditText, szTextBuffer);
				break;
			}
			case IDC_BUTTON_ADD:
			{
				//标记运算符
				nOperation = OPERATION_ADD;
				//切换输入的数字
				nSwitch = 1;
				//显示内容的拼接
				wcscat(szTextBuffer, L"+");
				//发送消息显示到EDIT
				SetWindowText(hEditText, szTextBuffer);
				break;
			}
			case IDC_BUTTON_SUB:
			{
				//标记运算符
				nOperation = OPERATION_SUB;
				//切换输入的数字
				nSwitch = 1;
				//显示内容的拼接
				wcscat(szTextBuffer, L"-");
				//发送消息显示到EDIT
				SetWindowText(hEditText, szTextBuffer);
				break;
			}
			case IDC_BUTTON_IMUL:
			{
				//标记运算符
				nOperation = OPERATION_IMUL;
				//切换输入的数字
				nSwitch = 1;
				//显示内容的拼接
				wcscat(szTextBuffer, L"*");
				//发送消息显示到EDIT
				SetWindowText(hEditText, szTextBuffer);
				break;
			}
			case IDC_BUTTON_IDIV:
			{
				//标记运算符
				nOperation = OPERATION_IDIV;
				//切换输入的数字
				nSwitch = 1;
				//显示内容的拼接
				wcscat(szTextBuffer, L"/");
				//发送消息显示到EDIT
				SetWindowText(hEditText, szTextBuffer);
				break;
			}
			default:
				break;
			}
			break;
		}
		default:
			break;
		}
		break;
	}
	case WM_CLOSE:
	{
		EndDialog(hDlg, 0);
		PostQuitMessage(0);
		return TRUE;
	}
	}

	return (INT_PTR)FALSE;
}

此时便实现了一个支持整数四则运算的计算器了

作业

将课上实现的计算器进行完善,使其能够支持负数浮点数以及连续运算

相关控件ID如下

IDC_EDIT_TEXT

IDC_BUTTON_0

IDC_BUTTON_1

IDC_BUTTON_2

IDC_BUTTON_3

IDC_BUTTON_4

IDC_BUTTON_5

IDC_BUTTON_6

IDC_BUTTON_7

IDC_BUTTON_8

IDC_BUTTON_9

IDC_BUTTON_DEL

IDC_BUTTON_RET

IDC_BUTTON_ADD

IDC_BUTTON_SUB

IDC_BUTTON_IMUL

IDC_BUTTON_IDIV

IDC_BUTTON_POINT

IDC_BUTTON_ENG

下图便是我们的计算器

该计算器可以支持整数浮点数负数的四则连续运算

// calc.cpp : 定义应用程序的入口点。
//

#include "framework.h"
#include "calc.h"
#include <atltypes.h>
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst;                                // 当前实例
WCHAR szTitle[MAX_LOADSTRING];                  // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING];            // 主窗口类名

// 此代码模块中包含的函数的前向声明:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
INT_PTR CALLBACK CalcDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: 在此处放置代码。

    // 初始化全局字符串
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_CALC, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // 执行应用程序初始化:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CALC));

    MSG msg;

    // 主消息循环:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}



//
//  函数: MyRegisterClass()
//
//  目标: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_CALC));
    wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_CALC);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   函数: InitInstance(HINSTANCE, int)
//
//   目标: 保存实例句柄并创建主窗口
//
//   注释:
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // 将实例句柄存储在全局变量中

   /*
   HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
*/
   HWND hWnd = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG_CALC), NULL, CalcDialog);

   if (!hWnd)
   {
	   return FALSE;
   }

   CRect rect;
   GetWindowRect(hWnd, &rect);
   int nX = GetSystemMetrics(SM_CXFULLSCREEN);
   int nY = GetSystemMetrics(SM_CYFULLSCREEN);
   int nW = rect.Width();
   int nH = rect.Height();
   MoveWindow(hWnd, (nX - nW) / 2, (nY - nH) / 2, nW, nH, TRUE);

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}


//显示缓冲区
WCHAR szTextBuffer[256] = { 0 };
//参与运算的两个数
float Value1 = 0;
float Value2 = 0;
//临时存放每一位的数组
float TempValue[4][8] = { 0 };
//数组索引和数组有多长的标记
int nFlag1 = 0;
int nFlag2 = 0;//第一个数整数和小数部分标记
int nFlag3 = 0;
int nFlag4 = 0;//第二个数整数和小数部分标记
//切换第一个数和第二个数以及负数和浮点数的标记
int nSwitch = 0;
int fSwitch1 = 0;
int fSwitch2 = 0;
int eSwitch = 0;
//运算符标记(+:1 -:2 *:3 /:4 )
int nOperation = 0;
//连续运算的标记
int cFlag = 0;
//结果值
float nRet = 0;

//宏定义
#define OPERATION_ADD 1
#define OPERATION_SUB 2
#define OPERATION_IMUL 3
#define OPERATION_IDIV 4
#define OPERATION_FLOAT 5
INT_PTR CALLBACK CalcDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
	{
		HWND hEditText = GetDlgItem(hDlg, IDC_EDIT_TEXT);
		switch (HIWORD(wParam))
		{
		case BN_CLICKED:
		{
			switch (LOWORD(wParam))
			{
			case IDC_BUTTON_0:
			{
				if (nSwitch == 0)//第一个数
				{

					if (fSwitch1 == 0)//判断是不是浮点数
					{
						//在数组中存入当前选中的数字
						TempValue[0][nFlag1] = 0;
						//后移索引
						nFlag1++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"0");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[1][nFlag2] = 0;
						nFlag2++;
						wcscat(szTextBuffer, L"0");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				//第二个数
				else
				{
					if (fSwitch2 == 0)
					{
						//在数组中存入当前选中的数字
						TempValue[2][nFlag3] = 0;
						//后移索引
						nFlag3++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"0");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[3][nFlag4] = 0;
						nFlag4++;
						wcscat(szTextBuffer, L"0");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				break;
			}
			case IDC_BUTTON_1:
			{
				if (nSwitch == 0)//第一个数
				{

					if (fSwitch1 == 0)//判断是不是浮点数
					{
						//在数组中存入当前选中的数字
						TempValue[0][nFlag1] = 1;
						//后移索引
						nFlag1++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"1");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[1][nFlag2] = 1;
						nFlag2++;
						wcscat(szTextBuffer, L"1");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				//第二个数
				else
				{
					if (fSwitch2 == 0)
					{
						//在数组中存入当前选中的数字
						TempValue[2][nFlag3] = 1;
						//后移索引
						nFlag3++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"1");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[3][nFlag4] = 1;
						nFlag4++;
						wcscat(szTextBuffer, L"1");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				break;
			}
			case IDC_BUTTON_2:
			{
				if (nSwitch == 0)//第一个数
				{

					if (fSwitch1 == 0)//判断是不是浮点数
					{
						//在数组中存入当前选中的数字
						TempValue[0][nFlag1] = 2;
						//后移索引
						nFlag1++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"2");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[1][nFlag2] = 2;
						nFlag2++;
						wcscat(szTextBuffer, L"2");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				//第二个数
				else
				{
					if (fSwitch2 == 0)
					{
						//在数组中存入当前选中的数字
						TempValue[2][nFlag3] = 2;
						//后移索引
						nFlag3++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"2");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[3][nFlag4] = 2;
						nFlag4++;
						wcscat(szTextBuffer, L"2");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				break;
			}
			case IDC_BUTTON_3:
			{
				if (nSwitch == 0)//第一个数
				{

					if (fSwitch1 == 0)//判断是不是浮点数
					{
						//在数组中存入当前选中的数字
						TempValue[0][nFlag1] = 3;
						//后移索引
						nFlag1++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"3");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[1][nFlag2] = 3;
						nFlag2++;
						wcscat(szTextBuffer, L"3");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				//第二个数
				else
				{
					if (fSwitch2 == 0)
					{
						//在数组中存入当前选中的数字
						TempValue[2][nFlag3] = 3;
						//后移索引
						nFlag3++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"3");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[3][nFlag4] = 3;
						nFlag4++;
						wcscat(szTextBuffer, L"3");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				break;
			}
			case IDC_BUTTON_4:
			{
				if (nSwitch == 0)//第一个数
				{

					if (fSwitch1 == 0)//判断是不是浮点数
					{
						//在数组中存入当前选中的数字
						TempValue[0][nFlag1] = 4;
						//后移索引
						nFlag1++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"4");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[1][nFlag2] = 4;
						nFlag2++;
						wcscat(szTextBuffer, L"4");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				//第二个数
				else
				{
					if (fSwitch2 == 0)
					{
						//在数组中存入当前选中的数字
						TempValue[2][nFlag3] = 4;
						//后移索引
						nFlag3++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"4");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[3][nFlag4] = 4;
						nFlag4++;
						wcscat(szTextBuffer, L"4");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				break;
			}
			case IDC_BUTTON_5:
			{
				if (nSwitch == 0)//第一个数
				{

					if (fSwitch1 == 0)//判断是不是浮点数
					{
						//在数组中存入当前选中的数字
						TempValue[0][nFlag1] = 5;
						//后移索引
						nFlag1++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"5");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[1][nFlag2] = 5;
						nFlag2++;
						wcscat(szTextBuffer, L"5");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				//第二个数
				else
				{
					if (fSwitch2 == 0)
					{
						//在数组中存入当前选中的数字
						TempValue[2][nFlag3] = 5;
						//后移索引
						nFlag3++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"5");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[3][nFlag4] = 5;
						nFlag4++;
						wcscat(szTextBuffer, L"5");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				break;
			}
			case IDC_BUTTON_6:
			{
				if (nSwitch == 0)//第一个数
				{

					if (fSwitch1 == 0)//判断是不是浮点数
					{
						//在数组中存入当前选中的数字
						TempValue[0][nFlag1] = 6;
						//后移索引
						nFlag1++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"6");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[1][nFlag2] = 6;
						nFlag2++;
						wcscat(szTextBuffer, L"6");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				//第二个数
				else
				{
					if (fSwitch2 == 0)
					{
						//在数组中存入当前选中的数字
						TempValue[2][nFlag3] = 6;
						//后移索引
						nFlag3++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"6");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[3][nFlag4] = 6;
						nFlag4++;
						wcscat(szTextBuffer, L"6");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				break;
			}
			case IDC_BUTTON_7:
			{
				if (nSwitch == 0)//第一个数
				{

					if (fSwitch1 == 0)//判断是不是浮点数
					{
						//在数组中存入当前选中的数字
						TempValue[0][nFlag1] = 7;
						//后移索引
						nFlag1++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"7");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[1][nFlag2] = 7;
						nFlag2++;
						wcscat(szTextBuffer, L"7");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				//第二个数
				else
				{
					if (fSwitch2 == 0)
					{
						//在数组中存入当前选中的数字
						TempValue[2][nFlag3] = 7;
						//后移索引
						nFlag3++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"7");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[3][nFlag4] = 7;
						nFlag4++;
						wcscat(szTextBuffer, L"7");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				break;
			}
			case IDC_BUTTON_8:
			{
				if(nSwitch == 0)//第一个数
				{ 
				
					if (fSwitch1 == 0)//判断是不是浮点数
					{
						//在数组中存入当前选中的数字
						TempValue[0][nFlag1] = 8;
						//后移索引
						nFlag1++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"8");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[1][nFlag2] = 8;
						nFlag2++;
						wcscat(szTextBuffer, L"8");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				//第二个数
				else
				{
					if (fSwitch2 == 0)
					{
						//在数组中存入当前选中的数字
						TempValue[2][nFlag3] = 8;
						//后移索引
						nFlag3++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"8");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[3][nFlag4] = 8;
						nFlag4++;
						wcscat(szTextBuffer, L"8");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				break;
			}
			case IDC_BUTTON_9:
			{
				//第一个数
				if (nSwitch == 0)
				{
					if (fSwitch1 == 0)
					{
						//在数组中存入当前选中的数字
						TempValue[0][nFlag1] = 9;
						//后移索引
						nFlag1++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"9");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[1][nFlag2] = 9;
						nFlag2++;
						wcscat(szTextBuffer, L"9");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				//第二个数
				else
				{
					if (fSwitch2 == 0)
					{
						//在数组中存入当前选中的数字
						TempValue[2][nFlag3] = 9;
						//后移索引
						nFlag3++;
						//显示内容的拼接
						wcscat(szTextBuffer, L"9");
						//发送消息显示到EDIT
						SetWindowText(hEditText, szTextBuffer);
					}
					else
					{
						TempValue[3][nFlag4] = 9;
						nFlag4++;
						wcscat(szTextBuffer, L"9");
						SetWindowText(hEditText, szTextBuffer);
					}
				}
				break;
			}
			case IDC_BUTTON_DEL:
			{
				Value1 = 0;
				Value2 = 0;
				TempValue[4][0] = { 0 };
				nFlag1 = 0;
				nFlag2 = 0;
				nFlag3 = 0;
				nFlag4 = 0;
				nSwitch = 0;
				eSwitch = 0;
				fSwitch1 = 0;
				fSwitch2 = 0;
				nOperation = 0;
				nRet = 0;
				cFlag = 0;
				memset(szTextBuffer, 0, 256);
				SetWindowText(hEditText, szTextBuffer);
				break;
			}
			case IDC_BUTTON_RET:
			{
				int nCoefficient = 1;
				if (cFlag == 0)
				{
					//数组输入的数字决定要*10的几倍,得到最终的数
					for (int i = nFlag1 - 1; i >= 0; i--)
					{
						//123 3 * 1 2 * 10 1 * 100
						Value1 += (TempValue[0][i] * nCoefficient);
						//系数增长
						nCoefficient *= 10;
					}
					nCoefficient = 10;
					for (int i = 0; i < nFlag2; i++)
					{
						float neg = TempValue[1][i] / nCoefficient;
						Value1 += neg;
						nCoefficient *= 10;
					}
					if (eSwitch == 1)
					{
						Value1 -= 2 * Value1;
					}
				}
				nCoefficient = 1;
				for (int i = nFlag3 - 1; i >= 0; i--)
				{
					//123 3 * 1 2 * 10 1 * 100
					Value2 += (TempValue[2][i] * nCoefficient);
					//系数增长
					nCoefficient *= 10;
				}
				nCoefficient = 10;
				for (int i = 0; i < nFlag4; i++)
				{
					Value2 += (TempValue[3][i] / nCoefficient);
					nCoefficient *= 10;
				}
				if (eSwitch == 2)
				{
					Value2 -= 2 * Value2;
				}

				switch (nOperation)
				{
				case OPERATION_ADD:
				{
					nRet = Value1 + Value2;
					break;
				}
				case OPERATION_SUB:
				{
					nRet = Value1 - Value2;
					break;
				}
				case OPERATION_IMUL:
				{
					nRet = Value1 * Value2;
					break;
				}
				case OPERATION_IDIV:
				{
					nRet = Value1 / Value2;
					break;
				}
				}
				WCHAR szRetBuffer[50] = { 0 };
				swprintf(szRetBuffer, 50, L"=%f", nRet);
				wcscat(szTextBuffer, szRetBuffer);
				SetWindowText(hEditText, szTextBuffer);
				Value1 = nRet;
				Value2 = 0;
				nFlag1 = 0;
				nFlag2 = 0;
				nFlag3 = 0;
				nFlag4 = 0;
				nSwitch = 1;
				cFlag = 1;
				break;
			}
			case IDC_BUTTON_ADD:
			{
				//标记运算符
				nOperation = OPERATION_ADD;
				//切换输入的数字
				nSwitch = 1;
				//显示内容的拼接
				wcscat(szTextBuffer, L"+");
				//发送消息显示到EDIT
				SetWindowText(hEditText, szTextBuffer);
				break;
			}
			case IDC_BUTTON_SUB:
			{
				//标记运算符
				nOperation = OPERATION_SUB;
				//切换输入的数字
				nSwitch = 1;
				//显示内容的拼接
				wcscat(szTextBuffer, L"-");
				//发送消息显示到EDIT
				SetWindowText(hEditText, szTextBuffer);
				break;
			}
			case IDC_BUTTON_IMUL:
			{
				//标记运算符
				nOperation = OPERATION_IMUL;
				//切换输入的数字
				nSwitch = 1;
				//显示内容的拼接
				wcscat(szTextBuffer, L"*");
				//发送消息显示到EDIT
				SetWindowText(hEditText, szTextBuffer);
				break;
			}
			case IDC_BUTTON_IDIV:
			{
				//标记运算符
				nOperation = OPERATION_IDIV;
				//切换输入的数字
				nSwitch = 1;
				//显示内容的拼接
				wcscat(szTextBuffer, L"/");
				//发送消息显示到EDIT
				SetWindowText(hEditText, szTextBuffer);
				break;
			}
			case IDC_BUTTON_POINT://切换第一个数和第二个数浮点数状态
			{
				if (nSwitch == 0) //第一个数
				{
					fSwitch1 = 1;
				}
				else//第二个数
				{
					fSwitch2 = 1;
				}
				wcscat(szTextBuffer, L".");
				SetWindowText(hEditText, szTextBuffer);
				break;
			}
			case IDC_BUTTON_ENG:
			{
				if (nSwitch == 0) //第一个数
				{
					eSwitch = 1;
				}
				else//第二个数
				{
					eSwitch = 2;
				}
				wcscat(szTextBuffer, L"-");
				SetWindowText(hEditText, szTextBuffer);
				break;
			}
			default:
				break;
			}
			break;
		}
		default:
			break;
		}
		break;
	}
	case WM_CLOSE:
	{
		EndDialog(hDlg, 0);
		PostQuitMessage(0);
		return TRUE;
	}
	}

	return (INT_PTR)FALSE;
}

//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目标: 处理主窗口的消息。
//
//  WM_COMMAND  - 处理应用程序菜单
//  WM_PAINT    - 绘制主窗口
//  WM_DESTROY  - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // 分析菜单选择:
            switch (wmId)
            {
            case IDM_ABOUT:
                DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
                break;
            case IDM_EXIT:
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, message, wParam, lParam);
            }
        }
        break;
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: 在此处添加使用 hdc 的任何绘图代码...
            EndPaint(hWnd, &ps);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值