如何用 CPrintDialog 打印一个TXT文档 ??

roger_it 2006-06-14 02:08:23
过路的大虾:
如何用 CPrintDialog 打印一个TXT 文档阿??

例如我要用 CPrintDialog 把 a.txt 给打印出来

怎么把 a.txt 的路径传给 CPrintDialog 阿??

帮助文件中没有提到过这个



...全文
486 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hansin 2006-07-03
  • 打赏
  • 举报
回复
用上面的程序应该可以
ianok 2006-06-20
  • 打赏
  • 举报
回复
引用头文件:
#include <fstream>
#include <iostream>
roger_it 2006-06-19
  • 打赏
  • 举报
回复
难道分数还不够??
roger_it 2006-06-16
  • 打赏
  • 举报
回复
各位大哥:

我要求实现的功能是:

做一个按钮,按一下,就会自动弹出 CPrintDialog 的那个对话框,(选择打印机),按确定之后,就自动打印“D:\test.txt",(这个文件肯定存在的)。

要如何实现阿??

谢啦
ianok 2006-06-15
  • 打赏
  • 举报
回复
之前那個笨了,你用這個好了:
ShellExecute(this->m_hWnd, "print", "D:\\test.txt","","", SW_SHOW);
ianok 2006-06-15
  • 打赏
  • 举报
回复
#include <vector>
using namespace std;
void CYouDlg::OnBnClickedButton1()
{
vector<CString> vecTmp;
static char BASED_CODE szFilter[] =
"Chart Files (*.xlc)|*.xlc|Worksheet Files (*.xls)|*.xls|Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files (*.*)|*.*||";

CFileDialog fileDlg(TRUE, NULL, "OpenFileDlg", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
szFilter, NULL,0);
if(fileDlg.DoModal() == IDOK){

ifstream ifs;
string str;
ifs.open(fileDlg.GetPathName());
if (!ifs.is_open()) {
throw ;
}
while(!ifs.eof())
{
getline(ifs, str);
vecTmp.push_back(str.c_str());
}
}

CPrintDialog printDlg(TRUE, PD_ALLPAGES|PD_ALLPAGES|PD_NOPAGENUMS, NULL);
if(printDlg.DoModal() == IDCANCEL)
return;
CDC dcPrinter;
PRINTDLG *pPrintDlg = &printDlg.m_pd;
AfxGetApp()->GetPrinterDeviceDefaults(pPrintDlg);
DEVMODE* lpDevMode = (DEVMODE*)::GlobalLock(pPrintDlg->hDevMode);
::GlobalUnlock(pPrintDlg->hDevMode);
lpDevMode->dmPaperSize = DMPAPER_A3; //A3 297 x 420 mm
lpDevMode->dmOrientation = DMORIENT_LANDSCAPE; //橫向
dcPrinter.Attach(printDlg.CreatePrinterDC());
// initialize DOCINFO
DOCINFO docinfo;
memset(&docinfo, 0, sizeof(docinfo));
docinfo.cbSize = sizeof(docinfo);
CString title;
GetWindowText(title);
docinfo.lpszDocName = title;

// if it fails, complain and exit gracefully
if (dcPrinter.StartDoc(&docinfo) < 0)
{
MessageBox(_T("プリンタを初期化できません"));
return;
}

if (dcPrinter.StartPage() < 0)
{
MessageBox(_T("ページ処理を開始できません"));
dcPrinter.AbortDoc();
return;
}

for (size_t i = 0, v = 0; i < vecTmp.size(); ++i) {
dcPrinter.TextOut(0, v, vecTmp[i]);
v += 100;

}
dcPrinter.EndPage();
dcPrinter.EndDoc();
}
roger_it 2006-06-15
  • 打赏
  • 举报
回复
to 楼上的大哥:

再简单点行不行?
我不懂啊

vector是个什么东西啊??

难道 CPrintDialog 就不能像 ShellExcute(..."print"....) 那样只要提供个文件的路径就能打印吗??

ShellExcute 不能选择打印机阿,所以感觉不怎么友好阿。

ianok 2006-06-15
  • 打赏
  • 举报
回复
提個建議:
先把文件讀出來一行一行存放在vector裏面,然後再用CPrintDialog 一行一行打印出來
roger_it 2006-06-14
  • 打赏
  • 举报
回复
自己顶
roger_it 2006-06-14
  • 打赏
  • 举报
回复
help

sos
roger_it 2006-06-14
  • 打赏
  • 举报
回复
继续等...
yubsh 2006-06-14
  • 打赏
  • 举报
回复
codeproject.com上有个分页打印CString的例子。
roger_it 2006-06-14
  • 打赏
  • 举报
回复
抢个沙发吧先

15,978

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