⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cprintpreview.cpp

📁 日本的开源编辑器源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/*!	@file
	@brief 印刷プレビュー管理クラス

	@author YAZAKI
	@date 2002/1/11 新規作成
	$Revision: 1.22 $
*/
/*
	Copyright (C) 2002, YAZAKI, aroka, MIK

	This source code is designed for sakura editor.
	Please contact the copyright holder to use this code for other purpose.
*/

#include "CPrintPreview.h"
#include "CLayout.h"
#include "CEditWnd.h"
#include "charcode.h"
#include "CDlgPrintPage.h"
#include "CDlgCancel.h"/// 2002/2/3 aroka from here
#include "Debug.h"///
#include "etc_uty.h"///
#include <stdio.h>/// 2002/2/3 aroka to here

#define MIN_PREVIEW_ZOOM 10
#define MAX_PREVIEW_ZOOM 400

#define		LINE_RANGE_X	48		/* 水平方向の1回のスクロール幅 */
#define		LINE_RANGE_Y	24		/* 垂直方向の1回のスクロール幅 */

#define		PAGE_RANGE_X	160		/* 水平方向の1回のページスクロール幅 */
#define		PAGE_RANGE_Y	160		/* 垂直方向の1回のページスクロール幅 */

CPrint CPrintPreview::m_cPrint;		//!< 現在のプリンタ情報 2003.05.02 かろと

/*! コンストラクタ
	印刷プレビューを表示するために必要な情報を初期化、領域確保。
	コントロールも作成する。
*/
CPrintPreview::CPrintPreview(CEditWnd* pParentWnd ) :
	m_nPreview_Zoom( 100 ),			/* 印刷プレビュー倍率 */
	m_nPreviewVScrollPos( 0 ),
	m_nPreviewHScrollPos( 0 ),
	m_nCurPageNum( 0 ),				/* 現在のページ */
	m_pParentWnd( pParentWnd )
{
	/* 印刷用のレイアウト情報の作成 */
	m_pLayoutMgr_Print = new CLayoutMgr;

	/* 印刷プレビュー コントロール 作成 */
	CreatePrintPreviewControls();
}

CPrintPreview::~CPrintPreview()
{
	/* 印刷プレビュー コントロール 破棄 */
	DestroyPrintPreviewControls();
	
	/* 印刷用のレイアウト情報の削除 */
	delete m_pLayoutMgr_Print;
}

