现在的位置: 首页 > 综合 > 正文

在mfc对话框前添加起始对话框的问题

2014年09月05日 ⁄ 综合 ⁄ 共 1615字 ⁄ 字号 评论关闭

前提:首先添加UIcontrol.cpp和UIcontrol.h两个文件。

#pragma once

enum {
	UI_INDEX_MAIN=0,	//主窗口界面
	//UI_INDEX_SECOND,	//第二个界面
//上面的界面对应自己的每一个界面
};

class CUIcontrol
{
public:
	CUIcontrol(void);
	~CUIcontrol(void);
	int get_current_index(){return m_current;};
	CDialog * get_current_ui();
	void showUI(int index);

private:
	int m_current;
	CDialog	*m_lpUI;
};
#include "StdAfx.h"
#include "UIcontrol.h"
//此处添加每一个对应界面类的头文件
CUIcontrol::CUIcontrol(void)
{
	m_lpUI = NULL;
	m_current = -1;
}

CUIcontrol::~CUIcontrol(void)
{
	if(m_lpUI)
	{
		delete m_lpUI;
	}
}

void CUIcontrol::showUI(int index)
{
	if(index == m_current)
	{
		return;
	}

	CDialog *lp = NULL;
	switch(index)
	{
	case UI_INDEX_MAIN:
		lp = (CDialog*)new CMainDlg();//对应界面的类
		break;
	case UI_INDEX_SECOND:
		lp = (CDialog*)new CSecond();//自己建立的类
		break;
	default:
		return;
	}
	if(lp)
	{
		if(m_lpUI)
		{
			m_lpUI->SendMessage(WM_CLOSE,0,0);
		}
		m_current = index;
		m_lpUI = lp;
		m_lpUI->DoModal();
	}
}

CDialog* CUIcontrol::get_current_ui()
{
	return m_lpUI;
}

然后添加一个对话框,创建类。添加的类对应上面的Second类。

然后在工程文件的APP类的InitInstance()中添加如下语句:

#include "UIcontrol.h"
CUIcontrol UIcontrol; 

#include "Second.h"

以上是定义UI成员变量。

void welcome::OnButton1() 
{
	UIcontrol.showUI(UI_INDEX_SECOND);//此处是第一个对话框上面的按钮事件,作用是调用第二个界面
}
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	//UIControl.showUI(UI_INDEX_MAIN);//主界面
	UIControl.showUI(UI_INDEX_SECOND);//第二个界面
	/*CCD_MetroDlg dlg;
		m_pMainWnd = &dlg;
		int nResponse = dlg.DoModal();
		if (nResponse == IDOK)
		{
			// TODO: Place code here to handle when the dialog is
			//  dismissed with OK
		}
		else if (nResponse == IDCANCEL)
		{
			// TODO: Place code here to handle when the dialog is
			//  dismissed with Cancel
		}*/


		// Since the dialog has been closed, return FALSE so that we exit the
		//  application, rather than start the application's message pump.
		return FALSE;
}

然后运行就是。不明白的可以联系我。以上说的已经很清楚了。

抱歉!评论已关闭.