C++ 无文档创建分割视图

本文档详细介绍了如何使用MFC在SDI项目中构建CLeftView和CRightView类,分别继承CTreeView和CListView。CLeftView用于展示文件系统树形结构,CRightView则显示选中文件的详细信息。在CMainFrame中,通过CSplitterWnd创建两个视图,并实现了视图之间的交互,当左侧树形视图选择项改变时,右侧列表视图实时更新显示对应文件的属性。
摘要由CSDN通过智能技术生成

1 新建SDI项目,取消文档视图结构选项

2 删除CChildView类,及相关include,编译通过

3 必须在类向导中添加MFC类,继承CTreeView

#pragma once
#include <afxcview.h>
// CLeftView 视图

class CLeftView : public CTreeView
{
	DECLARE_DYNCREATE(CLeftView)

protected:
	CLeftView();           // 动态创建所使用的受保护的构造函数
	virtual ~CLeftView();

public:
#ifdef _DEBUG
	virtual void AssertValid() const;
#ifndef _WIN32_WCE
	virtual void Dump(CDumpContext& dc) const;
#endif
#endif

protected:
	DECLARE_MESSAGE_MAP()
public:
	virtual void OnInitialUpdate();
	CTreeCtrl& m_tree;
	afx_msg void OnTvnSelchanged(NMHDR* pNMHDR, LRESULT* pResult);
};


// CLeftView.cpp: 实现文件
//

#include "pch.h"
#include "SA.h"
#include "CLeftView.h"
#include "MainFrm.h"


// CLeftView

IMPLEMENT_DYNCREATE(CLeftView, CTreeView)

CLeftView::CLeftView()
	:m_tree(GetTreeCtrl())
{

}

CLeftView::~CLeftView()
{
}

BEGIN_MESSAGE_MAP(CLeftView, CTreeView)
	ON_NOTIFY_REFLECT(TVN_SELCHANGED, &CLeftView::OnTvnSelchanged)
END_MESSAGE_MAP()


// CLeftView 诊断

#ifdef _DEBUG
void CLeftView::AssertValid() const
{
	CTreeView::AssertValid();
}

#ifndef _WIN32_WCE
void CLeftView::Dump(CDumpContext& dc) const
{
	CTreeView::Dump(dc);
}
#endif
#endif //_DEBUG


// CLeftView 消息处理程序


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

	// TODO: 在此添加专用代码和/或调用基类
	static BOOL bFirstTime = TRUE;
	if (bFirstTime)
	{
		bFirstTime = FALSE;
		m_tree.ModifyStyle(0, TVS_HASLINES | TVS_HASBUTTONS | TVS_SHOWSELALWAYS
			| TVS_EDITLABELS | TVS_LINESATROOT);
		HTREEITEM hItem = m_tree.InsertItem(_T("我的电脑"));
		char s[256];
		GetLogicalDriveStrings(sizeof(s), s);
		char* p = s;
		CString str;
		while (*p)
		{
			str = p;
			int nLen = str.GetLength();
			if (nLen<1)
			{
				break;
			}
			if (str[nLen-1]=='\\')
			{
				str = str.Left(nLen - 1);
			}
			m_tree.InsertItem(str, hItem);
			p += nLen + 1;
		}
		m_tree.Expand(hItem, TVE_EXPAND);
	}
	}
	
	


void CLeftView::OnTvnSelchanged(NMHDR* pNMHDR, LRESULT* pResult)
{
	LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	HTREEITEM hItem = pNMTreeView->itemNew.hItem;
	CString szText = m_tree.GetItemText(hItem);
	/*AfxGetMainWnd()->SetWindowText(szText);*/
	CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
	if (hItem != m_tree.GetRootItem())
	{
		pFrame->m_pRight->Refresh(szText);
	}
	
	*pResult = 0;
}

4 必须在向导中创建MFC类CRightView,继承CListView

#pragma once
#include <afxcview.h>

// CRightView 视图

class CRightView : public CListView
{
	DECLARE_DYNCREATE(CRightView)

protected:
	CRightView();           // 动态创建所使用的受保护的构造函数
	virtual ~CRightView();

public:
#ifdef _DEBUG
	virtual void AssertValid() const;
#ifndef _WIN32_WCE
	virtual void Dump(CDumpContext& dc) const;
#endif
#endif

protected:
	DECLARE_MESSAGE_MAP()
public:
	virtual void OnInitialUpdate();
	CListCtrl& m_list;

	void Refresh(CString szText);
};


// CRightView.cpp: 实现文件
//

#include "pch.h"
#include "SA.h"
#include "CRightView.h"


// CRightView

IMPLEMENT_DYNCREATE(CRightView, CListView)

CRightView::CRightView()
	:m_list(GetListCtrl())
{

}

CRightView::~CRightView()
{
}

BEGIN_MESSAGE_MAP(CRightView, CListView)
END_MESSAGE_MAP()


// CRightView 诊断

#ifdef _DEBUG
void CRightView::AssertValid() const
{
	CListView::AssertValid();
}

#ifndef _WIN32_WCE
void CRightView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}
#endif
#endif //_DEBUG


// CRightView 消息处理程序


void CRightView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();

	// TODO: 在此添加专用代码和/或调用基类
	static BOOL bFirstTime = TRUE;
	if (bFirstTime)
	{
		bFirstTime = FALSE;
		m_list.ModifyStyle(0, LVS_REPORT | LVS_EX_FULLROWSELECT);
		m_list.InsertColumn(0, _T("名称"), 0, 120);
		m_list.InsertColumn(1, _T("大小"), 0, 120);
		m_list.InsertColumn(2, _T("类型"), 0, 120);
		m_list.InsertColumn(3, _T("修改时间"), 0, 120);
	}

	}

void CRightView::Refresh(CString szText)
{
	m_list.DeleteAllItems();
	if (szText.IsEmpty())
	{
		return;
	}
	CFileFind ff;
	BOOL b = ff.FindFile(szText + "\\*.*");
	int i = 0;
	CString str;
	while (b)
	{
		b = ff.FindNextFile();
		if (ff.IsHidden())
		{
			continue;
		}
		m_list.InsertItem(i, ff.GetFileName());
		if (ff.IsDirectory())
		{
			m_list.SetItemText(i, 2, _T("文件夹"));
		}
		else
		{
			int nLen = ff.GetLength();
			str.Format("%d", nLen);
			m_list.SetItemText(i, 1, str);
			m_list.SetItemText(i, 2, _T("文件"));
		}
		FILETIME ft;
		ff.GetLastWriteTime(&ft);
		COleDateTime time(ft);
		m_list.SetItemText(i, 3, time.Format("%Y-%m-%d %M-%S"));
		
		
	}
}
	
	

5 修改CMainFrame,主要修改OnCreateClient函数


// MainFrm.h: CMainFrame 类的接口
//
#pragma once
#include "CLeftView.h"
#include "CRightView.h"

class CMainFrame : public CFrameWnd
{
	
public:
	CMainFrame() noexcept;
protected: 
	DECLARE_DYNAMIC(CMainFrame)

// 特性
public:
	CSplitterWnd m_split;
	CLeftView* m_pLeft;
	CRightView* m_pRight;
// 操作
public:

// 重写
public:
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
	virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo);

// 实现
public:
	virtual ~CMainFrame();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:  // 控件条嵌入成员
	CToolBar          m_wndToolBar;
	CStatusBar        m_wndStatusBar;

// 生成的消息映射函数
protected:
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnSetFocus(CWnd *pOldWnd);
	DECLARE_MESSAGE_MAP()

	virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext);
};



// MainFrm.cpp: CMainFrame 类的实现
//

#include "pch.h"
#include "framework.h"
#include "SA.h"
#include "CLeftView.h"
#include "MainFrm.h"
#include "CRightView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CMainFrame

IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_WM_CREATE()
	ON_WM_SETFOCUS()
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // 状态行指示器
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

// CMainFrame 构造/析构

CMainFrame::CMainFrame() noexcept
	:m_pLeft(NULL)
	,m_pRight(NULL)
{
	// TODO: 在此添加成员初始化代码
}

CMainFrame::~CMainFrame()
{
}

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("未能创建工具栏\n");
		return -1;      // 未能创建
	}

	if (!m_wndStatusBar.Create(this))
	{
		TRACE0("未能创建状态栏\n");
		return -1;      // 未能创建
	}
	m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT));

	// TODO: 如果不需要可停靠工具栏,则删除这三行
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);


	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
	cs.lpszClass = AfxRegisterWndClass(0);
	return TRUE;
}

// CMainFrame 诊断

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

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}
#endif //_DEBUG


// CMainFrame 消息处理程序

void CMainFrame::OnSetFocus(CWnd* /*pOldWnd*/)
{
	// 将焦点前移到视图窗口
	m_pLeft->SetFocus();
}

BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
	// 让视图第一次尝试该命令
	/*if (m_pLeft->OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
		return TRUE;*/

	// 否则,执行默认处理
	return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}



BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
	// TODO: 在此添加专用代码和/或调用基类
	m_split.CreateStatic(this, 1, 2);
	m_split.CreateView(0, 0, RUNTIME_CLASS(CLeftView), CSize(180, 0), NULL);
	m_split.CreateView(0, 1, RUNTIME_CLASS(CRightView), CSize(0, 0), NULL);

	m_pLeft = (CLeftView*)m_split.GetPane(0, 0);
	m_pRight =(CRightView*)m_split.GetPane(0, 1);
	return TRUE;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值