MFC入門(三)


1 新建項目

1.1 根據向導創建項目

1.2 添加預處理指令

由於微軟在VS2013中不建議再使用C/C++的傳統庫函數scanf,strcpy,sprintf等,所以直接使用這些庫函數會提示C4996錯誤。

VS建議采用帶_s的函數,如scanf_s、strcpy_s,但這些並不是標准C/C++函數。要想繼續使用此函數,需要添加 _CRT_SECURE_NO_WARNINGS這個預定義。

在項目 -> 屬性 -> C/C++->預處理器 -> 預處理器定中添加 _CRT_SECURE_NO_WARNINGS:

1.3 設置窗口屬性

1) 設置圖標

打開資源視圖,添加本地ICO圖標,在res文件夾中添加我們提前准備的圖標資源:

在資源的屬性中修改ID(IDI_ICON_WIN)

 

CMainFrame的 OnCreate()中添加如下代碼:

 

//設置圖標,IDI_ICON_WIN為圖標資源ID,此為WINAPI函數
SetClassLong(m_hWnd, GCL_HICON, (LONG)AfxGetApp()->LoadIconW(IDI_ICON_WIN));

2) 設置窗口大小和居中顯示

還是在CMainFrame的OnCreate()中接着寫代碼:

    //設置窗口的位置和大小:CWnd::MoveWindow
    //0, 0, 起點坐標x和y
    //800, 500, 窗口寬度和高度
    MoveWindow(0, 0, 800, 500);

    //將窗口移動到屏幕中央,CWnd::CenterWindow
    CenterWindow();

3) 設置窗口標題

在CSaleSystemDoc文檔類中的OnNewDocument()函數中添加如下代碼:

 

//設置窗口標題,CDocument::SetTitle
    SetTitle(TEXT("銷售管理系統"));

設置標題 分左右側標題Doc --OnNewDocument中設置右側

                                     Frame中OnCreate中設置左側

 

程序運行效果圖如下:

 

 

 

文件處理

2.1 文件內容格式

登陸用戶信息:

商品信息:

 

2.2 設計文件處理類CInfoFile

1) 添加文件處理類CInfoFile

 

登陸對話框

3.1 ui設計

1)添加對話框資源(ID修改為DIALOG_LOGIN),添加所需控件:

 

 

2)選中對話框 -> 右擊 -> 添加類 -> 類名:CLoginDlg

 

 

 

3) 根據需求,控件關聯所需變量

 

用戶名編輯區關聯CString m_user,密碼登陸框關聯CString m_pwd。

3.2 功能實現

1)在對話框類中,重寫 OnInitDialog 函數,進行初始化,設置一些默認登錄信息。

    m_user = TEXT("斧頭幫幫主");    //用戶名
    m_pwd = TEXT("123456");//密碼
    UpdateData(FALSE); //內容更新到對應的控件

2)登陸窗口的創建

在應用程序類CSaleSystemApp的InitInstance() 里面的APP 創建之前創建登陸對話框:

 

CLoginDlg dlg;    //創建登陸對話框,需要頭文件#include "LoginDlg.h"
dlg.DoModal();    //以模態方式運行

3)登陸按鈕功能實現

void CLoginDlg::OnBnClickedButton1()
{
    // TODO:  在此添加控件通知處理程序代碼

    UpdateData(TRUE); //更新控件的數據到對應的變量

    CInfoFile file; //創建操作文件類對象,需要頭文件#include "InfoFile.h" 
    CString user, pwd;

    //讀取配置文件,獲取用戶名密碼,參數為引用傳遞
    file.ReadLogin(user, pwd);

    if (m_user == user)//用戶名相等
    {
        if (m_pwd != pwd)
        {
            MessageBox(_T("密碼錯誤"));
            m_user.Empty(); //清空
            m_pwd.Empty();
        }
        else
        {
            CDialogEx::OnOK();
        }
    }
    else
    {
        MessageBox(_T("用戶名不存在"));
        m_user.Empty();
        m_pwd.Empty();
    }
}

4)取消按鈕功能實現

//取消按鈕功能實現
void CLoginDlg::OnBnClickedButton2()
{
    // TODO:  在此添加控件通知處理程序代碼
    exit(0);    //結束整個程序
}

5)右上角關閉按鈕功能實現

選中對話框模板 -> 右擊 -> 屬性 -> 消息 -> WM_CLOSE

 

//關閉按鈕
void CLoginDlg::OnClose()
{
    // TODO:  在此添加消息處理程序代碼和/或調用默認值
    exit(0);    //結束整個程序

    CDialogEx::OnClose();
}

6)編輯區回車鍵關閉對話框問題解決

 

void CLoginDlg::OnOK()
{
    // TODO: 在此添加專用代碼和/或調用基類

    //CDialogEx::OnOK();
}

靜態拆分窗口

4.1 自定義MFC視圖類

自定義兩個類:CSelectView和CDispalyView(它的基類必須是視圖類)。

CSelectView繼承於CTreeView,CDispalyView繼承於CFormView。

 

4.2 通過CSplitterWnd類拆分窗口

1) CMainFrame類中,聲明CSplitterWnd類型的對象:

private:
    CSplitterWnd m_spliter; // 切分窗口類對象

2) 重寫框架類CMainFrame的OnCreateClient函數

 

OnCreateClient()函數的返回值改為Return TRUE:

 

靜態拆分實現代碼如下:

 

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
    // TODO:  在此添加專用代碼和/或調用基類

    // 靜態拆分窗口,1行2列,CSplitterWnd::CreateStatic
    m_spliter.CreateStatic(this, 1, 2);

    // 創建視圖:CSplitterWnd::CreateView
    //0, 0 : 放在第0行第0列的位置
    //RUNTIME_CLASS(CSelectView) :需要頭文件#include "SelectView.h", CSelectView在SelectView.h中聲明
    // CSize(250, 500):指定視圖寬度和高度
    //pContext : 為OnCreateClient()最后一個形參
    m_spliter.CreateView(0, 0, RUNTIME_CLASS(CSelectView), CSize(200, 500), pContext);

    //0, 1: 放在第0行第1列的位置
    //CDispalyView,需要頭文件#include "DispalyView.h"
    m_spliter.CreateView(0, 1, RUNTIME_CLASS(CDispalyView), CSize(600, 500), pContext);

    //return CFrameWnd::OnCreateClient(lpcs, pContext);
    return TRUE;
}

程序運行效果圖如下:

 

 

5 樹視圖功能實現

5.1 添加功能節點

1) 加載圖標資源

圖標資源ID改為:IDI_ICON_RE

 

2) CSelectView類中聲明相應變量:

 

CTreeCtrl *m_treeCtrl;    //樹控件
CImageList m_imageList;    //圖標列表

3) 重寫CSelectView的OnInitUpdate函數

 

void CSelectView::OnInitialUpdate()
{
    CTreeView::OnInitialUpdate();

    // TODO:  在此添加專用代碼和/或調用基類
}

4) CSelectView的OnInitUpdate函數中,完成初始化功能

void CSelectView::OnInitialUpdate()
{
    CTreeView::OnInitialUpdate();

    // TODO:  在此添加專用代碼和/或調用基類

    //圖標資源的加載 CWinApp::LoadIcon
//IDI_ICON_RE為圖標資源ID
    HICON icon = AfxGetApp()->LoadIconW(IDI_ICON_RE); 

    //圖片列表的創建 CImageList::Create
    //30, 30:指定圖標的寬度和高度
    //ILC_COLOR32:圖標格式
    //1, 1:有多少圖標就寫多少
    m_imageList.Create(30, 30, ILC_COLOR32, 1, 1);
//圖片列表追加圖標 CImageList::Add
    m_imageList.Add(icon);

    //獲取數視圖中的樹控件 CTreeView::GetTreeCtrl
    m_treeCtrl = &GetTreeCtrl();

    //數控件設置圖片列表 CTreeCtrl::SetImageList
    m_treeCtrl->SetImageList(&m_imageList, TVSIL_NORMAL);

    //樹控件設置節點 CTreeCtrl::InsertItem
    m_treeCtrl->InsertItem(TEXT("個人信息"), 0, 0, NULL);
    m_treeCtrl->InsertItem(TEXT("銷售管理"), 0, 0, NULL);
    m_treeCtrl->InsertItem(TEXT("庫存信息"), 0, 0, NULL);
    m_treeCtrl->InsertItem(TEXT("庫存添加"), 0, 0, NULL);
    m_treeCtrl->InsertItem(TEXT("庫存刪除"), 0, 0, NULL);
}

程序運行效果圖如下:

 

5.2 功能節點相應消息處理

 

void CSelectView::OnTvnSelchanged(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
    // TODO:  在此添加控件通知處理程序代碼
    *pResult = 0;
}

在上面函數,實現獲取當前節點選中內容:

 

void CSelectView::OnTvnSelchanged(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
    // TODO:  在此添加控件通知處理程序代碼
    *pResult = 0;

    //獲取當前節點選中項目 CTreeCtrl::GetSelectedItem
    HTREEITEM item = m_treeCtrl->GetSelectedItem();

    //獲取選中項的文本內容 CTreeCtrl::GetItemText
    CString str = m_treeCtrl->GetItemText(item);
    //MessageBox(str);

    if (str == TEXT("個人信息"))
    {
    }
    else if (str == TEXT("銷售管理"))
{
    }
    else if (str == TEXT("庫存信息"))
    {
    }
    else if (str == TEXT("庫存增加"))
    {
    }
    else if (str == TEXT("庫存刪除"))
    {
    }
}

個人信息管理窗口

6.1 ui設計

1)添加對話框資源(ID修改為DIALOG_USER),添加所需控件:

 

在窗口屬性中,Border改為None,Style改為Child:

 

2)選中對話框 -> 右擊 -> 添加類 -> 類名:CUserDlg,基類選擇CFormView

 

3)根據需求,控件關聯所需變量

身份編輯區關聯CString m_user,用戶名編輯框關聯CString m_name,

新密碼編輯框關聯CString m_newPwd,確定密碼編輯框關聯CString m_surePwd。

 

6.2 功能實現

1) 在對話框類中,重寫 OnInitDialog 函數,進行初始化。

 

void CUserDlg::OnInitialUpdate()
{
    CFormView::OnInitialUpdate();

    // TODO:  在此添加專用代碼和/或調用基類
}
void CUserDlg::OnInitialUpdate()
{
    CFormView::OnInitialUpdate();

    // TODO:  在此添加專用代碼和/或調用基類

    CInfoFile file;    //需要頭文件#include "InfoFile.h"
    CString name, pwd;
    file.ReadLogin(name, pwd); //讀取文件的用戶名和密碼

    //初始化個人信息
    m_user = TEXT("銷售員");    //身份
    m_name = name;    //用戶名

    UpdateData(FALSE); //把數據更新到控件上
}

2) 確定修改密碼按鈕功能實現

void CUserDlg::OnBnClickedButton1()
{
    // TODO:  在此添加控件通知處理程序代碼

    UpdateData(TRUE);//更新控件內容到對應的變量中

    if (m_newPwd.IsEmpty() || m_surePwd.IsEmpty())
    {
        MessageBox(TEXT("輸入密碼不能為空"));
        return;
    }

    if (m_newPwd != m_surePwd)
    {
        MessageBox(TEXT("輸入密碼和確定密碼不相等"));
        return;
    }

    CInfoFile file;    //需要頭文件#include "InfoFile.h"
    CString name, pwd;
    file.ReadLogin(name, pwd); //讀取文件的用戶名和密碼

    if (m_surePwd == pwd)
    {
        MessageBox(TEXT("輸入密碼和舊密碼相等"));
        return;
    }

    //把用戶名和密碼的CString類型轉為char *
    char *tmpName, *tmpPwd;
    //用戶名
    CStringA tmp1;
    tmp1 = name;
    tmpName = tmp1.GetBuffer();
    //密碼
    CStringA tmp2;
    tmp2 = m_surePwd;
    tmpPwd = tmp2.GetBuffer();

    file.WritePwd(tmpName, tmpPwd); //修改密碼

    MessageBox(TEXT("密碼修改成功"));

    //輸入框內容清空
    m_surePwd.Empty();
    m_newPwd.Empty();
    UpdateData(FALSE); //把數據更新到控件上
}

3) 取消按鈕功能實現

void CUserDlg::OnBnClickedButton3()
{
    // TODO:  在此添加控件通知處理程序代碼

    //輸入框內容清空
    m_surePwd.Empty();
    m_newPwd.Empty();
    UpdateData(FALSE); //把數據更新到控件上
}

界面掛載

1. 聲明自定義的消息

2. 自定義消息,寫到分界宏

3. 聲明OnMyChange方法

4. SelectView中發送自定義消息::postMessage

5. 根據不同的參數不同界面的掛載

6. 界面具體掛載

7.1 自定義信息發送

1) CMainFrame 框架類中,添加自定義消息宏

 

//WM_USER 是用戶自定義消息的一個起始值
//WM_USER+100是為了區分系統消息和用戶消息,避免沖突
#define NM_A    (WM_USER + 100)
#define NM_B    (WM_USER + 101)
#define NM_C    (WM_USER + 102)
#define NM_D    (WM_USER + 103)
#define NM_E    (WM_USER + 104)

2) CMainFrame框架類中添加自定義消息處理函數:

//自定義消息處理函數
afx_msg LRESULT OnMyChange(WPARAM wParam, LPARAM lParam);

3)對應的.cpp定義其函數

LRESULT CMainFrame::OnMyChange(WPARAM wParam, LPARAM lParam)
{
}

4)在CMainFrame框架類BEGIN_MESSAGE_MAP和END_MESSAGE_MAP之間添加自定義消息入口,與自定義消息處理函數綁定。

 

    //ON_MESSAGE響應的是自定義消息
    //產生NM_X消息,自動調用OnMyChange函數
    ON_MESSAGE(NM_A, OnMyChange)
    ON_MESSAGE(NM_B, OnMyChange)
    ON_MESSAGE(NM_C, OnMyChange)
    ON_MESSAGE(NM_D, OnMyChange)
    ON_MESSAGE(NM_E, OnMyChange)

5) 發送自定義信號

CSelectView的OnTvnSelchanged函數中,發送自定義信號:

 

if (str == TEXT("個人信息"))
{
//需要包含框架類頭文件#include "MainFrm.h" 
    //CWnd::PostMessage 將一個消息放入窗口的消息隊列
    //AfxGetMainWnd():框架窗口對象的指針
    //AfxGetMainWnd()->GetSafeHwnd():獲取返回窗口的句柄,CWnd::GetSafeHwnd
    //NM_A:發送自定義消息
    //(WPARAM)NM_A:指定了附加的消息信息
    //(LPARAM)0:指定了附加的消息信息,此參數這里沒有意義
::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), NM_A, (WPARAM)NM_A, (LPARAM)0);
}
else if (str == TEXT("銷售管理"))
{
::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), NM_B, (WPARAM)NM_B, (LPARAM)0);
}
else if (str == TEXT("庫存信息"))
{
::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), NM_C, (WPARAM)NM_C, (LPARAM)0);
}
else if (str == TEXT("庫存添加"))
{
::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), NM_D, (WPARAM)NM_D, (LPARAM)0);
}
else if (str == TEXT("庫存刪除"))
{
::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), NM_E, (WPARAM)NM_E, (LPARAM)0);
}

7.2 自定義信息處理

CMainFrame框架類OnMyChange函數中處理相應消息

LRESULT CMainFrame::OnMyChange(WPARAM wParam, LPARAM lParam)
{
    switch (wParam)
    {
    case NM_A:
        MessageBox(_T("NM_A"));
        break;
    case NM_B:
        MessageBox(_T("NM_B"));
        break;
    case NM_C:
        MessageBox(_T("NM_C"));
        break;
    case NM_D:
        MessageBox(_T("NM_D"));
        break;
    case NM_E:
        MessageBox(_T("NM_E"));
        break;
    default:
        MessageBox(_T("error"));
    }
    return 0;
}

7.3 界面掛載

如果是NM_A信號,則掛載CUserDlg窗口,后面界面的掛載以此類推。

 

CCreateContext   Context;
switch (wParam)
{
case NM_A:
{
    //CUserDlg類需要包含頭文件#include "UserDlg.h"
    Context.m_pNewViewClass = RUNTIME_CLASS(CUserDlg); 
    Context.m_pCurrentFrame = this;
    Context.m_pLastView = (CFormView *)m_spliter.GetPane(0, 1);
    m_spliter.DeleteView(0, 1);
    m_spliter.CreateView(0, 1, RUNTIME_CLASS(CUserDlg), CSize(600,500), &Context);
    CUserDlg *pNewView = (CUserDlg *)m_spliter.GetPane(0, 1);
    m_spliter.RecalcLayout();
    pNewView->OnInitialUpdate();
    m_spliter.SetActivePane(0, 1);
}
    break;
case NM_B:
//……………

程序運行效果如下:

 

 

 

銷售管理窗口

8.1 ui設計

1)添加對話框資源(ID修改為DIALOG_SELL),添加所需控件。

在窗口屬性中,Border改為None,Style改為Child:

 

 

2)選中對話框 -> 右擊 -> 添加類 -> 類名:CSellDlg,基類選擇CFormView

 

 

3)根據需求,控件關聯所需變量

商品名組合框關聯CComboBox m_combo,單價編輯框關聯int m_price,

個數編輯框關聯int m_num,銷售信息編輯框關聯CString m_sellInfo。

 

 

8.2 界面掛載

CMainFrame類中OnMyChange函數,添加如下代碼:

case NM_B:
    {
        //CSellDlg類需要包含頭文件#include "SellDlg.h"
        Context.m_pNewViewClass = RUNTIME_CLASS(CSellDlg);
        Context.m_pCurrentFrame = this;
        Context.m_pLastView = (CFormView *)m_spliter.GetPane(0, 1);
        m_spliter.DeleteView(0, 1);
        m_spliter.CreateView(0, 1, RUNTIME_CLASS(CSellDlg), CSize(600, 0), &Context);
        CSellDlg *pNewView = (CSellDlg *)m_spliter.GetPane(0, 1);
        m_spliter.RecalcLayout();
        pNewView->OnInitialUpdate();
        m_spliter.SetActivePane(0, 1);
    }
        break;

8.3功能實現

1) 在對話框類中,重寫 OnInitDialog 函數,進行初始化。

void CSellDlg::OnInitialUpdate()
{
    CFormView::OnInitialUpdate();

    // TODO:  在此添加專用代碼和/或調用基類
        //讀取文件,獲取商品名,給組合框添加字符串
    //需要包含#include "InfoFile.h"
    CInfoFile file;
    file.ReadDocline(); //讀取商品信息
    for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
    {
        m_combo.AddString((CString)it->name.c_str());
    }

    file.ls.clear(); //清空list的內容

    //將第一個商品名設為默認選中項
    m_combo.SetCurSel(0);
}

2)處理組合框所需控制事件

 

void CSellDlg::OnCbnSelchangeCombo1()
{
    // TODO:  在此添加控件通知處理程序代碼

    CString text;
    //獲取當前選中項
    int index = m_combo.GetCurSel();
    //獲取當前內容
        m_combo.GetLBText(index, text);

    //需要包含#include "InfoFile.h"
    CInfoFile file;
    file.ReadDocline(); //讀取商品信息
    for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
    {
        if (text == it->name.c_str())
        {
            m_price = it->price;
            m_num = 0;
            UpdateData(FALSE); //內容更新到對應的控件
        }
    }

    file.ls.clear(); //清空list的內容
}

3)購買按鈕功能實現

 

void CSellDlg::OnBnClickedButton1()
{
    // TODO:  在此添加控件通知處理程序代碼
    
    //獲取控件上的內容,更新到對應關聯的變量中
    UpdateData(TRUE);

    if (m_num == 0)
    {
        MessageBox(TEXT("個數不能為0"));
        return;
    }

    CString type;
    //獲取當前選中項
    int index = m_combo.GetCurSel();
    //獲取組合框當前內容
    m_combo.GetLBText(index, type);

    CString str;
    str.Format(_T("商品:%s \r\n單價:%d \r\n個數:%d \r\n總價:%d"), type, m_price, m_num, m_price*m_num);
    m_sellInfo = str; //銷售信息
    UpdateData(FALSE);
    MessageBox(str);


    //需要包含#include "InfoFile.h"
    CInfoFile file;
    file.ReadDocline(); //讀取商品信息
    for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
    {
        if (type == it->name.c_str())
        {
            it->num = it->num - m_num;
        }
    }
    file.WirteDocline(); //更新文件內容

    file.ls.clear(); //清空list的內容

    m_sellInfo.Empty();
    m_num = 0;
    UpdateData(FALSE); //更新到對應的控件
}

 

