MFC Grid control 2.26打印出/入库单的实现

来源:互联网 发布:goagent php 编辑:程序博客网 时间:2024/04/30 11:27

程序中需要实现打印出库单和入库单,本来以为挺简单的,因为Grid Control本身提供了print()函数,只需要新建一个对话框,添加上Grid Control,根据需要的格式打印即可。在实际实现时发现存在较大的问题,从网上找到比较好的出库单和入库单的格式并不容易实现,特别是有标题,还有单号、仓库名称、时间等单子的信息,这与单内的数据项不一致,而Grid Control仅仅能打印其内部的数据,这样一来除非是将两个Grid的内容打印在同一张纸上,否则不能利用其本身的print()函数。

     在目前的情况下,想偷懒不再做大的修改,在Grid Control本身的print()函数上来实现打印出/入库单。考虑了一下,只能到CGridCtrl中修改代码,因为有的时候还需要直接打印Grid Control内的数据,比如查询的结果,这样需要保持其他时候维持原样,只在打印出/入库单时才让我修改的代码起作用。

因为要求不是很高,简单实现出/入库单的打印,效果如下:

                                                    入库单

单号: XXX                      仓库: XXX             时间: 2010-07-08 17:13:23

------------------------------------------------------------------------------------------

| Headers……

------------------------------------------------------------------------------------------

.

.

.

------------------------------------------------------------------------------------------

备注: XXX

 

 

 

 

 

------------------------------------------------------------------------------------------

Page 1 of 1                                                                 07/08/10 17:13:23

实现步骤

Step1:添加变量

在GridCtrl.h中添加如下变量:

    BOOL m_bPrintBill;
    CString m_strTitle;
    CString m_strBillNo;
    CString m_strWareHouse;
    CString m_strDateTime;
    CString m_strDrawer;
    CString m_strRemark;
其中m_bPrintBill作为是否打印出/入库单的开关标志,只在打印出/入库单的时候置位。后面几个分别是出/入库单的信息,备注显示在出/入库单项目的下方,其他的内容显示在标题下方。

Step2:初始化变量

在GridCtrl的构造函数中初始化变量,m_bPrintBill初始化为FALSE,其他的字符串都初始化为空。

Step3:添加打印函数

原来的print()函数是在页面上方打印文件名字,这样要添加三个函数,分别打印标题(出/入库单);出/入库单信息;备注。

void CGridCtrl::PrintBillTitle(CDC *pDC, CPrintInfo *pInfo)
{   
    CFont   BoldFont;
    LOGFONT lf;
   
    //create bold font for header and footer
    VERIFY(m_PrinterFont.GetLogFont(&lf));
    lf.lfWeight = FW_BOLD;
    VERIFY(BoldFont.CreateFontIndirect(&lf));
   
    CFont *pNormalFont = pDC->SelectObject(&BoldFont);
    int nPrevBkMode = pDC->SetBkMode(TRANSPARENT);
   
    CRect   rc(pInfo->m_rectDraw);
    if (!m_strTitle.IsEmpty())
    {
        pDC->DrawText( m_strTitle, &rc, DT_CENTER | DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER);
    }
   
    pDC->SetBkMode(nPrevBkMode);
    pDC->SelectObject(pNormalFont);
    BoldFont.DeleteObject();   
}

void CGridCtrl::PrintBillInfo(CDC *pDC, CPrintInfo *pInfo)
{
    CFont   BoldFont;
    LOGFONT lf;
   
    //create bold font for header and footer
    VERIFY(m_PrinterFont.GetLogFont(&lf));
    lf.lfWeight = FW_BOLD;
    VERIFY(BoldFont.CreateFontIndirect(&lf));
   
    CFont *pNormalFont = pDC->SelectObject(&BoldFont);
    int nPrevBkMode = pDC->SetBkMode(TRANSPARENT);
   
    CRect   rc(pInfo->m_rectDraw);
    CString strVal;
    if (!m_strBillNo.IsEmpty())
    {
        strVal.Format("单号: %s", m_strBillNo);
        pDC->DrawText( strVal, &rc, DT_LEFT | DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER);
    }
    if (!m_strWareHouse.IsEmpty())
    {       
        if (!m_strDrawer.IsEmpty())
        {
            strVal.Format("仓库: %s    领料人: %s", m_strWareHouse, m_strDrawer);
        }
        else
        {
            strVal.Format("仓库: %s", m_strWareHouse);
        }
        pDC->DrawText( strVal, &rc, DT_CENTER | DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER);
    }
    if (!m_strDateTime.IsEmpty())
    {
        strVal.Format("时间: %s", m_strDateTime);
        pDC->DrawText( strVal, &rc, DT_RIGHT | DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER);
    }

    pDC->SetBkMode(nPrevBkMode);
    pDC->SelectObject(pNormalFont);
    BoldFont.DeleteObject();
}

void CGridCtrl::PrintRemark(CDC *pDC, CPrintInfo *pInfo)
{
    CFont   BoldFont;
    LOGFONT lf;
   
    //create bold font for header and footer
    VERIFY(m_PrinterFont.GetLogFont(&lf));
    lf.lfWeight = FW_BOLD;
    VERIFY(BoldFont.CreateFontIndirect(&lf));
   
    CFont *pNormalFont = pDC->SelectObject(&BoldFont);
    int nPrevBkMode = pDC->SetBkMode(TRANSPARENT);
   
    CRect   rc(pInfo->m_rectDraw);
    CString strVal;
    if (!m_strRemark.IsEmpty())
    {
        strVal.Format("备注: %s", m_strRemark);
        pDC->DrawText( strVal, &rc, DT_LEFT | DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER);
    }
   
    pDC->SetBkMode(nPrevBkMode);
    pDC->SelectObject(pNormalFont);
    BoldFont.DeleteObject();

}

Step 4:修改CGridCtrl::OnPrint(CDC *pDC, CPrintInfo *pInfo)函数

在原来调用PrintHeader()函数的地方,修改:

if (m_bPrintBill)
    {
        PrintBillTitle(pDC, pInfo);

        pDC->OffsetWindowOrg(0, -m_nHeaderHeight * m_CharSize.cy);

        pInfo->m_rectDraw.bottom = m_nGap *m_CharSize.cy;
        PrintBillInfo(pDC, pInfo);
    }
    else
    {
        PrintHeader(pDC, pInfo);
        pDC->OffsetWindowOrg(0, -m_nHeaderHeight * m_CharSize.cy);
    }

原创粉丝点击