⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.cpp

📁 精通MFC程序设计
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "CToobarDemo.h"

#include "MainFrm.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_WM_TIMER()
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(ID_BUTTON1111, OnButton11)
	ON_COMMAND(IDD_SHOW_HIDE_IE_BAR, OnShowHideIeBar)
	ON_UPDATE_COMMAND_UI(IDD_SHOW_HIDE_IE_BAR, OnUpdateShowHideIeBar)
	ON_NOTIFY(TVN_SELCHANGED,IDS_TREECTRL,OnSelchanged)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CLOCK,
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
}

CMainFrame::~CMainFrame()
{	
	//删除控件对象
	delete[] pButton1;
	delete[] pEdit1;
	delete[] pEdit2;
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}
	if (!m_wndToolBar.CreateCombo( WS_CHILD | CBS_DROPDOWNLIST | 
		CBS_AUTOHSCROLL | WS_VSCROLL | CBS_HASSTRINGS, &m_pCombo,
		IDC_COMBOX, 200, 300 ))
	{
		TRACE0("Failed to create combo box.\n");
		return -1;
	}
		m_pCombo.AddString("组合框选择一");
		m_pCombo.AddString("组合框选择二");
		m_pCombo.AddString("组合框选择三");
		m_pCombo.AddString("组合框选择四");	
		//Don't forget the initial position.
		m_pCombo.SetCurSel(0);


	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	SetTimer(1,1000,NULL);

	//创建对话条,ID为创建的对话条IDD_DIABAR,但并不在主框架上显示 
	if (!m_WzdDialogBar.Create(this, IDD_DIABAR,WS_CHILD|WS_VISIBLE,-1))
	{
		TRACE0( "Failed to create dialog bar\n" );
		return -1; // fail to create
	}
	//动态创建按钮控件1
	pButton1 = new CButton();
	CRect rect1(70,2,155,28);//按钮控件的大小位置
	if(!pButton1->Create("对话条响应",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,rect1,&m_WzdDialogBar,ID_BUTTON1111))
	{
		AfxMessageBox("创建按钮控件失败!");
		return false;
	}
	
	//动态创建编辑控件1
	pEdit1=new CEdit();
	CRect rect2(170,2,245,28);//编辑控件的大小位置
	if(!pEdit1->Create(WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL,rect2,&m_WzdDialogBar,ID_EDIT1111))
	{
		AfxMessageBox("创建编辑框控件失败!");
		return false;
	}
	//pEdit1->EnableWindow(false);
	
	if ( !m_wndReBar.Create( this ) )
	{
		TRACE0( "Failed to create rebar\n" );
		return -1; // fail to create
	}
	//将创建的对话条载入到伸缩条
	m_wndReBar.AddBar( &m_WzdDialogBar,"伸缩条", NULL,  RBBS_GRIPPERALWAYS | RBBS_FIXEDBMP);

	//动态创建编辑控件2
	pEdit2=new CEdit();
	CRect rect4(440,2,515,28);//编辑控件的大小位置
	if(!pEdit2->Create(WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL,rect4,this,ID_EDIT2222))
	{
		AfxMessageBox("创建编辑框控件失败!");
		return false;
	}
	//将编辑控件2加载到伸缩条
	m_wndReBar.AddBar( pEdit2,"伸缩条2", NULL,  RBBS_GRIPPERALWAYS | RBBS_FIXEDBMP);

	//创建自制控制条并初始化
	if(!m_IEBar.Create(this, IDD_IEBAR, CBRS_LEFT, 100) || !m_IEBar.InitIEBar())
		return -1;
	pTreeCtrl = m_IEBar.GetTreeCtrl();//获取树形控件
	ASSERT(pTreeCtrl);
	// modify tree styles
	pTreeCtrl->ModifyStyle(NULL, TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS);//设置树形控件风格
	HTREEITEM t_hRoot = pTreeCtrl->InsertItem("消息分类");//插入根节点
	for(int i=0; i<20; i++)//插入子节点
	{
		CString t_ItemStr;
		t_ItemStr.Format("消息命令%d", i+1);
		HTREEITEM hItem = pTreeCtrl->InsertItem(t_ItemStr, t_hRoot) ;
		pTreeCtrl->SetItemData(hItem, i+1 ); //为子节点设置值便于调用
	}
	m_IEBar.EnableDocking(CBRS_ORIENT_HORZ);//水平停靠
	DockControlBar(&m_IEBar);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers


void CMainFrame::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CTime time;
	time=CTime::GetCurrentTime();
	CString s=time.Format("%H:%M:%S");
	m_wndStatusBar.SetPaneText(m_wndStatusBar.CommandToIndex(ID_INDICATOR_CLOCK),s);
	CFrameWnd::OnTimer(nIDEvent);
}

void CMainFrame::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	KillTimer(1);	
	CFrameWnd::OnClose();
}
void CMainFrame::OnButton1()
{
	//AfxMessageBox("伸缩条命令响应成功");
	pEdit1->SetWindowText("");
	pEdit2->SetWindowText("");
}
void CMainFrame::OnButton11()
{
	pEdit1->SetWindowText("响应成功");
	pEdit2->SetWindowText("响应成功");
	//AfxMessageBox("伸缩条命令响应成功");	
}


void CMainFrame::DoDataExchange(CDataExchange* pDX) 
{
	// TODO: Add your specialized code here and/or call the base class	
	CFrameWnd::DoDataExchange(pDX);
//	DDX_Text(pDX, IDC_EDITCHAR, m_Char);
}

void CMainFrame::OnShowHideIeBar() 
{
	// TODO: Add your command handler code here
	ShowControlBar(&m_IEBar, (m_IEBar.GetStyle() & WS_VISIBLE) == 0, FALSE);
	
}

void CMainFrame::OnUpdateShowHideIeBar(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck((m_IEBar.GetStyle() & WS_VISIBLE) != 0);
	
}

void CMainFrame::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult)
{
	NMTREEVIEW *pTreeView=(NMTREEVIEW*)pNMHDR;
	HTREEITEM SelItem;//定义条目
	SelItem=pTreeCtrl->GetSelectedItem();	//获取当前被选择的tree view项
	DWORD m;
	m=pTreeCtrl->GetItemData(SelItem);//回与一个项关联的32位的应用程序指定值
	CString str;
	str.Format("这是消息命令%d",m);
	if (m>0)
	AfxMessageBox(str);				//命令响应
	pResult=0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -