📄 tayoprndlg.cpp
字号:
// TAYOPRNDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TAYOPRN.h"
#include "TAYOPRNDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTAYOPRNDlg dialog
CTAYOPRNDlg::CTAYOPRNDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTAYOPRNDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTAYOPRNDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTAYOPRNDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTAYOPRNDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTAYOPRNDlg, CDialog)
//{{AFX_MSG_MAP(CTAYOPRNDlg)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTAYOPRNDlg message handlers
BOOL CTAYOPRNDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
bool AdjustRightAfterSelect(CString DeviceName,CString PortName,DWORD dwFlags)
{
//打印机
if (DeviceName!="PCL Inkjet"){
AfxMessageBox(_T("打印机必须选择PCL_Inkjet"));
return false;
}
//端口
if (PortName!="LPT1:"){
AfxMessageBox(_T("端口必须选择LPT1:,如果下拉列表没有LPT1:,请检查打印机是否连线正确并上电"));
return false;
}
//纸张大小
if ((dwFlags & PD_SELECTA4)==0){
AfxMessageBox(_T("必须选择A4幅面纸张"));
return false;
}
//草稿模式
if ((dwFlags & PD_SELECTDRAFTMODE)!=0){
AfxMessageBox(_T("不能选择草稿模式"));
return false;
}
//彩色
if ((dwFlags & PD_SELECTPRINTINCOLOR)==0){
AfxMessageBox(_T("必须选择彩色模式"));
return false;
}
//打印全部
if ((dwFlags & PD_SELECTALLPAGES)==0){
AfxMessageBox(_T("必须选择全部打印"));
return false;
}
//横向打印
if ((dwFlags & PD_SELECTPORTRAIT)==0){
AfxMessageBox(_T("必须选择纵向打印"));
return false;
}
return true;
}
void CTAYOPRNDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CPrintDialog dlg(FALSE);//让用户选择正确的打印机类型和端口号
if (dlg.DoModal()==IDOK){
//必须选择“确定”
CString DeviceName=dlg.GetDeviceName();
CString PortName=dlg.GetPortName();
DWORD dwFlags=dlg.m_pd.dwFlags;
if (AdjustRightAfterSelect(DeviceName,PortName,dwFlags)){
//必须正确判断才打印
//AfxMessageBox(_T("Setup Right"));
//////////////////////////////////////////////////////
HDC hdcPrinter = dlg.GetPrinterDC();
if (hdcPrinter == NULL)
{
//没有打印机,或者在打印设置对话框中选择了×,而不是OK
AfxMessageBox(_T("打印机配置不正确"));
}
else
{
// Create a CDC and attach it to the default printer.
CDC dcPrinter;
dcPrinter.Attach(hdcPrinter);
// Call StartDoc() to begin printing.
DOCINFO docinfo;
memset(&docinfo, 0, sizeof(docinfo));
docinfo.cbSize = sizeof(docinfo);
docinfo.lpszDocName = _T("CDC::StartDoc() Code Fragment");
// If it fails, complain and exit gracefully.
if (dcPrinter.StartDoc(&docinfo) < 0)
{
AfxMessageBox(_T("Printer would not initalize"));
}
else
{
// Start a page.
if (dcPrinter.StartPage() < 0)
{
AfxMessageBox(_T("Could not start page"));
dcPrinter.AbortDoc();
}
else
{
// Do some printing.
//黑白打印,大字
CFont newFont;
newFont.CreateFont(
30, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
_T("Arial"));
CFont* pOldFont = dcPrinter.SelectObject(&newFont);
// CGdiObject* pOldFont = dcPrinter.SelectStockObject(SYSTEM_FONT);
dcPrinter.ExtTextOut(150, 150,ETO_CLIPPED,NULL,_T("这是一个测试打印程序!"),8,NULL);
dcPrinter.ExtTextOut(50, 50,ETO_CLIPPED,NULL,_T("Hello World!"), 12,NULL);
dcPrinter.SelectObject(pOldFont);
newFont.DeleteObject();
//大字体,彩色
CFont newFont1;
newFont1.CreateFont(
100, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_NORMAL, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
_T("Arial"));
pOldFont = dcPrinter.SelectObject(&newFont1);
//标准字体,彩色
dcPrinter.SetTextColor(RGB(0,255,0));
dcPrinter.ExtTextOut(400, 550,ETO_CLIPPED,NULL,_T("这是一个测试打印程序!"),8,NULL);
dcPrinter.SelectObject(pOldFont);
newFont1.DeleteObject();
//黑色线
dcPrinter.MoveTo(200,200);
dcPrinter.LineTo(400,400);
//彩色线,变宽
CPen newPen(PS_SOLID,10,RGB(0,255,0));
CPen* pOldPen=dcPrinter.SelectObject(&newPen);
dcPrinter.MoveTo(750,750);
dcPrinter.LineTo(950,950);
dcPrinter.SelectObject(pOldPen);
newPen.DeleteObject();
//彩色打印矩形
CBrush brush2(RGB(255,0,0));
CRect rc(300,300,400,400);
dcPrinter.FillRect(&rc,&brush2);
brush2.DeleteObject();
dcPrinter.EndPage();
dcPrinter.EndDoc();
}
}
CDC::FromHandle(hdcPrinter)->DeleteDC();//N非ULL才能释放
}
//CDC::FromHandle(hdcPrinter)->DeleteDC();//N非ULL才能释放
//////////////////////////////////////////////////////
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -