本文参考or抄袭"Visual C++程序设计自学手册" 10.4.3 设计工作证打印程序
MFC有用于打印预览的设备环境类CPreviewDC和预览视图类CPreviewView,并且MFC在文档/视图模式下提供了方便的打印方式,可是现在的应用程序大多是基于对话框的,因此如何在基于对话框的程序中使用文档视图的框架成了首要的问题,也很必要啦~
下面带来的是不完美版本,先建立单文档/视图程序,然后在其中创建对话框,跟需求的有点区别,不过先看看再说
1.创建一个单文档的应用程序,在"MFC App Wizard-Step 4 of 6"对话框中去掉"Initial status bar"复选框中的" √ ",使生成的程序没有状态栏.
2.在"MFC App Wizard-Step 6 of 6"对话框的"Base Class"组合框中选择"CStrollView",使生成的程序具有滚动条.
3.修改菜单资源,修改后的菜单如下图所示
不一定要这样做啦~
4.修改工具栏资源,修改后的工具栏如下图所示
只用了一个字,很简单而已
5.创建一个对话框,向对话框中添加一个列表视图控件和一个按钮控件,并为对话框关联一个类CWorkCard
6.在初始化函数InitInstance()中修改默认代码,使应用程序框架在初始化时不显示,而是弹出创建的对话框
注释此段代码
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
// // Parse command line for standard shell commands, DDE, file open
// CCommandLineInfo cmdInfo;
// ParseCommandLine(cmdInfo);
//
// // Dispatch commands specified on the command line
// if (!ProcessShellCommand(cmdInfo))
// return FALSE;
//
// // The one and only window has been initialized, so show and update it.
// m_pMainWnd->ShowWindow(SW_SHOW);
// m_pMainWnd->UpdateWindow();
添加代码
CWorkCard dlg;
dlg.DoModal();
变为
BOOL CDocViewApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CDocViewDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CDocViewView));
AddDocTemplate(pDocTemplate);
// // Parse command line for standard shell commands, DDE, file open
// CCommandLineInfo cmdInfo;
// ParseCommandLine(cmdInfo);
//
// // Dispatch commands specified on the command line
// if (!ProcessShellCommand(cmdInfo))
// return FALSE;
//
// // The one and only window has been initialized, so show and update it.
// m_pMainWnd->ShowWindow(SW_SHOW);
// m_pMainWnd->UpdateWindow();
CWorkCard dlg;
dlg.DoModal();
return TRUE;
}
当然头文件是必须的
#include "WorkCard.h"
后面就是将列表控件与数据库连接
此处省略...
[1] 在StdAfx.h中添加代码
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF")
[2]在初始化函数InitInstance()中添加代码进行初始化COM环境
::CoInitialize(NULL);
[3]CWorkCard类中定义指针
_ConnectionPtr m_pConnection;
_RecordsetPtr m_pRecordset;
[4]在CWorkCard类中添加消息处理函数OnInitDialog()并编辑
添加如下代码
m_Grid.SetExtendedStyle(LVS_EX_FLATSB
|LVS_EX_FULLROWSELECT
|LVS_EX_HEADERDRAGDROP
|LVS_EX_ONECLICKACTIVATE
|LVS_EX_GRIDLINES);
m_Grid.InsertColumn(0,"编号",LVCFMT_LEFT,100,0);
m_Grid.InsertColumn(1,"姓名",LVCFMT_LEFT,100,1);
m_Grid.InsertColumn(2,"职务",LVCFMT_LEFT,100,2);
m_Grid.InsertColumn(3,"部门",LVCFMT_LEFT,100,3);
arrays[0][0] = "编号:";
arrays[1][0] = "姓名:";
arrays[2][0] = "职务:";
arrays[3][0] = "部门:";
CString strname;
strname.Format("uid=;pwd=;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=shujuku.mdb;");
try
{
m_pConnection.CreateInstance("ADODB.Connection");
_bstr_t strConnect=strname;
m_pConnection->Open(strConnect,"","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
_bstr_t bstrSQL;
bstrSQL = "select * from employees order by 编号 desc";
CString strText;
int i=9;
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
while(!m_pRecordset->adoEOF)
{
arrays[0][i] = (char*)(_bstr_t)m_pRecordset->GetCollect("编号");
arrays[1][i] = (char*)(_bstr_t)m_pRecordset->GetCollect("姓名");
arrays[2][i] = (char*)(_bstr_t)m_pRecordset->GetCollect("职务");
arrays[3][i] = (char*)(_bstr_t)m_pRecordset->GetCollect("部门");
m_Grid.InsertItem(0,0);
m_Grid.SetItemText(0,0,arrays[0][i]);
m_Grid.SetItemText(0,1,arrays[1][i]);
m_Grid.SetItemText(0,2,arrays[2][i]);
m_Grid.SetItemText(0,3,arrays[3][i]);
m_pRecordset->MoveNext();
i--;
}
if(m_pRecordset!=NULL)
m_pRecordset->Close();
m_pConnection->Close();
当然要先定义变量m_Grid,arrays
m_Grid为CWorkCard成员变量
arrays在App中定义
不要忘记
extern CDocViewApp theApp;
extern CString arrays[4][10];
后来就变成这样了哦!
------------------------------------------------------------------------------------------------------------------------------------------
O(∩_∩)O哈哈~明天继续
------------------------------------------------------------------------------------------------------------------------------------------
[5]为对话框的打印按钮添加响应函数OnButprint
void CWorkCard::OnButPrint()
{
// TODO: Add your control notification handler code here
theApp.m_pDocManager->OnFileNew(); //弹出单文档视图
CDialog::OnOK(); //弹出单文档视图后关闭对话框
}
到这里其实已经完成了从对话框弹出但文档视图,接下来是打印
[6]定义各种变量
CFont titlefont; //标题字体
CFont bodyfont; //正文字体
int screenx,screeny; //获取窗口每英寸像素数
int printx,printy; //获取打印机每英寸像素数
double ratex,ratey; //打印机与屏幕的像素比率
[7]在CDocViewView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)中添加代码,当然首先要去掉括号中的注释
void CDocViewView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: add extra initialization before printing
pInfo->SetMaxPage(3); //设置最大打印页数
printx =pDC->GetDeviceCaps(LOGPIXELSX);
printy =pDC->GetDeviceCaps(LOGPIXELSY);
ratex = (double)printx /screenx; //确定打印机与屏幕的比率
ratey = (double)printy /screeny;
}
[8]设置Scroll拖动范围
void CDocViewView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = 100;
sizeTotal.cy = 3800;
SetScrollSizes(MM_TEXT, sizeTotal); //设置拖动范围
}
SetScrollSizes用法参见MSDN
[9]在OnDraw中绘制单文档视图
[10]在OnPrint中设置打印
[11]为打印设置,打印预览,打印三个按钮添加响应函数
修改ID即可,与文件下的ID 相同