www.gusucode.com > 几个VC++打印小例子-源码程序 > 几个VC++打印小例子-源码程序/code/Print2/PreparentDlg.cpp
// PreparentDlg.cpp : implementation file // Download by http://www.NewXing.com #include "stdafx.h" #include "Print2.h" #include "PreparentDlg.h" #include "PreviewGotoDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CPreparentDlg dialog CPreparentDlg::CPreparentDlg(CWnd* pParent /*=NULL*/) : CDialog(CPreparentDlg::IDD, pParent) { m_pPreviewDlg = NULL; m_OneCount = B5_ONELINE; m_NextCount = B5_OTHERLINE; m_nCount = 0; m_PosPage = 1; memset(&PrnInfo, 0, sizeof(PRNINFO)); //{{AFX_DATA_INIT(CPreparentDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT } void CPreparentDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CPreparentDlg) DDX_Control(pDX, IDC_TL_LIST, m_lList); DDX_Control(pDX, IDC_TL_UP, m_sUp); DDX_Control(pDX, IDC_TL_DOWN, m_sDown); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CPreparentDlg, CDialog) //{{AFX_MSG_MAP(CPreparentDlg) ON_COMMAND(IDC_TBTN_EXIT, OnTbtnExit) ON_COMMAND(IDC_TBTN_FIRST, OnTbtnFirst) ON_COMMAND(IDC_TBTN_GOTO, OnTbtnGoto) ON_COMMAND(IDC_TBTN_LAST, OnTbtnLast) ON_COMMAND(IDC_TBTN_NEXT, OnTbtnNext) ON_COMMAND(IDC_TBTN_PREVIEW, OnTbtnPreview) ON_COMMAND(IDC_TBTN_PREVIOUS, OnTbtnPrevious) ON_WM_SIZE() ON_WM_PAINT() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CPreparentDlg message handlers WNDPROC CPreparentDlg::wpListProc = NULL; HWND CPreparentDlg::hPrvWnd = NULL; LRESULT CALLBACK CPreparentDlg::ListProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { POINT pt; RECT rc; switch(uMsg) { case WM_MOUSEWHEEL: ::GetCursorPos(&pt); if(!IsWindow(hPrvWnd)) break; ::GetWindowRect(hPrvWnd, &rc); if(::PtInRect(&rc, pt)) { ::SendMessage(hPrvWnd, WM_MOUSEWHEEL, wParam, lParam); return 0; } break; } return CallWindowProc(wpListProc, hwnd, uMsg, wParam, lParam); } BOOL CPreparentDlg::OnInitDialog() { if(m_nCount<=0) { EndDialog(FALSE); return FALSE; } CDialog::OnInitDialog(); wpListProc = (WNDPROC)::SetWindowLong(m_lList.m_hWnd, GWL_WNDPROC, (LONG)ListProc); m_lList.MoveWindow(-1000, -1000, 10, 10, TRUE); //添加图标 m_hIcon = ::LoadIcon(AfxGetApp()->m_hInstance, (LPCTSTR)IDI_ICON_PREVIEW); ::SetClassLong(this->m_hWnd, GCL_HICON, (LONG)m_hIcon); ShowWindow(SW_MAXIMIZE); //添加工具条 if (!m_wndtoolbar.CreateEx(this,TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS , CRect(4,4,0,0)) || !m_wndtoolbar.LoadToolBar(IDR_TOOLBAR)) { MessageBox("创建工具栏失败!", "错误", MB_ICONSTOP); return FALSE; } m_wndtoolbar.ShowWindow(SW_SHOW); RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0); m_wndtoolbar.GetWindowRect(&m_TbRect); GetWindowRect(&rcClient); SendMessage(WM_SIZE, 0, 0); //创建子窗口 m_pPreviewDlg = new CPreviewDlg; m_pPreviewDlg->Create(IDD_PREVIEW_DIALOG, this); m_pPreviewDlg->ShowWindow(SW_SHOW); CRect rcTemp; rcTemp.SetRect(rcClient.left, m_TbRect.Height()+2, rcClient.right, rcClient.bottom); m_pPreviewDlg->SetCallBackFun(pDrawInfo, PrnInfo); m_pPreviewDlg->MoveWindow(&rcTemp); hPrvWnd = m_pPreviewDlg->m_hWnd; //初始化时为第一页 //初始时,向前、最前不可用 m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_FIRST, FALSE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_PREVIOUS, FALSE); if(m_nCount <= m_OneCount)//总行数小于等于第一页行数,则向后、最后、转页不可用 { m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_GOTO, FALSE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_NEXT, FALSE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_LAST, FALSE); } else//总行数超过一页时,向后、最后、转页可用 { m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_GOTO, TRUE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_NEXT, TRUE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_LAST, TRUE); } return TRUE; } void CPreparentDlg::OnSize(UINT nType, int cx, int cy) { CDialog::OnSize(nType, cx, cy); GetClientRect(&rcClient); CRect rup; rup.left = 0; rup.top = 0; rup.bottom = 2; rup.right = rcClient.right; if(IsWindow(m_sUp.m_hWnd)) m_sUp.MoveWindow(&rup); if(IsWindow(m_sDown.m_hWnd)) { rup.top = m_TbRect.Height(); rup.bottom = rup.top+2; m_sDown.MoveWindow(&rup); } if(m_pPreviewDlg != NULL) { if(IsWindow(m_pPreviewDlg->m_hWnd)) { CRect rcTemp; rcTemp.SetRect(rcClient.left, m_TbRect.Height()+2, rcClient.right, rcClient.bottom); m_pPreviewDlg->MoveWindow(&rcTemp); } } } BOOL CPreparentDlg::OnCommand(WPARAM wParam, LPARAM lParam) { // TODO: Add your specialized code here and/or call the base class return CDialog::OnCommand(wParam, lParam); } void CPreparentDlg::OnPaint() { CPaintDC dc(this); // device context for painting if (IsIconic()) { SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CRect rect; GetClientRect(rect); dc.FillSolidRect(rect, RGB(60,70,120)); CDialog::OnPaint(); } } void CPreparentDlg::SetCallBackFun(PRINTPREVIEW pFun, PRNINFO sPrnInfo) { memcpy(&PrnInfo, &sPrnInfo, sizeof(PRNINFO)); pDrawInfo = pFun; m_nCount = PrnInfo.nMaxLine; m_nCountPage = 1; int m = m_nCount-m_OneCount; int n = m/m_NextCount; m_nCountPage += n; n = m%m_NextCount; if(n>0) m_nCountPage++; PrnInfo.nCountPage = m_nCountPage; } void CPreparentDlg::OnTbtnExit() { // TODO: Add your command handler code here SendMessage(WM_SYSCOMMAND, SC_CLOSE, NULL); } void CPreparentDlg::OnTbtnFirst() { // TODO: Add your command handler code here m_PosPage = 1; m_pPreviewDlg->SetCurrentPage(m_nCountPage, m_PosPage); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_FIRST, FALSE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_PREVIOUS, FALSE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_NEXT, TRUE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_LAST, TRUE); UpdatePreViewWnd(); } void CPreparentDlg::OnTbtnGoto() { // TODO: Add your command handler code here int nPage = 1; int m = m_nCount-m_OneCount; int n = m/m_NextCount; nPage += n; n = m%m_NextCount; if(n>0) nPage++; CPreviewGotoDlg cpg; cpg.m_nMax = nPage; cpg.m_nCurrentPage = m_PosPage; if(cpg.DoModal()) { m_PosPage = cpg.m_nGotoPage; m_pPreviewDlg->SetCurrentPage(m_nCountPage, m_PosPage); if(m_PosPage > 1 && m_PosPage< m_nCountPage) { m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_FIRST, TRUE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_PREVIOUS, TRUE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_NEXT, TRUE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_LAST, TRUE); } if(m_PosPage == 1) OnTbtnFirst(); if(m_PosPage == m_nCountPage) OnTbtnLast(); } UpdatePreViewWnd(); } void CPreparentDlg::OnTbtnLast() { // TODO: Add your command handler code here m_PosPage = m_nCountPage; m_pPreviewDlg->SetCurrentPage(m_nCountPage, m_PosPage); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_FIRST, TRUE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_PREVIOUS, TRUE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_NEXT, FALSE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_LAST, FALSE); UpdatePreViewWnd(); } void CPreparentDlg::OnTbtnNext() { // TODO: Add your command handler code here m_PosPage++; m_pPreviewDlg->SetCurrentPage(m_nCountPage, m_PosPage); int nSpare = 0; nSpare = m_nCount - m_PosPage*m_NextCount; if(m_PosPage <= 2) nSpare +=(m_NextCount - m_OneCount); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_FIRST, TRUE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_PREVIOUS, TRUE); if(nSpare>0) { m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_NEXT, TRUE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_LAST, TRUE); } else { m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_NEXT, FALSE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_LAST, FALSE); } UpdatePreViewWnd(); } void CPreparentDlg::OnTbtnPreview() { // TODO: Add your command handler code here m_pPreviewDlg->PrintDoc(); } void CPreparentDlg::OnTbtnPrevious() { // TODO: Add your command handler code here m_PosPage--; m_pPreviewDlg->SetCurrentPage(m_nCountPage, m_PosPage); if(m_PosPage<=1) { m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_FIRST, FALSE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_PREVIOUS, FALSE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_NEXT, TRUE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_LAST, TRUE); } else { m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_FIRST, TRUE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_PREVIOUS, TRUE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_NEXT, TRUE); m_wndtoolbar.SendMessage(TB_ENABLEBUTTON, IDC_TBTN_LAST, TRUE); } UpdatePreViewWnd(); } void CPreparentDlg::UpdatePreViewWnd() { m_pPreviewDlg->SendMessage(WM_PAINT, NULL, NULL); } BOOL CPreparentDlg::DestroyWindow() { // TODO: Add your specialized code here and/or call the base class if(IsWindow(m_wndtoolbar.m_hWnd)) m_wndtoolbar.DestroyWindow(); if(m_pPreviewDlg != NULL) { m_pPreviewDlg->DestroyWindow(); delete m_pPreviewDlg; } return CDialog::DestroyWindow(); }