4)取消按鈕功能實現

 

void CSellDlg::OnBnClickedButton3()
{
    // TODO:  在此添加控件通知處理程序代碼

    m_combo.SetCurSel(0); //選擇第0項目
    m_sellInfo = "";
    m_num = 0;
    OnCbnSelchangeCombo1();
}

 

9 庫存信息窗口

9.1 ui設計

1)添加對話框資源(ID修改為DIALOG_INFO),添加所需控件。

 

 

在窗口屬性中,Border改為None,Style改為Child:

 View 屬性為 Report(報表表模式):

 

2)選中對話框 -> 右擊 -> 添加類 -> 類名:CInfoDlg,基類選擇CFormView

 

3)根據需求,控件關聯所需變量

列表控件關聯CListCtrl m_list:

 

9.2 界面掛載

CMainFrame類中OnMyChange函數,添加如下代碼:

 

case NM_C:
    {
        //CInfoDlg類需要包含頭文件#include "InfoDlg.h"
        Context.m_pNewViewClass = RUNTIME_CLASS(CInfoDlg);
        Context.m_pCurrentFrame = this;
        Context.m_pLastView = (CFormView *)m_spliter.GetPane(0, 1);
        m_spliter.DeleteView(0, 1);
        m_spliter.CreateView(0, 1, RUNTIME_CLASS(CInfoDlg), CSize(600, 0), &Context);
        CInfoDlg *pNewView = (CInfoDlg *)m_spliter.GetPane(0, 1);
        m_spliter.RecalcLayout();
        pNewView->OnInitialUpdate();
        m_spliter.SetActivePane(0, 1);
    }
        break;

程序運行效果圖:

 

 

 

9.3功能實現

在對話框類中,重寫 OnInitDialog 函數,進行商品信息初始化:

 

void CInfoDlg::OnInitialUpdate()
{
    CFormView::OnInitialUpdate();

    // TODO:  在此添加專用代碼和/或調用基類
    // 設置擴展風格
    //LVS_EX_FULLROWSELECT選中整行,LVS_EX_GRIDLINES網格
    m_list.SetExtendedStyle(m_list.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

    // 初始化表頭
    CString field[] = { _T("商品ID"), _T("商品名稱"), _T("商品價格"), _T("庫存數量") };
    for (int i = 0; i < sizeof(field) / sizeof(field[0]); ++i)
    {
        m_list.InsertColumn(i, field[i], LVCFMT_CENTER, 90);
    }

    //需要包含#include "InfoFile.h"
    CInfoFile file;
    file.ReadDocline(); //讀取商品信息

    //添加數據
    int i = 0;
    CString str;
    for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
    {
        str.Format(_T("%d"), it->id);
        m_list.InsertItem(i, str);
        int column = 1;
        m_list.SetItemText(i, column++, (CString)it->name.c_str());
        str.Format(_T("%d"), it->price);
        m_list.SetItemText(i, column++, str);
        str.Format(_T("%d"), it->num);
        m_list.SetItemText(i, column++, str);
        i++;
    }

    
}

10 庫存添加窗口

10.1 ui設計

1)添加對話框資源(ID修改為DIALOG_ADD),添加所需控件。

 

在窗口屬性中,Border改為None,Style改為Child:

 

 

 

2)選中對話框 -> 右擊 -> 添加類 -> 類名:CAddDlg,基類選擇CFormView

 

3)根據需求,控件關聯所需變量

添加個數:

商品組合框關聯CComboBox m_combo,單價編輯框關聯int m_price1

個數編輯框關聯int m_num1

 

添加新產品:

商品組合框關聯CString m_name2,單價編輯框關聯int m_price2

個數編輯框關聯int m_num2

 

 

 

10.2 界面掛載

CMainFrame類中OnMyChange函數,添加如下代碼:

 

10.3功能實現

1)在對話框類中,重寫 OnInitDialog 函數,進行商品信息初始化:

 

void CAddDlg::OnInitialUpdate()
{
    CFormView::OnInitialUpdate();

    // TODO:  在此添加專用代碼和/或調用基類

    //讀取文件,獲取商品名,給組合框添加字符串
    //需要包含#include "InfoFile.h"
    CInfoFile file;
    file.ReadDocline(); //讀取商品信息
    for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
    {
        m_combo.AddString((CString)it->name.c_str());
    }

    file.ls.clear(); //清空list的內容

    //將第一個商品名設為默認選中項
    m_combo.SetCurSel(0);
}

2)處理組合框所需控制事件

 

 

void CAddDlg::OnCbnSelchangeCombo2()
{
    // TODO:  在此添加控件通知處理程序代碼

    CString text;
    //獲取當前選中項
    int index = m_combo.GetCurSel();
    //獲取當前內容
    m_combo.GetLBText(index, text);

    //需要包含#include "InfoFile.h"
    CInfoFile file;
    file.ReadDocline(); //讀取商品信息
    for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
    {
        if (text == it->name.c_str())
        {
            m_price1 = it->price;
            m_num1 = 0;
            UpdateData(FALSE); //內容更新到對應的控件
        }
    }

    file.ls.clear(); //清空list的內容
}

3) 添加個數按鈕實現

 

void CAddDlg::OnBnClickedButton1()
{
    // TODO:  在此添加控件通知處理程序代碼

    //獲取控件上的內容,更新到對應關聯的變量中
    UpdateData(TRUE);

    if (m_num1 == 0)
    {
        MessageBox(TEXT("個數不能為0"));
        return;
    }

    CString type;
    //獲取當前選中項
    int index = m_combo.GetCurSel();
    //獲取組合框當前內容
    m_combo.GetLBText(index, type);

    CString str;
    str.Format(_T("添加了 商品:%s \r\n單價:%d \r\n個數:%d"), type, m_price1, m_num1);
    MessageBox(str);


    //需要包含#include "InfoFile.h"
    CInfoFile file;
    file.ReadDocline(); //讀取商品信息
    for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
    {
        if (type == it->name.c_str())
        {
            it->num +=  m_num1;
        }
    }
    file.WirteDocline(); //更新文件內容

    file.ls.clear(); //清空list的內容

    m_num1 = 0;
    UpdateData(FALSE); //更新到對應的控件
}

4) 添加個數取消按鈕實現

 

void CAddDlg::OnBnClickedButton2()
{
    // TODO:  在此添加控件通知處理程序代碼

    m_combo.SetCurSel(0);
    m_num1 = 0;
    OnCbnSelchangeCombo2();
}

5) 添加新商品按鈕實現

 

void CAddDlg::OnBnClickedButton4()
{
    // TODO:  在此添加控件通知處理程序代碼

    UpdateData(TRUE); //獲取控件內容

    if (m_num2 <= 0 || m_price2 <= 0 || m_name2.IsEmpty())
    {
        MessageBox(TEXT("輸入信息有誤"));
        return;
    }

    //需要包含#include "InfoFile.h"
    CInfoFile file;
    file.ReadDocline(); //讀取商品信息
    file.Addline(m_name2, m_num2, m_price2); //添加商品
    file.WirteDocline(); //寫文件
    file.ls.clear(); //清空list的內容
    MessageBox(_T("添加成功"));

    m_name2.Empty();
    m_num2 = 0;
    m_price2 = 0;
    UpdateData(FALSE);
}

6) 添加新商品取消按鈕實現

 

void CAddDlg::OnBnClickedButton5()
{
    // TODO:  在此添加控件通知處理程序代碼
    m_name2.Empty();
    m_num2 = 0;
    m_price2 = 0;
    UpdateData(FALSE);
}

11 庫存刪除窗口

11.1 ui設計

1)添加對話框資源(ID修改為DIALOG_DEL),添加所需控件。

 

在窗口屬性中,Border改為None,Style改為Child:

 

2)選中對話框 -> 右擊 -> 添加類 -> 類名:CDelDlg,基類選擇CFormView

 

3)根據需求,控件關聯所需變量

商品名組合框關聯CComboBox m_combo,單價編輯框關聯int m_price

個數編輯框關聯int m_num

 

 

11.2 界面掛載

CMainFrame類中OnMyChange函數,添加如下代碼:

case NM_E:
    {
        //CDelDlg類需要包含頭文件#include "DelDlg.h"
        Context.m_pNewViewClass = RUNTIME_CLASS(CDelDlg);
        Context.m_pCurrentFrame = this;
        Context.m_pLastView = (CFormView *)m_spliter.GetPane(0, 1);
        m_spliter.DeleteView(0, 1);
        m_spliter.CreateView(0, 1, RUNTIME_CLASS(CDelDlg), CSize(600, 0), &Context);
        CDelDlg *pNewView = (CDelDlg *)m_spliter.GetPane(0, 1);
        m_spliter.RecalcLayout();
        pNewView->OnInitialUpdate();
        m_spliter.SetActivePane(0, 1);
    }
        break;

程序運行效果圖:

11.3功能實現

1)在對話框類中,重寫 OnInitDialog 函數,進行初始化。

 

void CDelDlg::OnInitialUpdate()
{
    CFormView::OnInitialUpdate();

    //讀取文件,獲取商品名,給組合框添加字符串
    //需要包含#include "InfoFile.h"
    CInfoFile file;
        file.ReadDocline(); //讀取商品信息
    for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
    {
        m_combo.AddString((CString)it->name.c_str());
    }


    //將第一個商品名設為默認選中項
    m_combo.SetCurSel(0);
}

2)處理組合框所需控制事件

void CDelDlg::OnCbnSelchangeCombo1()
{
    // TODO:  在此添加控件通知處理程序代碼

    CString text;
    //獲取當前選中項
    int index = m_combo.GetCurSel();
    //獲取當前內容
    m_combo.GetLBText(index, text);

    //需要包含#include "InfoFile.h"
    CInfoFile file;
        file.ReadDocline(); //讀取商品信息
    for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
    {
        if (text == it->name.c_str())
        {
            m_price = it->price;
            m_num = 0;
            UpdateData(FALSE); //內容更新到對應的控件
        }
    }

    
}

3)確定按鈕功能實現

void CDelDlg::OnBnClickedButton1()
{
    // TODO:  在此添加控件通知處理程序代碼

    //獲取控件上的內容,更新到對應關聯的變量中
    UpdateData(TRUE);

    if (m_num == 0)
    {
        MessageBox(TEXT("個數不能為0"));
        return;
    }

    CString type;
    //獲取當前選中項
    int index = m_combo.GetCurSel();
    //獲取組合框當前內容
    m_combo.GetLBText(index, type);

    CString str;
    str.Format(_T("刪除商品:%s \r\n單價:%d \r\n個數:%d "), type, m_price, m_num);
    MessageBox(str);


    //需要包含#include "InfoFile.h"
    CInfoFile file;
    file.ReadDocline(); //讀取商品信息
        for (list<msg>::iterator it = file.ls.begin(); it != file.ls.end(); it++)
    {
        if (type == it->name.c_str())
        {
            it->num = it->num - m_num;
        }
    }
    file.WirteDocline(); //更新文件內容
    m_num = 0;
    UpdateData(FALSE); //更新到對應的控件
}

4)取消按鈕功能實現

 

void CDelDlg::OnBnClickedButton2()
{
    // TODO:  在此添加控件通知處理程序代碼

    m_combo.SetCurSel(0); //選擇第0項目
    m_num = 0;
    OnCbnSelchangeCombo1();
}

12 菜單欄

1)切換到資源視圖的Menu,刪除掉所有默認(幫助除外):

 

2)右鍵菜單欄項,添加事件處理程序,選擇COMMAND 消息類型,添加至CMainFrame框架類中。

 

3)在事件處理函數中發送自定義信號,其它菜單操作類似。

 

//個人信息菜單
void CMainFrame::On32772()
{
    // TODO:  在此添加命令處理程序代碼
::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), NM_A, (WPARAM)NM_A, (LPARAM)0);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM