效果演示
上位机MFC实现树状控件的内容打印实例
有良好层次结构的树状控件,有时我们希望将整个结构打印下来,当前例程就是实现树状控件的打印功能,
效果如上图,点击工具栏上打印按钮,可以将树状控件的内容全部打印出来。
例程是将内容打印到PDF上,并进行了截图展示。工程源代码可以下载使用。
下载地址:
功能实现
1.新建立基于CFormView的单文档程序,添加树状控件。
树状控件属性可以设置为有按钮,有线条,根部有线,比较直观。
2.复制使用ADVTREECTRL.CPP,ADVTREECTRL.H,导入到工程后在视窗类中包含头文件。
并关联树状控件
#include "AdvTreeCtrl.h"
CAdvancedTreeCtrl m_wndTree;
树状控件当前是空白没有内容的,所以初始化控件,添加一些内容上去
m_wndTree.InitImageList();
// Fill the tree with colored text.
CString m_str;
for(int i=1;i<=10;i++)
{
m_str.Format("Parent Item %d", i);
HTREEITEM hItem = m_wndTree.InsertItem(m_str, 0, 0);
for(int j=1;j<=5;j++)
{
m_str.Format("Child Item %d", j);
HTREEITEM hChild = m_wndTree.InsertItem(m_str, 1, 1, hItem);
for(int k=1;k<=5;k++)
{
m_str.Format("ChildChild Item %d", k);
HTREEITEM hChildChild = m_wndTree.InsertItem(m_str, 1, 1, hChild);
for(int s=1;s<=5;s++)
{
m_str.Format("ChildChildChild Item %d", s);
HTREEITEM hChildChildChild = m_wndTree.InsertItem(m_str, 1, 1, hChildChild);
}
}
}
}
// Fill the tree with default text.
HTREEITEM hItem = m_wndTree.InsertItem("Parent Item default A", 0, 0);
HTREEITEM hChild = m_wndTree.InsertItem("Child Item default A", 1, 1, hItem);
hItem = m_wndTree.InsertItem("Parent Item default B", 0, 0);
hChild = m_wndTree.InsertItem("Child Item default B", 1, 1, hItem);
// Fill the tree special text.
hItem = m_wndTree.InsertItem("Parent Item default A", 0, 0);
hChild = m_wndTree.InsertItem("Child Item default A", 1, 1, hItem);
hItem = m_wndTree.InsertItem("Parent Item default B", 0, 0);
hChild = m_wndTree.InsertItem("Child Item default B", 1, 1, hItem);
// Fill the tree with Text and Icons
hItem = m_wndTree.InsertItem( "Floppy Drive", ILI_FLOPPY, ILI_FLOPPY );
hChild = m_wndTree.InsertItem( "Child Node", ILI_CLOSED_FOLDER,
ILI_OPEN_FOLDER, hItem, TVI_SORT);
hItem = m_wndTree.InsertItem( "HardDisk Drive", ILI_HARD_DISK, ILI_HARD_DISK );
hChild = m_wndTree.InsertItem( "Child Node", ILI_CLOSED_FOLDER,
ILI_OPEN_FOLDER, hItem, TVI_SORT);
hItem = m_wndTree.InsertItem( "Net Drive", ILI_NET_DRIVE, ILI_NET_DRIVE );
hChild = m_wndTree.InsertItem( "Child Node", ILI_CLOSED_FOLDER,
ILI_OPEN_FOLDER, hItem, TVI_SORT);
hItem = m_wndTree.InsertItem( "CD Drive", ILI_CD_ROM, ILI_CD_ROM );
hChild = m_wndTree.InsertItem( "Child Node", ILI_CLOSED_FOLDER,
ILI_OPEN_FOLDER, hItem, TVI_SORT);
HTREEITEM hChildChild = m_wndTree.InsertItem( "ChildChild Node", ILI_CLOSED_FOLDER,
ILI_OPEN_FOLDER, hChild, TVI_SORT);
// Set Overlay-Image
BOOL b = m_wndTree.SetItemState( hChildChild, INDEXTOOVERLAYMASK(2), TVIS_OVERLAYMASK );
// Sizing the Treecontrol equal to the ClientAreaSize
RECT locRect;
GetClientRect( &locRect );
m_wndTree.MoveWindow( 0, 0, locRect.right, locRect.bottom );
m_wndTree.ShowWindow( TRUE );
3.编译可以发现有错误,提示少一些资源IDB_MINUSBUTTON
IDB_PLUSBUTTON,IDR_DRIVEIMAGES,三个图像资源可以自己添加或直接使用例程的。
另外还有一模板类不识别,assert函数不识别错误,这是因为没有包含其所在头文件,
所以还得在stdafx.h文件内包含代码段
#include "resource.h"
#include <assert.h>
#include <afxtempl.h> // MFC template
这样编辑就可以通过,运行例程查看树状控件效果。
4.紧接着是实现打印功能,编辑如下函数
- void CCTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- m_bFirstTime = TRUE;
- m_wndTree.m_bContinuePrinting = TRUE;
- // Fill the DIB-Array
- m_wndTree.CreateDibArrays();
- }
- void CCTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- m_bFirstTime = TRUE;
- m_wndTree.m_bContinuePrinting = TRUE;
- // Empty the DIB-Array
- m_wndTree.DeleteDibArrays();
- }
复制代码 添加函数
void CCTestView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
// Define the Originmapping of the viewport and
// the window for Footer and Header
int iSavedPageStart = pInfo->m_rectDraw.top;
pDC->SetWindowOrg( 0, 0 );
pDC->SetViewportOrg( pInfo->m_rectDraw.left, -iSavedPageStart );
// Print the Page
// Get the View-DC
CDC* pViewDC = GetDC();
// Set the Mappingmode
pDC->SetMapMode(MM_ANISOTROPIC);
// Define the Originmapping of the viewport and
// the window for Treeoutput
pDC->SetWindowOrg( 0, 0 );
pDC->SetViewportOrg( pInfo->m_rectDraw.left, pInfo->m_rectDraw.top - iSavedPageStart );
// Define the Extentmapping of the viewport and the Window
pDC->SetViewportExt( pDC->GetDeviceCaps(LOGPIXELSX)
,pDC->GetDeviceCaps(LOGPIXELSY) );
pDC->SetWindowExt( pViewDC->GetDeviceCaps(LOGPIXELSX)
,pViewDC->GetDeviceCaps(LOGPIXELSY) );
// Release the View-DC
ReleaseDC(pViewDC);
if( TRUE == m_bFirstTime )
{
// Draw the Tree to the Printer-DC
m_wndTree.MakePaging( pDC, pInfo );
m_bFirstTime = FALSE;
}
// Draw the Tree to the Printer-DC
m_wndTree.DrawTreeCtrl( pDC, pInfo );
}
5.编译运行程序,就可以点击打印按钮进行打印树状控件了
|