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

2024-08-25 01:04

本文主要是介绍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;
}

这篇关于Windows SDK(九)登录框和计算器练习的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1104116

相关文章

Redis在windows环境下如何启动

《Redis在windows环境下如何启动》:本文主要介绍Redis在windows环境下如何启动的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Redis在Windows环境下启动1.在redis的安装目录下2.输入·redis-server.exe

springboot security验证码的登录实例

《springbootsecurity验证码的登录实例》:本文主要介绍springbootsecurity验证码的登录实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录前言代码示例引入依赖定义验证码生成器定义获取验证码及认证接口测试获取验证码登录总结前言在spring

Windows Server服务器上配置FileZilla后,FTP连接不上?

《WindowsServer服务器上配置FileZilla后,FTP连接不上?》WindowsServer服务器上配置FileZilla后,FTP连接错误和操作超时的问题,应该如何解决?首先,通过... 目录在Windohttp://www.chinasem.cnws防火墙开启的情况下,遇到的错误如下:无法与

最新Spring Security实战教程之表单登录定制到处理逻辑的深度改造(最新推荐)

《最新SpringSecurity实战教程之表单登录定制到处理逻辑的深度改造(最新推荐)》本章节介绍了如何通过SpringSecurity实现从配置自定义登录页面、表单登录处理逻辑的配置,并简单模拟... 目录前言改造准备开始登录页改造自定义用户名密码登陆成功失败跳转问题自定义登出前后端分离适配方案结语前言

Python解析器安装指南分享(Mac/Windows/Linux)

《Python解析器安装指南分享(Mac/Windows/Linux)》:本文主要介绍Python解析器安装指南(Mac/Windows/Linux),具有很好的参考价值,希望对大家有所帮助,如有... 目NMNkN录1js. 安装包下载1.1 python 下载官网2.核心安装方式3. MACOS 系统安

Windows系统下如何查找JDK的安装路径

《Windows系统下如何查找JDK的安装路径》:本文主要介绍Windows系统下如何查找JDK的安装路径,文中介绍了三种方法,分别是通过命令行检查、使用verbose选项查找jre目录、以及查看... 目录一、确认是否安装了JDK二、查找路径三、另外一种方式如果很久之前安装了JDK,或者在别人的电脑上,想

Windows命令之tasklist命令用法详解(Windows查看进程)

《Windows命令之tasklist命令用法详解(Windows查看进程)》tasklist命令显示本地计算机或远程计算机上当前正在运行的进程列表,命令结合筛选器一起使用,可以按照我们的需求进行过滤... 目录命令帮助1、基本使用2、执行原理2.1、tasklist命令无法使用3、筛选器3.1、根据PID

Python中Windows和macOS文件路径格式不一致的解决方法

《Python中Windows和macOS文件路径格式不一致的解决方法》在Python中,Windows和macOS的文件路径字符串格式不一致主要体现在路径分隔符上,这种差异可能导致跨平台代码在处理文... 目录方法 1:使用 os.path 模块方法 2:使用 pathlib 模块(推荐)方法 3:统一使

Windows server服务器使用blat命令行发送邮件

《Windowsserver服务器使用blat命令行发送邮件》在linux平台的命令行下可以使用mail命令来发送邮件,windows平台没有内置的命令,但可以使用开源的blat,其官方主页为ht... 目录下载blatBAT命令行示例备注总结在linux平台的命令行下可以使用mail命令来发送邮件,Win

Windows环境下安装达梦数据库的完整步骤

《Windows环境下安装达梦数据库的完整步骤》达梦数据库的安装大致分为Windows和Linux版本,本文将以dm8企业版Windows_64位环境为例,为大家介绍一下达梦数据库的具体安装步骤吧... 目录环境介绍1 下载解压安装包2 根据安装手册安装2.1 选择语言 时区2.2 安装向导2.3 接受协议