/*!	印刷プレビュー時の、WM_PAINTを処理
*/
LRESULT CPrintPreview::OnPaint(
	HWND			hwnd,	// handle of window
	UINT			uMsg,	// message identifier
	WPARAM			wParam,	// first message parameter
	LPARAM			lParam 	// second message parameter
)
{
	PAINTSTRUCT		ps;
	HDC				hdcOld = ::BeginPaint( hwnd, &ps );
	HDC				hdc = m_pParentWnd->m_hdcCompatDC;	//	親ウィンドウのComatibleDCに描く

	/* 印刷プレビュー 操作バー */
	RECT			rc;
	::GetClientRect( hwnd, &rc );
	::FillRect( hdc, &rc, (HBRUSH)::GetStockObject( GRAY_BRUSH ) );

	int nToolBarHeight = 0;
	if( NULL != m_hwndPrintPreviewBar ){
		::GetWindowRect( m_hwndPrintPreviewBar, &rc );
		nToolBarHeight = rc.bottom - rc.top;
	}

	/* プリンタ情報の表示 */
	char	szText[1024];
	char	szPaperName[256];
	::SetDlgItemText( m_hwndPrintPreviewBar, IDC_STATIC_PRNDEV, m_pPrintSetting->m_mdmDevMode.m_szPrinterDeviceName );
	m_cPrint.GetPaperName( m_pPrintSetting->m_mdmDevMode.dmPaperSize , (char*)szPaperName );
	wsprintf( szText, "%s  %s",
		szPaperName,
		(m_pPrintSetting->m_mdmDevMode.dmOrientation & DMORIENT_LANDSCAPE) ? "横" : "縦"
	);
	::SetDlgItemText( m_hwndPrintPreviewBar, IDC_STATIC_PAPER, szText );

	/* バックグラウンド モードを変更 */
	::SetBkMode( hdc, TRANSPARENT );

	/* マッピングモードの変更 */
	int nMapModeOld =
	::SetMapMode( hdc, MM_LOMETRIC );
	::SetMapMode( hdc, MM_ANISOTROPIC );

	/* 出力倍率の変更 */
	SIZE			sz;
	::GetWindowExtEx( hdc, &sz );
	int nCx = sz.cx;
	int nCy = sz.cy;
	nCx = (int)( ((long)nCx) * 100L / ((long)m_nPreview_Zoom) );
	nCy = (int)( ((long)nCy) * 100L / ((long)m_nPreview_Zoom) );
	::SetWindowExtEx( hdc, nCx, nCy, &sz );

	/* 印刷フォント作成 & 設定 */
	m_lfPreviewHan.lfHeight	= m_pPrintSetting->m_nPrintFontHeight;
	m_lfPreviewHan.lfWidth	= m_pPrintSetting->m_nPrintFontWidth;
	strcpy( m_lfPreviewHan.lfFaceName, m_pPrintSetting->m_szPrintFontFaceHan );

	m_lfPreviewZen.lfHeight	= m_pPrintSetting->m_nPrintFontHeight;
	m_lfPreviewZen.lfWidth	= m_pPrintSetting->m_nPrintFontWidth;
	strcpy( m_lfPreviewZen.lfFaceName, m_pPrintSetting->m_szPrintFontFaceZen );

	/* 印刷用半角フォントと全角フォントを生成 */
	HFONT	hFontHan = CreateFontIndirect( &m_lfPreviewHan );
	HFONT	hFontZen = CreateFontIndirect( &m_lfPreviewZen );

	/* 印刷用半角フォントに設定し、以前のフォントを保持 */
	HFONT	hFontOld = (HFONT)::SelectObject( hdc, hFontHan );

	/* 操作ウィンドウの下に物理座標原点を移動 */
	POINT			poViewPortOld;
	::SetViewportOrgEx( hdc, -1 * m_nPreviewHScrollPos, nToolBarHeight + m_nPreviewVScrollPos, &poViewPortOld );

	/* 用紙の描画 */
	int	nDirectY = -1;	//	Y座標の下をプラス方向にするため?
	::Rectangle( hdc,
		m_nPreview_ViewMarginLeft,
		nDirectY * ( m_nPreview_ViewMarginTop ),
		m_nPreview_ViewMarginLeft + m_nPreview_PaperAllWidth + 1,
		nDirectY * (m_nPreview_ViewMarginTop + m_nPreview_PaperAllHeight + 1 )
	);
	/* マージン枠の表示 */
	HPEN			hPen, hPenOld;
	hPen = ::CreatePen( PS_SOLID, 0, RGB(127,127,127) );
	hPenOld = (HPEN)::SelectObject( hdc, hPen );
	::Rectangle( hdc,
		m_nPreview_ViewMarginLeft + m_pPrintSetting->m_nPrintMarginLX,
		nDirectY * ( m_nPreview_ViewMarginTop + m_pPrintSetting->m_nPrintMarginTY ),
		m_nPreview_ViewMarginLeft + m_nPreview_PaperAllWidth - m_pPrintSetting->m_nPrintMarginRX + 1,
		nDirectY * ( m_nPreview_ViewMarginTop + m_nPreview_PaperAllHeight - m_pPrintSetting->m_nPrintMarginBY )
	);
	::SelectObject( hdc, hPenOld );
	::DeleteObject( hPen );

	::SetTextColor( hdc, RGB( 0, 0, 0 ) );

	RECT cRect;	/* 紙の大きさをあらわすRECT */
	cRect.left   = m_nPreview_ViewMarginLeft +                             m_pPrintSetting->m_nPrintMarginLX + 5;
	cRect.right  = m_nPreview_ViewMarginLeft + m_nPreview_PaperAllWidth - (m_pPrintSetting->m_nPrintMarginRX + 5);
	cRect.top    = nDirectY * ( m_nPreview_ViewMarginTop +                              m_pPrintSetting->m_nPrintMarginTY + 5);
	cRect.bottom = nDirectY * ( m_nPreview_ViewMarginTop + m_nPreview_PaperAllHeight - (m_pPrintSetting->m_nPrintMarginBY + 5));

	/* ヘッダ */
	DrawHeader( hdc, cRect, hFontZen );

	/* 印刷/印刷プレビュー ページテキストの描画 */
	DrawPageText(
		hdc,
		m_nPreview_ViewMarginLeft + m_pPrintSetting->m_nPrintMarginLX,
		m_nPreview_ViewMarginTop  + m_pPrintSetting->m_nPrintMarginTY + 2 * ( m_pPrintSetting->m_nPrintFontHeight + (m_pPrintSetting->m_nPrintFontHeight * m_pPrintSetting->m_nPrintLineSpacing / 100) ),
		m_nCurPageNum,
		hFontZen,
		NULL
	);

	DrawFooter( hdc, cRect, hFontZen );

	/* 印刷フォント解除 & 破棄 */
	::SelectObject( hdc, hFontOld );
	::DeleteObject( hFontZen );
	::DeleteObject( hFontHan );

	/* マッピングモードの変更 */
	::SetMapMode( hdc, nMapModeOld );

	/* 物理座標原点をもとに戻す */
	::SetViewportOrgEx( hdc, poViewPortOld.x, poViewPortOld.y, NULL );


	/* メモリDCを利用した再描画の場合はメモリDCに描画した内容を画面へコピーする */
	rc = ps.rcPaint;
	::DPtoLP( hdc, (POINT*)&rc, 2 );
	::BitBlt(
		hdcOld,
		ps.rcPaint.left,
		ps.rcPaint.top,
		ps.rcPaint.right - ps.rcPaint.left,
		ps.rcPaint.bottom - ps.rcPaint.top,
		hdc,
		ps.rcPaint.left,
		ps.rcPaint.top,
		SRCCOPY
	);
	::EndPaint( hwnd, &ps );
	return 0L;
}

LRESULT CPrintPreview::OnSize( WPARAM wParam, LPARAM lParam )
{
	int	cx = LOWORD( lParam );
	int	cy = HIWORD( lParam );

	/* 印刷プレビュー 操作バー */
	int nToolBarHeight = 0;
	if( NULL != m_hwndPrintPreviewBar ){
		RECT			rc;
		::GetWindowRect( m_hwndPrintPreviewBar, &rc );
		nToolBarHeight = rc.bottom - rc.top;
		::MoveWindow( m_hwndPrintPreviewBar, 0, 0, cx, nToolBarHeight, TRUE );
	}

	/* 印刷プレビュー 垂直スクロールバーウィンドウ */
	int	nCxVScroll = ::GetSystemMetrics( SM_CXVSCROLL );
	int	nCyVScroll = ::GetSystemMetrics( SM_CYVSCROLL );
	if( NULL != m_hwndVScrollBar ){
		::MoveWindow( m_hwndVScrollBar, cx - nCxVScroll, nToolBarHeight, nCxVScroll, cy - nCyVScroll - nToolBarHeight, TRUE );
	}
	
	/* 印刷プレビュー 水平スクロールバーウィンドウ */
	int	nCxHScroll = ::GetSystemMetrics( SM_CXHSCROLL );
	int	nCyHScroll = ::GetSystemMetrics( SM_CYHSCROLL );
	if( NULL != m_hwndHScrollBar ){
		::MoveWindow( m_hwndHScrollBar, 0, cy - nCyHScroll, cx - nCxVScroll, nCyHScroll, TRUE );
	}
	
	/* 印刷プレビュー サイズボックスウィンドウ */
	if( NULL != m_hwndSizeBox){
		::MoveWindow( m_hwndSizeBox, cx - nCxVScroll, cy - nCyHScroll, nCxHScroll, nCyVScroll, TRUE );
	}

	HDC			hdc = ::GetDC( m_pParentWnd->m_hWnd );
	::SetMapMode( hdc, MM_LOMETRIC );
	::SetMapMode( hdc, MM_ANISOTROPIC );

	/* 出力倍率の変更 */
	SIZE		sz;
	::GetWindowExtEx( hdc, &sz );
	int nCx = sz.cx;
	int nCy = sz.cy;
	nCx = (int)( ((long)nCx) * 100L / ((long)m_nPreview_Zoom) );
	nCy = (int)( ((long)nCy) * 100L / ((long)m_nPreview_Zoom) );
	::SetWindowExtEx( hdc, nCx, nCy, &sz );

	/* ビューのサイズ */
	POINT		po;
	po.x = m_nPreview_PaperAllWidth + m_nPreview_ViewMarginLeft * 2;
	po.y = m_nPreview_PaperAllHeight + m_nPreview_ViewMarginTop * 2;
	::LPtoDP( hdc, &po, 1 );

	/* 再描画用メモリBMP */
	if( m_pParentWnd->m_hbmpCompatBMP != NULL ){
		::SelectObject( m_pParentWnd->m_hdcCompatDC, m_pParentWnd->m_hbmpCompatBMPOld );	/* 再描画用メモリBMP(OLD) */
		::DeleteObject( m_pParentWnd->m_hbmpCompatBMP );
	}
	m_pParentWnd->m_hbmpCompatBMP = ::CreateCompatibleBitmap( hdc, cx, cy );
	m_pParentWnd->m_hbmpCompatBMPOld = (HBITMAP)::SelectObject( m_pParentWnd->m_hdcCompatDC, m_pParentWnd->m_hbmpCompatBMP );


	::ReleaseDC( m_pParentWnd->m_hWnd, hdc );

	/* 印刷プレビュー:ビュー幅(ピクセル) */
	m_nPreview_ViewWidth = abs( po.x );
	
	/* 印刷プレビュー:ビュー高さ(ピクセル) */
	m_nPreview_ViewHeight = abs( po.y );
	
	/* 印刷プレビュー スクロールバー初期化 */
	InitPreviewScrollBar();
	
	/* 印刷プレビュー スクロールバーの初期化 */
	
	m_pParentWnd->m_nDragPosOrgX = 0;
	m_pParentWnd->m_nDragPosOrgY = 0;
	m_pParentWnd->m_bDragMode = TRUE;
	OnMouseMove( 0, MAKELONG( 0, 0 ) );
	m_pParentWnd->m_bDragMode = FALSE;
	//	SizeBox問題テスト
	if( NULL != m_hwndSizeBox ){
		if( wParam == SIZE_MAXIMIZED ){
			::ShowWindow( m_hwndSizeBox, SW_HIDE );
		}else
		if( wParam == SIZE_RESTORED ){
			if( ::IsZoomed( m_pParentWnd->m_hWnd ) ){
				::ShowWindow( m_hwndSizeBox, SW_HIDE );
			}else{
				::ShowWindow( m_hwndSizeBox, SW_SHOW );
			}
		}else{
			::ShowWindow( m_hwndSizeBox, SW_SHOW );
		}
	}
	::InvalidateRect( m_pParentWnd->m_hWnd, NULL, TRUE );
	return 0L;
}

LRESULT CPrintPreview::OnVScroll( WPARAM wParam, LPARAM lParam )
{
	int			nPreviewVScrollPos;
	SCROLLINFO	si;
	int			nNowPos;
	int			nMove;
	int			nNewPos;
	int			nScrollCode;
	int			nPos;
	HWND		hwndScrollBar;
	nScrollCode = (int) LOWORD(wParam);
	nPos = (int) HIWORD(wParam);
	hwndScrollBar = (HWND) lParam;
	si.cbSize = sizeof( SCROLLINFO );
	si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
	::GetScrollInfo( hwndScrollBar, SB_CTL, (SCROLLINFO*)&si );
	nNowPos = ::GetScrollPos( hwndScrollBar, SB_CTL );
	nNewPos = 0;
	nMove = 0;
	switch( nScrollCode ){
	case SB_LINEUP:
		nMove = -1 * LINE_RANGE_Y;
		break;
	case SB_LINEDOWN:
		nMove = LINE_RANGE_Y;
		break;
	case SB_PAGEUP:
		nMove = -1 * PAGE_RANGE_Y;
		break;
	case SB_PAGEDOWN:
		nMove = PAGE_RANGE_Y;
		break;
	case SB_THUMBPOSITION:
	case SB_THUMBTRACK:
		nMove = nPos - nNowPos;
		break;
	default:
		return 0;
	}
	nNewPos = nNowPos + nMove;
	if( nNewPos < 0 ){
		nNewPos = 0;
	}else
	if( nNewPos > (int)(si.nMax - si.nPage + 1) ){
		nNewPos = (int)(si.nMax - si.nPage + 1);
	}
	nMove = nNowPos - nNewPos;
	nPreviewVScrollPos = -1 * nNewPos;
	if( nPreviewVScrollPos != m_nPreviewVScrollPos ){
		::SetScrollPos( hwndScrollBar, SB_CTL, nNewPos, TRUE);
		m_nPreviewVScrollPos = nPreviewVScrollPos;
		/* 描画 */
		::ScrollWindowEx( m_pParentWnd->m_hWnd, 0, nMove, NULL, NULL, NULL , NULL, SW_ERASE | SW_INVALIDATE );
	}
	return 0;
}

LRESULT CPrintPreview::OnHScroll( WPARAM wParam, LPARAM lParam )
{
	int			nPreviewHScrollPos;
	SCROLLINFO	si;
	int			nNowPos;
	int			nMove;
	int			nNewPos;
	int			nScrollCode;
	int			nPos;
	HWND		hwndScrollBar;
	nScrollCode = (int) LOWORD(wParam);
	nPos = (int) HIWORD(wParam);
	hwndScrollBar = (HWND) lParam;
	si.cbSize = sizeof( SCROLLINFO );
	si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE | SIF_TRACKPOS;
	::GetScrollInfo( hwndScrollBar, SB_CTL, (SCROLLINFO*)&si );
	nNowPos = ::GetScrollPos( hwndScrollBar, SB_CTL );
	nMove = 0;
	switch( nScrollCode ){
	case SB_LINEUP:
		nMove = -1 * LINE_RANGE_Y;
		break;
	case SB_LINEDOWN:
		nMove = LINE_RANGE_Y;
		break;
	case SB_PAGEUP:
		nMove = -1 * PAGE_RANGE_Y;
		break;
	case SB_PAGEDOWN:
		nMove = PAGE_RANGE_Y;
		break;
	case SB_THUMBPOSITION:
	case SB_THUMBTRACK:
		nMove = nPos - nNowPos;
		break;
	default:
		return 0;
	}
	nNewPos = nNowPos + nMove;
	if( nNewPos < 0 ){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -