我的工程很简单,单文档单视图的,请高手耐心听我说一下,总共7步,做完这个工程只需要2分钟,多谢。
1、生成单文档单视图工程PrintLeft
2、添加一个对话框,不要最大化最小化按钮,在dialog properties下设置stytle属性,其中styles是Child,而后用classwizard生成它对应的对话框类
class CCRight : public CFormView,其他不用管
3、在主框架类一个变量 bool m_bSplitterCreated; 并初始化为false。
4、加入代码
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
if (!m_wndSplitter.CreateStatic(this, 1, 2))
{
TRACE0("Failed to create splitter window\n");
return FALSE;
}
CRect rect;
GetWindowRect( &rect );
CSize paneSize(27*rect.Width()/100, rect.Height());
CSize paneSize1(73*rect.Width()/100, rect.Height()); if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CPrintLeftView), paneSize1, pContext))
{
TRACE0("Failed to create left pane view\n");
return FALSE;
}
if (!m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CCRight), paneSize, pContext))
{
TRACE0("Failed to create right pane frame\n");
return FALSE;
} m_bSplitterCreated = true;
SetActiveView((CView*) m_wndSplitter.GetPane(0, 0));
return TRUE;
// return CFrameWnd::OnCreateClient(lpcs, pContext);
}
5、加入WM_size消息对应代码
void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
CFrameWnd::OnSize(nType, cx, cy);
CRect rect;
GetWindowRect( &rect );
if( m_bSplitterCreated ) // m_bSplitterCreated set in OnCreateClient
{
m_wndSplitter.SetColumnInfo(0, 73*rect.Width()/100, 10);
m_wndSplitter.SetColumnInfo(1, 27*rect.Width()/100, 10);
m_wndSplitter.RecalcLayout();
}
}
6、在工程视图类CPrintLeftView加入代码
void CPrintLeftView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
if(pDC->IsPrinting()) //横向打印
{
LPDEVMODE pDevMode;
pDevMode=pInfo->m_pPD->GetDevMode();
pDevMode->dmOrientation=DMORIENT_LANDSCAPE;
pDC->ResetDC(pDevMode);
} CView::OnPrepareDC(pDC, pInfo);
long iLogPixelX=pDC->GetDeviceCaps(LOGPIXELSX);
long iLogPixelY=pDC->GetDeviceCaps(LOGPIXELSY); pDC->SetMapMode(MM_ANISOTROPIC); //转换坐标映射方式、确定窗口大小
CSize size = CSize(6000, 5000);
pDC->SetWindowExt(size);
int xLogPixPerInch = pDC->GetDeviceCaps(LOGPIXELSX); //得到实际设备每逻辑英寸的象素数量
int yLogPixPerInch = pDC->GetDeviceCaps(LOGPIXELSY);
long xExt = (long)size.cx *xLogPixPerInch/96; //得到设备坐标和逻辑坐标的比例
long yExt = (long)size.cy *yLogPixPerInch/96;
pDC->SetViewportExt((int)xExt, (int)yExt);
}
7、在工程视图类CPrintLeftView的绘图函数中加入代码
void CPrintLeftView::OnDraw(CDC* pDC)
{
CPrintLeftDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc); CRect rect;
GetWindowRect(&rect);
CPoint prePoint,nextPoint;
CPoint prexGrid,nextxGrid;
CPoint preyGrid,nextyGrid;
prexGrid.x=0;
prexGrid.y=0;
nextxGrid.x=rect.Width();
nextxGrid.y =0;
CPen newPen;
newPen.CreatePen(PS_DOT, 1, RGB(220, 220, 220));
CPen* pOldPen = pDC->SelectObject(&newPen);
CPen BoldLine(PS_SOLID, 1.5, RGB(220,220,220)); int x_times=3;
for(int i=0;i<rect.Height();i++)//画背景横线
{ if(i%5==0)
{
pDC->SelectObject(&BoldLine);
pDC->MoveTo(prexGrid);
pDC->LineTo(nextxGrid);
pDC->SelectObject(&newPen);
}
else
{
pDC->MoveTo(prexGrid);
pDC->LineTo(nextxGrid); }
prexGrid.y = prexGrid.y+100;//60/x_times;
nextxGrid.y = nextxGrid.y+100;//60/x_times;
} pDC->SelectObject(pOldPen); preyGrid.x=0;
preyGrid.y=0;
nextyGrid.x=0;
nextyGrid.y =rect.Height()*2; for(i=0;i <rect.Width()-1;i++)//画背景竖线
{ if(i%5==0)//5小格等于1大格
{ pDC->SelectObject(&BoldLine);
pDC->MoveTo(preyGrid);
pDC->LineTo(nextyGrid);
pDC->SelectObject(pOldPen);
}
else
{ pDC->SelectObject(&newPen);
pDC->MoveTo(preyGrid);
pDC->LineTo(nextyGrid);
pDC->SelectObject(pOldPen);
} preyGrid.x=preyGrid.x+120;
nextyGrid.x=nextyGrid.x+120;
}}
一切就绪,编译工程,运行,而后点击打印预览,或者点击打印,结果打印出来的图和显示的图不一致!
1、生成单文档单视图工程PrintLeft
2、添加一个对话框,不要最大化最小化按钮,在dialog properties下设置stytle属性,其中styles是Child,而后用classwizard生成它对应的对话框类
class CCRight : public CFormView,其他不用管
3、在主框架类一个变量 bool m_bSplitterCreated; 并初始化为false。
4、加入代码
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
if (!m_wndSplitter.CreateStatic(this, 1, 2))
{
TRACE0("Failed to create splitter window\n");
return FALSE;
}
CRect rect;
GetWindowRect( &rect );
CSize paneSize(27*rect.Width()/100, rect.Height());
CSize paneSize1(73*rect.Width()/100, rect.Height()); if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CPrintLeftView), paneSize1, pContext))
{
TRACE0("Failed to create left pane view\n");
return FALSE;
}
if (!m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CCRight), paneSize, pContext))
{
TRACE0("Failed to create right pane frame\n");
return FALSE;
} m_bSplitterCreated = true;
SetActiveView((CView*) m_wndSplitter.GetPane(0, 0));
return TRUE;
// return CFrameWnd::OnCreateClient(lpcs, pContext);
}
5、加入WM_size消息对应代码
void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
CFrameWnd::OnSize(nType, cx, cy);
CRect rect;
GetWindowRect( &rect );
if( m_bSplitterCreated ) // m_bSplitterCreated set in OnCreateClient
{
m_wndSplitter.SetColumnInfo(0, 73*rect.Width()/100, 10);
m_wndSplitter.SetColumnInfo(1, 27*rect.Width()/100, 10);
m_wndSplitter.RecalcLayout();
}
}
6、在工程视图类CPrintLeftView加入代码
void CPrintLeftView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
{
// TODO: Add your specialized code here and/or call the base class
if(pDC->IsPrinting()) //横向打印
{
LPDEVMODE pDevMode;
pDevMode=pInfo->m_pPD->GetDevMode();
pDevMode->dmOrientation=DMORIENT_LANDSCAPE;
pDC->ResetDC(pDevMode);
} CView::OnPrepareDC(pDC, pInfo);
long iLogPixelX=pDC->GetDeviceCaps(LOGPIXELSX);
long iLogPixelY=pDC->GetDeviceCaps(LOGPIXELSY); pDC->SetMapMode(MM_ANISOTROPIC); //转换坐标映射方式、确定窗口大小
CSize size = CSize(6000, 5000);
pDC->SetWindowExt(size);
int xLogPixPerInch = pDC->GetDeviceCaps(LOGPIXELSX); //得到实际设备每逻辑英寸的象素数量
int yLogPixPerInch = pDC->GetDeviceCaps(LOGPIXELSY);
long xExt = (long)size.cx *xLogPixPerInch/96; //得到设备坐标和逻辑坐标的比例
long yExt = (long)size.cy *yLogPixPerInch/96;
pDC->SetViewportExt((int)xExt, (int)yExt);
}
7、在工程视图类CPrintLeftView的绘图函数中加入代码
void CPrintLeftView::OnDraw(CDC* pDC)
{
CPrintLeftDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc); CRect rect;
GetWindowRect(&rect);
CPoint prePoint,nextPoint;
CPoint prexGrid,nextxGrid;
CPoint preyGrid,nextyGrid;
prexGrid.x=0;
prexGrid.y=0;
nextxGrid.x=rect.Width();
nextxGrid.y =0;
CPen newPen;
newPen.CreatePen(PS_DOT, 1, RGB(220, 220, 220));
CPen* pOldPen = pDC->SelectObject(&newPen);
CPen BoldLine(PS_SOLID, 1.5, RGB(220,220,220)); int x_times=3;
for(int i=0;i<rect.Height();i++)//画背景横线
{ if(i%5==0)
{
pDC->SelectObject(&BoldLine);
pDC->MoveTo(prexGrid);
pDC->LineTo(nextxGrid);
pDC->SelectObject(&newPen);
}
else
{
pDC->MoveTo(prexGrid);
pDC->LineTo(nextxGrid); }
prexGrid.y = prexGrid.y+100;//60/x_times;
nextxGrid.y = nextxGrid.y+100;//60/x_times;
} pDC->SelectObject(pOldPen); preyGrid.x=0;
preyGrid.y=0;
nextyGrid.x=0;
nextyGrid.y =rect.Height()*2; for(i=0;i <rect.Width()-1;i++)//画背景竖线
{ if(i%5==0)//5小格等于1大格
{ pDC->SelectObject(&BoldLine);
pDC->MoveTo(preyGrid);
pDC->LineTo(nextyGrid);
pDC->SelectObject(pOldPen);
}
else
{ pDC->SelectObject(&newPen);
pDC->MoveTo(preyGrid);
pDC->LineTo(nextyGrid);
pDC->SelectObject(pOldPen);
} preyGrid.x=preyGrid.x+120;
nextyGrid.x=nextyGrid.x+120;
}}
一切就绪,编译工程,运行,而后点击打印预览,或者点击打印,结果打印出来的图和显示的图不一致!
1、窗口要显示成什么样,格子数量固定还是格子的尺寸固定,单位用像素还是实际长度,坐标原点是否要居中等。
2、想打印成什么样,格子的数量和尺寸如何计算,充满整个页面还是固定长宽比例缩放。
如果能贴个当前效果图和目标效果图那就最好了。