当前位置: 首页 > news >正文

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");//使用该函数,编译器会认为函数危险,我们只需要在项目属性代码生成以及预处理器中添加提示设置即可//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}//第二个数else{//在数组中存入当前选中的数字TempValue2[nFlag2] = 0;//后移索引nFlag2++;//显示内容的拼接wcscat(szTextBuffer, L"0");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}break;}case IDC_BUTTON_1:{//第一个数if (nSwitch == 0){//在数组中存入当前选中的数字TempValue1[nFlag1] = 1;//后移索引nFlag1++;//显示内容的拼接wcscat(szTextBuffer, L"1");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}//第二个数else{//在数组中存入当前选中的数字TempValue2[nFlag2] = 1;//后移索引nFlag2++;//显示内容的拼接wcscat(szTextBuffer, L"1");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}break;}case IDC_BUTTON_2:{//第一个数if (nSwitch == 0){//在数组中存入当前选中的数字TempValue1[nFlag1] = 2;//后移索引nFlag1++;//显示内容的拼接wcscat(szTextBuffer, L"2");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}//第二个数else{//在数组中存入当前选中的数字TempValue2[nFlag2] = 2;//后移索引nFlag2++;//显示内容的拼接wcscat(szTextBuffer, L"2");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}break;}case IDC_BUTTON_3:{//第一个数if (nSwitch == 0){//在数组中存入当前选中的数字TempValue1[nFlag1] = 3;//后移索引nFlag1++;//显示内容的拼接wcscat(szTextBuffer, L"3");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}//第二个数else{//在数组中存入当前选中的数字TempValue2[nFlag2] = 3;//后移索引nFlag2++;//显示内容的拼接wcscat(szTextBuffer, L"3");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}break;}case IDC_BUTTON_4:{//第一个数if (nSwitch == 0){//在数组中存入当前选中的数字TempValue1[nFlag1] = 4;//后移索引nFlag1++;//显示内容的拼接wcscat(szTextBuffer, L"4");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}//第二个数else{//在数组中存入当前选中的数字TempValue2[nFlag2] = 4;//后移索引nFlag2++;//显示内容的拼接wcscat(szTextBuffer, L"4");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}break;}case IDC_BUTTON_5:{//第一个数if (nSwitch == 0){//在数组中存入当前选中的数字TempValue1[nFlag1] = 5;//后移索引nFlag1++;//显示内容的拼接wcscat(szTextBuffer, L"5");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}//第二个数else{//在数组中存入当前选中的数字TempValue2[nFlag2] = 5;//后移索引nFlag2++;//显示内容的拼接wcscat(szTextBuffer, L"5");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}break;}case IDC_BUTTON_6:{//第一个数if (nSwitch == 0){//在数组中存入当前选中的数字TempValue1[nFlag1] = 6;//后移索引nFlag1++;//显示内容的拼接wcscat(szTextBuffer, L"6");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}//第二个数else{//在数组中存入当前选中的数字TempValue2[nFlag2] = 6;//后移索引nFlag2++;//显示内容的拼接wcscat(szTextBuffer, L"6");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}break;}case IDC_BUTTON_7:{//第一个数if (nSwitch == 0){//在数组中存入当前选中的数字TempValue1[nFlag1] = 7;//后移索引nFlag1++;//显示内容的拼接wcscat(szTextBuffer, L"7");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}//第二个数else{//在数组中存入当前选中的数字TempValue2[nFlag2] = 7;//后移索引nFlag2++;//显示内容的拼接wcscat(szTextBuffer, L"7");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}break;}case IDC_BUTTON_8:{//第一个数if (nSwitch == 0){//在数组中存入当前选中的数字TempValue1[nFlag1] = 8;//后移索引nFlag1++;//显示内容的拼接wcscat(szTextBuffer, L"8");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}//第二个数else{//在数组中存入当前选中的数字TempValue2[nFlag2] = 8;//后移索引nFlag2++;//显示内容的拼接wcscat(szTextBuffer, L"8");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}break;}case IDC_BUTTON_9:{//第一个数if (nSwitch == 0){//在数组中存入当前选中的数字TempValue1[nFlag1] = 9;//后移索引nFlag1++;//显示内容的拼接wcscat(szTextBuffer, L"9");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);}//第二个数else{//在数组中存入当前选中的数字TempValue2[nFlag2] = 9;//后移索引nFlag2++;//显示内容的拼接wcscat(szTextBuffer, L"9");//发送消息显示到EDITSetWindowText(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 * 100int 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 * 100Value2 += (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"+");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);break;}case IDC_BUTTON_SUB:{//标记运算符nOperation = OPERATION_SUB;//切换输入的数字nSwitch = 1;//显示内容的拼接wcscat(szTextBuffer, L"-");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);break;}case IDC_BUTTON_IMUL:{//标记运算符nOperation = OPERATION_IMUL;//切换输入的数字nSwitch = 1;//显示内容的拼接wcscat(szTextBuffer, L"*");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);break;}case IDC_BUTTON_IDIV:{//标记运算符nOperation = OPERATION_IDIV;//切换输入的数字nSwitch = 1;//显示内容的拼接wcscat(szTextBuffer, L"/");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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");//发送消息显示到EDITSetWindowText(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 * 100Value1 += (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 * 100Value2 += (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"+");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);break;}case IDC_BUTTON_SUB:{//标记运算符nOperation = OPERATION_SUB;//切换输入的数字nSwitch = 1;//显示内容的拼接wcscat(szTextBuffer, L"-");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);break;}case IDC_BUTTON_IMUL:{//标记运算符nOperation = OPERATION_IMUL;//切换输入的数字nSwitch = 1;//显示内容的拼接wcscat(szTextBuffer, L"*");//发送消息显示到EDITSetWindowText(hEditText, szTextBuffer);break;}case IDC_BUTTON_IDIV:{//标记运算符nOperation = OPERATION_IDIV;//切换输入的数字nSwitch = 1;//显示内容的拼接wcscat(szTextBuffer, L"/");//发送消息显示到EDITSetWindowText(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;
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 数码管进阶设计验证
  • 3.3-CoroutineScope/CoroutineContext:从挂起函数里获取 CoroutineContext
  • 总结:Python语法
  • [JAVA]初识线程池及其基本应用
  • leetcode406:根据身高重建队列
  • FastDFS的安装(分布式项目中的图片管理)
  • 《晶核》服务器架构——第二篇
  • fastap之使用 contextvars 实现上下文变量
  • Ps:首选项 - 常规
  • Unity+Addressable
  • 15.CentOS7升级内核
  • Android 关于设备定屏/黑屏/冻屏/ANR那些事
  • 【北京仁爱堂】脖子歪斜,拉扯疼痛怎么办?规律的生活让痉挛性斜颈的恢复事半功倍!
  • 微信小程序登陆
  • 【精选】基于springboot个人理财APP(源码+设计+辅导)
  • [js高手之路]搞清楚面向对象,必须要理解对象在创建过程中的内存表示
  • Apache Zeppelin在Apache Trafodion上的可视化
  • HashMap ConcurrentHashMap
  • iOS筛选菜单、分段选择器、导航栏、悬浮窗、转场动画、启动视频等源码
  • JavaScript设计模式系列一:工厂模式
  • JAVA多线程机制解析-volatilesynchronized
  • java概述
  • js算法-归并排序(merge_sort)
  • QQ浏览器x5内核的兼容性问题
  • React-flux杂记
  • Redis 中的布隆过滤器
  • Travix是如何部署应用程序到Kubernetes上的
  • 关于List、List?、ListObject的区别
  • 基于Android乐音识别(2)
  • 前嗅ForeSpider采集配置界面介绍
  • 使用Gradle第一次构建Java程序
  • 通过来模仿稀土掘金个人页面的布局来学习使用CoordinatorLayout
  • 学习JavaScript数据结构与算法 — 树
  • #FPGA(基础知识)
  • $.ajax()方法详解
  • (1)(1.13) SiK无线电高级配置(五)
  • (145)光线追踪距离场柔和阴影
  • (草履虫都可以看懂的)PyQt子窗口向主窗口传递参数,主窗口接收子窗口信号、参数。
  • (附源码)ssm教师工作量核算统计系统 毕业设计 162307
  • (附源码)ssm考生评分系统 毕业设计 071114
  • (附源码)计算机毕业设计SSM教师教学质量评价系统
  • (三)uboot源码分析
  • (十)c52学习之旅-定时器实验
  • (十二)python网络爬虫(理论+实战)——实战:使用BeautfulSoup解析baidu热搜新闻数据
  • (一)十分简易快速 自己训练样本 opencv级联haar分类器 车牌识别
  • (原創) 如何將struct塞進vector? (C/C++) (STL)
  • (转)ORM
  • (转)微软牛津计划介绍——屌爆了的自然数据处理解决方案(人脸/语音识别,计算机视觉与语言理解)...
  • ***微信公众号支付+微信H5支付+微信扫码支付+小程序支付+APP微信支付解决方案总结...
  • .NET CORE使用Redis分布式锁续命(续期)问题
  • .net 程序发生了一个不可捕获的异常
  • .net 调用php,php 调用.net com组件 --
  • .NET/C# 推荐一个我设计的缓存类型(适合缓存反射等耗性能的操作,附用法)
  • .Net6使用WebSocket与前端进行通信
  • @Pointcut 使用