一、安裝:MFC應用(如果沒有的話)

二、打開:VS2019,并創建新專案-MFC應用,在應用程式型別處選擇基于對話框,專案名在本教程中是calculator_project

三、運行:沒有報錯,觀察解決方案中本專案下有五個檔案夾:參考,外部依賴項,頭檔案,源檔案,資源檔案,我們主要操作的是后面三個
四、先對資源檔案進行操作,打開工具箱,拉入button與edit control,其實會操作這兩個就基本夠用
(1)設定視窗屬性中的描述文字與樣式,以及按鈕的描述文字,基本上資源檔案這里前端就做完了,下面是連接后端代碼實作
(2)依次雙擊button控制,生成對應的點擊函式(可以垂直劃分界面不用整天切換檔案,方法是:右擊當前正顯示的檔案標題,新建垂直檔案組)
(3)右擊edit control,添加變數,填寫變數名并確定,本教程中是m_Edit1,以后用m_Edit1.SetWindowText(cstr);與m_Edit1.GetWindowTextW(cs);來讀寫其文本
【注意傳入傳出的引數是CString型別,一般習慣C++是用string型別的,所以要轉換型別,轉換方法見下面的代碼,可以只看第15與第16個按鈕的實作代碼】

五、修改源檔案calculator_projectDlg.cpp與頭檔案calculator_projectDlg.h
cpp檔案中我們需要實作16個按鈕的代碼,其他是專案自帶和上一步創建前后端連接時自動生成的
h檔案中我們需要匯入#include "cj_test.h",其他是專案自帶和上一步創建前后端連接時自動生成的
// calculator_projectDlg.cpp: 實作檔案
//
#include "pch.h"
#include "framework.h"
#include "calculator_project.h"
#include "calculator_projectDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CcalculatorprojectDlg 對話框
CcalculatorprojectDlg::CcalculatorprojectDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_CALCULATOR_PROJECT_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CcalculatorprojectDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT1, m_Edit1);
}
BEGIN_MESSAGE_MAP(CcalculatorprojectDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &CcalculatorprojectDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CcalculatorprojectDlg::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3, &CcalculatorprojectDlg::OnBnClickedButton3)
ON_BN_CLICKED(IDC_BUTTON4, &CcalculatorprojectDlg::OnBnClickedButton4)
ON_BN_CLICKED(IDC_BUTTON5, &CcalculatorprojectDlg::OnBnClickedButton5)
ON_BN_CLICKED(IDC_BUTTON6, &CcalculatorprojectDlg::OnBnClickedButton6)
ON_BN_CLICKED(IDC_BUTTON7, &CcalculatorprojectDlg::OnBnClickedButton7)
ON_BN_CLICKED(IDC_BUTTON8, &CcalculatorprojectDlg::OnBnClickedButton8)
ON_BN_CLICKED(IDC_BUTTON9, &CcalculatorprojectDlg::OnBnClickedButton9)
ON_BN_CLICKED(IDC_BUTTON10, &CcalculatorprojectDlg::OnBnClickedButton10)
ON_BN_CLICKED(IDC_BUTTON11, &CcalculatorprojectDlg::OnBnClickedButton11)
ON_BN_CLICKED(IDC_BUTTON12, &CcalculatorprojectDlg::OnBnClickedButton12)
ON_BN_CLICKED(IDC_BUTTON13, &CcalculatorprojectDlg::OnBnClickedButton13)
ON_BN_CLICKED(IDC_BUTTON14, &CcalculatorprojectDlg::OnBnClickedButton14)
ON_BN_CLICKED(IDC_BUTTON15, &CcalculatorprojectDlg::OnBnClickedButton15)
ON_BN_CLICKED(IDC_BUTTON16, &CcalculatorprojectDlg::OnBnClickedButton16)
END_MESSAGE_MAP()
// CcalculatorprojectDlg 訊息處理程式
BOOL CcalculatorprojectDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 設定此對話框的圖示, 當應用程式主視窗不是對話框時,框架將自動
// 執行此操作
SetIcon(m_hIcon, TRUE); // 設定大圖示
SetIcon(m_hIcon, FALSE); // 設定小圖示
// TODO: 在此添加額外的初始化代碼
return TRUE; // 除非將焦點設定到控制元件,否則回傳 TRUE
}
// 如果向對話框添加最小化按鈕,則需要下面的代碼
// 來繪制該圖示, 對于使用檔案/視圖模型的 MFC 應用程式,
// 這將由框架自動完成,
void CcalculatorprojectDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于繪制的設備背景關系
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使圖示在作業區矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 繪制圖示
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//當用戶拖動最小化視窗時系統呼叫此函式取得游標
//顯示,
HCURSOR CcalculatorprojectDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CcalculatorprojectDlg::OnBnClickedButton1()
{
// TODO: 在此添加控制元件通知處理程式代碼
s += "1";
CString cstr(s.c_str());
m_Edit1.SetWindowText(cstr);
}
void CcalculatorprojectDlg::OnBnClickedButton2()
{
// TODO: 在此添加控制元件通知處理程式代碼
s += "2";
CString cstr(s.c_str());
m_Edit1.SetWindowText(cstr);
}
void CcalculatorprojectDlg::OnBnClickedButton3()
{
// TODO: 在此添加控制元件通知處理程式代碼
s += "3";
CString cstr(s.c_str());
m_Edit1.SetWindowText(cstr);
}
void CcalculatorprojectDlg::OnBnClickedButton4()
{
// TODO: 在此添加控制元件通知處理程式代碼
s += "4";
CString cstr(s.c_str());
m_Edit1.SetWindowText(cstr);
}
void CcalculatorprojectDlg::OnBnClickedButton5()
{
// TODO: 在此添加控制元件通知處理程式代碼
s += "5";
CString cstr(s.c_str());
m_Edit1.SetWindowText(cstr);
}
void CcalculatorprojectDlg::OnBnClickedButton6()
{
// TODO: 在此添加控制元件通知處理程式代碼
s += "6";
CString cstr(s.c_str());
m_Edit1.SetWindowText(cstr);
}
void CcalculatorprojectDlg::OnBnClickedButton7()
{
// TODO: 在此添加控制元件通知處理程式代碼
s += "7";
CString cstr(s.c_str());
m_Edit1.SetWindowText(cstr);
}
void CcalculatorprojectDlg::OnBnClickedButton8()
{
// TODO: 在此添加控制元件通知處理程式代碼
s += "8";
CString cstr(s.c_str());
m_Edit1.SetWindowText(cstr);
}
void CcalculatorprojectDlg::OnBnClickedButton9()
{
// TODO: 在此添加控制元件通知處理程式代碼
s += "9";
CString cstr(s.c_str());
m_Edit1.SetWindowText(cstr);
}
void CcalculatorprojectDlg::OnBnClickedButton10()
{
// TODO: 在此添加控制元件通知處理程式代碼
s += "0";
CString cstr(s.c_str());
m_Edit1.SetWindowText(cstr);
}
void CcalculatorprojectDlg::OnBnClickedButton11()
{
// TODO: 在此添加控制元件通知處理程式代碼
s += "+";
CString cstr(s.c_str());
m_Edit1.SetWindowText(cstr);
}
void CcalculatorprojectDlg::OnBnClickedButton12()
{
// TODO: 在此添加控制元件通知處理程式代碼
s += "-";
CString cstr(s.c_str());
m_Edit1.SetWindowText(cstr);
}
void CcalculatorprojectDlg::OnBnClickedButton13()
{
// TODO: 在此添加控制元件通知處理程式代碼
s += "*";
CString cstr(s.c_str());
m_Edit1.SetWindowText(cstr);
}
void CcalculatorprojectDlg::OnBnClickedButton14()
{
// TODO: 在此添加控制元件通知處理程式代碼
s += "/";
CString cstr(s.c_str());
m_Edit1.SetWindowText(cstr);
}
void CcalculatorprojectDlg::OnBnClickedButton15()
{
// TODO: 在此添加控制元件通知處理程式代碼
s = "";
CString cstr(s.c_str());
m_Edit1.SetWindowText(cstr);
}
void CcalculatorprojectDlg::OnBnClickedButton16()
{
// TODO: 在此添加控制元件通知處理程式代碼
CString cs;
m_Edit1.GetWindowTextW(cs);
string s_get(CW2A(cs.GetString()));
s = cal(s_get);
CString cstr(s.c_str());
m_Edit1.SetWindowText(cstr);
}
// calculator_projectDlg.h: 頭檔案
//
#pragma once
#include "cj_test.h"
// CcalculatorprojectDlg 對話框
class CcalculatorprojectDlg : public CDialogEx
{
// 構造
public:
CcalculatorprojectDlg(CWnd* pParent = nullptr); // 標準建構式
// 對話框資料
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_CALCULATOR_PROJECT_DIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 實作
protected:
HICON m_hIcon;
// 生成的訊息映射函式
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
string s;
CEdit m_Edit1;
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButton2();
afx_msg void OnBnClickedButton3();
afx_msg void OnBnClickedButton6();
afx_msg void OnBnClickedButton5();
afx_msg void OnBnClickedButton4();
afx_msg void OnBnClickedButton7();
afx_msg void OnBnClickedButton8();
afx_msg void OnBnClickedButton9();
afx_msg void OnBnClickedButton10();
afx_msg void OnBnClickedButton11();
afx_msg void OnBnClickedButton12();
afx_msg void OnBnClickedButton13();
afx_msg void OnBnClickedButton14();
afx_msg void OnBnClickedButton15();
afx_msg void OnBnClickedButton16();
};
六、新建源檔案calculator_projectDlg.cpp與頭檔案calculator_projectDlg.h
這兩個檔案是為前端點擊按鈕后進行的計算功能提供介面,本教程提供了test與cal兩個介面
#include "cj_test.h"
string test(string s_get) {
string s = s_get + s_get;
return s;
}
stack<char> s;
stack<int> ss;
string cal(string s_get) {
int len1, len2, len, i, j;
string str1, str2, str3;
str1 = s_get;
len1 = str1.length();
str2.clear();
str3.clear();
for (i = 0; i < len1; i++) {
if (str1[i] >= '0' && str1[i] <= '9')
str2.push_back(str1[i]);
else {
if (s.size() == 0 || str1[i] == '(')
s.push(str1[i]);
else {
char tmp1 = s.top();
if (str1[i] == ')') {
len = s.size();
while (len) {
char tmp = s.top();
s.pop();
if (tmp == '(')
break;
else
str2.push_back(tmp);
len--;
}
}
else {
if (tmp1 == '*' || tmp1 == '/') {
if (str1[i] == '*' || str1[i] == '/')
s.push(str1[i]);
else {
len = s.size();
while (len) {
char tmp = s.top();
str2.push_back(tmp);
s.pop();
len--;
}
s.push(str1[i]);
}
}
else {
s.push(str1[i]);
}
}
}
}
}
if (s.size() != 0) {
len = s.size();
while (len) {
char tmp = s.top();
str2.push_back(tmp);
s.pop();
len--;
}
}
//由后綴運算式計算結果
int temp1, temp2, temp3;
len2 = str2.length();
for (i = 0; i < len2; i++) {
if (str2[i] >= '0' && str2[i] <= '9') {
int t = str2[i] - 48;
ss.push(t);
}
else {
temp1 = ss.top();
ss.pop();
temp2 = ss.top();
ss.pop();
if (str2[i] == '+')
temp3 = temp2 + temp1;
else if (str2[i] == '-')
temp3 = temp2 - temp1;
else if (str2[i] == '*')
temp3 = temp2 * temp1;
else if (str2[i] == '/')
temp3 = temp2 / temp1;
ss.push(temp3);
}
}
stringstream xxx;
xxx << ss.top();
xxx >> str3;
//return str2+":"+str3;
return str3;
}
#pragma once
#include<iostream>
#include<cstdio>
#include<string>
#include<stack>
#include <sstream>
using namespace std;
string test(string s_get);
string cal(string s_get);
七、補充
如果報錯:遇到意外的檔案結尾
解決方法:在出錯的檔案上右鍵選屬性,配置屬性 -> C/C++ ->預編譯頭,在“創建/使用預編譯頭”這里設定不使用預編譯頭
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/257368.html
標籤:其他
