大家好!当我进行打印预览的进行时候,所弹出的预览窗口中“上一页”按钮始终是灰色的,当我移动到下一页以后想返回时,却无法返回。我想知道如何去控制预览窗口中的几个按钮,希望各位大侠能够鼎力相助,不胜感激!

解决方案 »

  1.   

    派生一个自己的打印预览类:
    // MyPreviewView.h : header file
    //#include "afxpriv.h"
    #include "mappedbitmapbutton.h"/////////////////////////////////////////////////////////////////////////////
    // CMyPreviewView viewclass CMyPreviewView : public CPreviewView
    {
    protected:
    CMyPreviewView();           // protected constructor used by dynamic creation
    DECLARE_DYNCREATE(CMyPreviewView)// Attributes
    public:protected:         //各个按钮
    CMappedBitmapButton m_print;
    CMappedBitmapButton m_next;
    CMappedBitmapButton m_previous;
    CMappedBitmapButton m_onetwo;
    CMappedBitmapButton m_zoomIn;
    CMappedBitmapButton m_zoomOut; BOOL m_bOne;// Operations
    public:// Overrides
    virtual void OnDisplayPageNumber(UINT nPage, UINT nPagesDisplayed); // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMyPreviewView)
    //}}AFX_VIRTUAL// Implementation
    protected:
    virtual ~CMyPreviewView();
    #ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
    #endif // Generated message map functions
    protected:
    //{{AFX_MSG(CMyPreviewView)
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    //}}AFX_MSG
    afx_msg void OnPreviewPrint();
    afx_msg void OnUpdateNumPageChange(CCmdUI* pCmdUI);
    afx_msg void OnUpdateZoomIn(CCmdUI* pCmdUI);
    afx_msg void OnUpdateZoomOut(CCmdUI* pCmdUI);
    DECLARE_MESSAGE_MAP()
    };
      

  2.   

    cpp文件:
    #include "stdafx.h"
    #include "afxpriv.h"
    #include "afxres.h"
    #include "MyPreviewView.h"
    #include "resource.h"IMPLEMENT_DYNCREATE(CMyPreviewView, CPreviewView)#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CMyPreviewView
    CMyPreviewView::CMyPreviewView()
    {
    }CMyPreviewView::~CMyPreviewView()
    {
    }void CMyPreviewView::OnDisplayPageNumber(UINT nPage, UINT nPagesDisplayed)
    {
    CPreviewView::OnDisplayPageNumber( nPage, nPagesDisplayed );
    }void CMyPreviewView::OnPreviewPrint()
    {
    CPreviewView::OnPreviewPrint();
    }BEGIN_MESSAGE_MAP(CMyPreviewView, CPreviewView)
    //{{AFX_MSG_MAP(CMyPreviewView)
    ON_WM_CREATE()
    //}}AFX_MSG_MAP
    ON_COMMAND(AFX_ID_PREVIEW_PRINT, OnPreviewPrint)
    ON_UPDATE_COMMAND_UI(AFX_ID_PREVIEW_NUMPAGE, OnUpdateNumPageChange)
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CMyPreviewView drawing/////////////////////////////////////////////////////////////////////////////
    // CMyPreviewView diagnostics#ifdef _DEBUG
    void CMyPreviewView::AssertValid() const
    {
    CPreviewView::AssertValid();
    }void CMyPreviewView::Dump(CDumpContext& dc) const
    {
    CPreviewView::Dump(dc);
    }
    #endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
    // CMyPreviewView message handlersvoid CMyPreviewView::OnUpdateNumPageChange(CCmdUI* pCmdUI)
    {
    UINT nPages = m_nZoomState == ZOOM_OUT ? m_nPages : m_nZoomOutPages; if (m_bOne) {
    if (nPages == 1) {
    // need to swap to show 2 pages
    m_onetwo.LoadBitmap( IDB_PREV_TWO );
    m_bOne = FALSE;
    m_onetwo.Invalidate();
    }
    }
    else {
    if (nPages != 1) {
    // need to swap to show 1 page
    m_onetwo.LoadBitmap( IDB_PREV_ONE );
    m_bOne = TRUE;
    m_onetwo.Invalidate();
    }
    } // enable it only if valid to display another page and not zoomed
    pCmdUI->Enable(m_nZoomState == ZOOM_OUT && m_nMaxPages != 1 &&
    (m_pPreviewInfo->GetMaxPage() > 1 || m_nPages > 1));
    }int CMyPreviewView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    if (CPreviewView::OnCreate(lpCreateStruct) == -1)
    return -1; m_pToolBar->EnableToolTips( TRUE ); m_bOne = FALSE;  // the default is to show 2 pages, set in the dialog text m_print.AutoLoad( AFX_ID_PREVIEW_PRINT, m_pToolBar, IDB_PREV_PRINT );
    m_next.AutoLoad( AFX_ID_PREVIEW_NEXT, m_pToolBar, IDB_PREV_NEXT );
    m_previous.AutoLoad( AFX_ID_PREVIEW_PREV, m_pToolBar, IDB_PREV_PREVIOUS );
    m_onetwo.AutoLoad( AFX_ID_PREVIEW_NUMPAGE, m_pToolBar, IDB_PREV_TWO );
    m_zoomIn.AutoLoad( AFX_ID_PREVIEW_ZOOMIN, m_pToolBar, IDB_PREV_ZOOMIN );
    m_zoomOut.AutoLoad( AFX_ID_PREVIEW_ZOOMOUT, m_pToolBar, IDB_PREV_ZOOMOUT );

    return 0;
    }
      

  3.   

    上面的代码中的IDB_PREV_PRINT 、IDB_PREV_NEXT等等都是自己导入工程的位图,也就是那些按钮,你可以用你喜欢的样式的,^_^
    在自己的视图中加入一个函数:
    void CMyView::MyPrintPreview()
    {
    CPrintPreviewState* pState = new CPrintPreviewState;
    if (!DoPrintPreview(IDD_PREVIEW, this,
    RUNTIME_CLASS(CMyPreviewView), pState))
    {
    // In derived classes, reverse special window handling here for
    // Preview failure case
    TRACE0("Error: DoPrintPreview failed.\n");
    AfxMessageBox(AFX_IDP_COMMAND_FAILURE);
    delete pState;      // preview failed to initialize, delete State now
    }
    }在需要的地方调用这个MyPrintPreview()函数,就可以预览了
    帖了这么多,累死我了
      

  4.   

    Mark先。  有工夫我也试试。
    TO yuliangpei(踏雪无痕):你设置了打印的最大页数了吗?SetMaxPage()
      

  5.   

    to jinxulei(石头)我没有设置SetMaxPage(),我也查了一些资料。上面也有说需要设置这个,但我想它应该提供一个接口给我们,让我们来设定它。