MFC按钮点击切换对话框

我是按照老师给的示例程序改的。

  1. 资源视图,右键“插入Dialog”,改一下ID,比如说IDD_DLG_DB。
    这个对话框的属性是:Style-Child,Border-None。
  2. 在对话框上右击,新建类。
    基类:CDialog,声明和定义自己起好风格统一的名字,比如说CDlgDatabase,DlgDatabase.h/.cpp。
  3. 为了实现按钮切换对话框,就要在主对话框上加四个按钮,刚刚新建的IDD_DLG_DB不是主对话框。
    找到主对话框对应的头文件:
// 因为我有很多个要切换的对话框……
#include "resource.h"
#include "DlgImage.h"
#include "DlgCheck.h"
#include "DlgSystem.h"
#include "DlgDatabase.h"
#include "TabControler.h"

#define SHEET_IMAGE 0
#define SHEET_CHECK 1
#define SHEET_SYSTEM 2
#define SHEET_DATABASE 3

// CAreaScanProjectDlg 对话框
class CAreaScanProjectDlg : public CDialogEx
{
// 构造
public:
    CAreaScanProjectDlg(CWnd* pParent = NULL);  // 标准构造函数

// 对话框数据
    enum { IDD = IDD_AREASCANPROJECT_DIALOG };

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持


// 实现
protected:
    HICON m_hIcon;

    // 生成的消息映射函数
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    DECLARE_MESSAGE_MAP()

    public:
        void InitTabParameters();
        void ReduceTabRect( CRect* rect, int left, int right, int top, int bottom );
    public:
        //主要是改这个地方:定义变量
        CTabControler m_TabControler;
        CRect m_Rect_Parameters;
        CStatic m_Tab_Parameters;
        CDlgImage m_Dlg_Image;
        CDlgCheck m_Dlg_Check;
        CDlgSystem m_Dlg_System;
        CDlgDatabase m_Dlg_Database;
        afx_msg void OnBnClickedButtonImage();
        afx_msg void OnBnClickedButtonCheck();
        afx_msg void OnBnClickedButtonSystem();
        afx_msg void OnBnClickedButtonDatabase();
};

// AreaScanProjectDlg.cpp : 实现文件
BEGIN_MESSAGE_MAP(CAreaScanProjectDlg, CDialogEx)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BUTTON_IMAGE, &CAreaScanProjectDlg::OnBnClickedButtonImage)
    ON_BN_CLICKED(IDC_BUTTON_CHECK, &CAreaScanProjectDlg::OnBnClickedButtonCheck)
    ON_BN_CLICKED(IDC_BUTTON_SYSTEM, &CAreaScanProjectDlg::OnBnClickedButtonSystem)
    ON_BN_CLICKED(IDC_BUTTON_DATABASE, &CAreaScanProjectDlg::OnBnClickedButtonDatabase)
END_MESSAGE_MAP()

void CAreaScanProjectDlg::InitTabParameters()
{
    m_Tab_Parameters.GetClientRect(&m_Rect_Parameters);
    ReduceTabRect(&m_Rect_Parameters,1,-1,1,-1);

    m_Dlg_Image.Create(IDD_DLG_IMAGE,&m_Tab_Parameters);
    m_Dlg_Check.Create(IDD_DLG_CHECK,&m_Tab_Parameters);
    m_Dlg_System.Create(IDD_DLG_SYSTEM,&m_Tab_Parameters);
    m_Dlg_Database.Create(IDD_DLG_DB, &m_Tab_Parameters);
    m_TabControler.Show_Parameters_Tab(SHEET_IMAGE);
}
//对于按钮响应函数:
void CAreaScanProjectDlg::OnBnClickedButtonDatabase()
{
    // TODO: 在此添加控件通知处理程序代码
    m_TabControler.Show_Parameters_Tab(SHEET_DATABASE);
}
//TavControler.cpp
#include "StdAfx.h"
#include "TabControler.h"
#include "AreaScanProjectDlg.h"

extern CAreaScanProjectDlg* g_pMainDlg;
CTabControler::CTabControler(void)
{
}


CTabControler::~CTabControler(void)
{
}

void CTabControler::Hide_All_Parameters()
{
    g_pMainDlg->m_Dlg_Image.SetWindowPos(NULL,g_pMainDlg->m_Rect_Parameters.left,g_pMainDlg->m_Rect_Parameters.top,g_pMainDlg->m_Rect_Parameters.Width(),g_pMainDlg->m_Rect_Parameters.Height(),SWP_HIDEWINDOW);
    g_pMainDlg->m_Dlg_Check.SetWindowPos(NULL,g_pMainDlg->m_Rect_Parameters.left,g_pMainDlg->m_Rect_Parameters.top,g_pMainDlg->m_Rect_Parameters.Width(),g_pMainDlg->m_Rect_Parameters.Height(),SWP_HIDEWINDOW);
    g_pMainDlg->m_Dlg_System.SetWindowPos(NULL,g_pMainDlg->m_Rect_Parameters.left,g_pMainDlg->m_Rect_Parameters.top,g_pMainDlg->m_Rect_Parameters.Width(),g_pMainDlg->m_Rect_Parameters.Height(),SWP_HIDEWINDOW);
    g_pMainDlg->m_Dlg_Database.SetWindowPos(NULL, g_pMainDlg->m_Rect_Parameters.left, g_pMainDlg->m_Rect_Parameters.top, g_pMainDlg->m_Rect_Parameters.Width(), g_pMainDlg->m_Rect_Parameters.Height(), SWP_HIDEWINDOW);

}

void CTabControler::Show_Parameters_Tab( int index )
{
    Hide_All_Parameters();
    switch(index)
    {
    case SHEET_IMAGE:
        g_pMainDlg->m_Dlg_Image.SetWindowPos(NULL,g_pMainDlg->m_Rect_Parameters.left,g_pMainDlg->m_Rect_Parameters.top,g_pMainDlg->m_Rect_Parameters.Width(),g_pMainDlg->m_Rect_Parameters.Height(),SWP_SHOWWINDOW);
        break;
    case SHEET_CHECK:
        g_pMainDlg->m_Dlg_Check.SetWindowPos(NULL,g_pMainDlg->m_Rect_Parameters.left,g_pMainDlg->m_Rect_Parameters.top,g_pMainDlg->m_Rect_Parameters.Width(),g_pMainDlg->m_Rect_Parameters.Height(),SWP_SHOWWINDOW);
        break;
    case SHEET_SYSTEM:
        g_pMainDlg->m_Dlg_System.SetWindowPos(NULL,g_pMainDlg->m_Rect_Parameters.left,g_pMainDlg->m_Rect_Parameters.top,g_pMainDlg->m_Rect_Parameters.Width(),g_pMainDlg->m_Rect_Parameters.Height(),SWP_SHOWWINDOW);
        break;
    case SHEET_DATABASE:
        g_pMainDlg->m_Dlg_Database.SetWindowPos(NULL, g_pMainDlg->m_Rect_Parameters.left, g_pMainDlg->m_Rect_Parameters.top, g_pMainDlg->m_Rect_Parameters.Width(), g_pMainDlg->m_Rect_Parameters.Height(), SWP_SHOWWINDOW);
        break;
    default:
        g_pMainDlg->m_Dlg_Image.SetWindowPos(NULL,g_pMainDlg->m_Rect_Parameters.left,g_pMainDlg->m_Rect_Parameters.top,g_pMainDlg->m_Rect_Parameters.Width(),g_pMainDlg->m_Rect_Parameters.Height(),SWP_SHOWWINDOW);
    }
}

这边挺蠢的一个问题就是,我有时候忘记加break;这个语句了,然后子对话框里面的控件全都不能响应。

你可能感兴趣的:(MFC按钮点击切换对话框)