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

📄 ruler.cpp

📁 VC++写的仿Windows系统的写字板程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ruler.cpp : implementation file
// Download by http://www.codefans.net
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.

#include "stdafx.h"
#include "wordpad.h"
#include "ruler.h"
#include "wordpvw.h"
#include "wordpdoc.h"
#include "strings.h"
#include <memory.h>

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

#define HEIGHT 17
#define RULERBARHEIGHT 17

CRulerItem::CRulerItem(UINT nBitmapID)
{
	m_nAlignment = TA_CENTER;
	m_pDC = NULL;
	m_bTrack = FALSE;
	m_hbm = NULL;
	m_hbmMask = NULL;
	if (nBitmapID != 0)
	{
		m_hbmMask = ::LoadBitmap(
			AfxFindResourceHandle(MAKEINTRESOURCE(nBitmapID+1), RT_BITMAP),
			MAKEINTRESOURCE(nBitmapID+1));
		ASSERT(m_hbmMask != NULL);
		VERIFY(LoadMaskedBitmap(MAKEINTRESOURCE(nBitmapID)));
		BITMAP bm;
		::GetObject(m_hbm, sizeof(BITMAP), &bm);
		m_size = CSize(bm.bmWidth, bm.bmHeight);
	}
}

CRulerItem::~CRulerItem()
{
	if (m_hbm != NULL)
		::DeleteObject(m_hbm);
	if (m_hbmMask != NULL)
		::DeleteObject(m_hbmMask);
}

BOOL CRulerItem::LoadMaskedBitmap(LPCTSTR lpszResourceName)
{
	ASSERT(lpszResourceName != NULL);

	if (m_hbm != NULL)
		::DeleteObject(m_hbm);

	HINSTANCE hInst = AfxFindResourceHandle(lpszResourceName, RT_BITMAP);
	HRSRC hRsrc = ::FindResource(hInst, lpszResourceName, RT_BITMAP);
	if (hRsrc == NULL)
		return FALSE;

	m_hbm = AfxLoadSysColorBitmap(hInst, hRsrc);
	return (m_hbm != NULL);
}

void CRulerItem::SetHorzPosTwips(int nXPos)
{
	if (GetHorzPosTwips() != nXPos)
	{
		if (m_bTrack)
			DrawFocusLine();
		Invalidate();
		m_nXPosTwips = nXPos;
		Invalidate();
		if (m_bTrack)
			DrawFocusLine();
	}
}

void CRulerItem::TrackHorzPosTwips(int nXPos, BOOL /*bOnRuler*/)
{
	int nMin = GetMin();
	int nMax = GetMax();
	if (nXPos < nMin)
		nXPos = nMin;
	if (nXPos > nMax)
		nXPos = nMax;
	SetHorzPosTwips(nXPos);
}

void CRulerItem::DrawFocusLine()
{
	if (GetHorzPosTwips() != 0)
	{
		m_rcTrack.left = m_rcTrack.right = GetHorzPosPix();
		ASSERT(m_pDC != NULL);
		int nLeft = m_pRuler->XRulerToClient(m_rcTrack.left);
		m_pDC->MoveTo(nLeft, m_rcTrack.top);
		m_pDC->LineTo(nLeft, m_rcTrack.bottom);
	}
}

void CRulerItem::SetTrack(BOOL b)
{
	m_bTrack = b;

	if (m_pDC != NULL) // just in case we lost focus Capture somewhere
	{
		DrawFocusLine();
		m_pDC->RestoreDC(-1);
		delete m_pDC ;
		m_pDC = NULL;
	}
	if (m_bTrack)
	{
		CWordPadView* pView = (CWordPadView*)m_pRuler->GetView();
		ASSERT(pView != NULL);
		pView->GetClientRect(&m_rcTrack);
		m_pDC = new CWindowDC(pView);
		m_pDC->SaveDC();
		m_pDC->SelectObject(&m_pRuler->penFocusLine);
		m_pDC->SetROP2(R2_XORPEN);
		DrawFocusLine();
	}
}

void CRulerItem::Invalidate()
{
	CRect rc = GetHitRectPix();
	m_pRuler->RulerToClient(rc.TopLeft());
	m_pRuler->RulerToClient(rc.BottomRight());
	m_pRuler->InvalidateRect(rc);
}

CRect CRulerItem::GetHitRectPix()
{
	int nx = GetHorzPosPix();
	return CRect(
		CPoint(
			(m_nAlignment == TA_CENTER) ? (nx - m_size.cx/2) :
			(m_nAlignment == TA_LEFT) ? nx : nx - m_size.cx
			, m_nYPosPix
			),
		m_size);
}

void CRulerItem::Draw(CDC& dc)
{
	CDC dcBitmap;
	dcBitmap.CreateCompatibleDC(&dc);
	CPoint pt(GetHorzPosPix(), GetVertPosPix());

	HGDIOBJ hbm = ::SelectObject(dcBitmap.m_hDC, m_hbmMask);

	// do mask part
	if (m_nAlignment == TA_CENTER)
		dc.BitBlt(pt.x - m_size.cx/2, pt.y, m_size.cx, m_size.cy, &dcBitmap, 0, 0, SRCAND);
	else if (m_nAlignment == TA_LEFT)
		dc.BitBlt(pt.x, pt.y, m_size.cx, m_size.cy, &dcBitmap, 0, 0, SRCAND);
	else // TA_RIGHT
		dc.BitBlt(pt.x - m_size.cx, pt.y, m_size.cx, m_size.cy, &dcBitmap, 0, 0, SRCAND);

	// do image part
	::SelectObject(dcBitmap.m_hDC, m_hbm);

	if (m_nAlignment == TA_CENTER)
		dc.BitBlt(pt.x - m_size.cx/2, pt.y, m_size.cx, m_size.cy, &dcBitmap, 0, 0, SRCINVERT);
	else if (m_nAlignment == TA_LEFT)
		dc.BitBlt(pt.x, pt.y, m_size.cx, m_size.cy, &dcBitmap, 0, 0, SRCINVERT);
	else // TA_RIGHT
		dc.BitBlt(pt.x - m_size.cx, pt.y, m_size.cx, m_size.cy, &dcBitmap, 0, 0, SRCINVERT);

	::SelectObject(dcBitmap.m_hDC, hbm);
}

CComboRulerItem::CComboRulerItem(UINT nBitmapID1, UINT nBitmapID2, CRulerItem& item)
	: CRulerItem(nBitmapID1), m_secondary(nBitmapID2) , m_link(item)
{
	m_bHitPrimary = TRUE;
}

BOOL CComboRulerItem::HitTestPix(CPoint pt)
{
	m_bHitPrimary = FALSE;
	if (CRulerItem::GetHitRectPix().PtInRect(pt))
		m_bHitPrimary = TRUE;
	else
		return m_secondary.HitTestPix(pt);
	return TRUE;
}

void CComboRulerItem::Draw(CDC& dc)
{
	CRulerItem::Draw(dc);
	m_secondary.Draw(dc);
}

void CComboRulerItem::SetHorzPosTwips(int nXPos)
{
	if (m_bHitPrimary) // only change linked items by delta
		m_link.SetHorzPosTwips(m_link.GetHorzPosTwips() + nXPos - GetHorzPosTwips());
	CRulerItem::SetHorzPosTwips(nXPos);
	m_secondary.SetHorzPosTwips(nXPos);
}

void CComboRulerItem::TrackHorzPosTwips(int nXPos, BOOL /*bOnRuler*/)
{
	int nMin = GetMin();
	int nMax = GetMax();
	if (nXPos < nMin)
		nXPos = nMin;
	if (nXPos > nMax)
		nXPos = nMax;
	SetHorzPosTwips(nXPos);
}

void CComboRulerItem::SetVertPos(int nYPos)
{
	m_secondary.SetVertPos(nYPos);
	nYPos += m_secondary.GetHitRectPix().Height();
	CRulerItem::SetVertPos(nYPos);
}

void CComboRulerItem::SetAlignment(int nAlign)
{
	CRulerItem::SetAlignment(nAlign);
	m_secondary.SetAlignment(nAlign);
}

void CComboRulerItem::SetRuler(CRulerBar* pRuler)
{
	m_pRuler = pRuler;
	m_secondary.SetRuler(pRuler);
}

void CComboRulerItem::SetBounds(int nMin, int nMax)
{
	CRulerItem::SetBounds(nMin, nMax);
	m_secondary.SetBounds(nMin, nMax);
}

int CComboRulerItem::GetMin()
{
	if (m_bHitPrimary)
	{
		int nPDist = GetHorzPosTwips() - CRulerItem::GetMin();
		int nLDist = m_link.GetHorzPosTwips() - m_link.GetMin();
		return GetHorzPosTwips() - min(nPDist, nLDist);
	}
	else
		return CRulerItem::GetMin();
}

int CComboRulerItem::GetMax()
{
	if (m_bHitPrimary)
	{
		int nPDist = CRulerItem::GetMax() - GetHorzPosTwips();
		int nLDist = m_link.GetMax() - m_link.GetHorzPosTwips();
		int nMinDist = (nPDist < nLDist) ? nPDist : nLDist;
		return GetHorzPosTwips() + nMinDist;
	}
	else
		return CRulerItem::GetMax();
}

void CTabRulerItem::TrackHorzPosTwips(int nXPos, BOOL bOnRuler)
{
	if (bOnRuler)
		CRulerItem::TrackHorzPosTwips(nXPos, bOnRuler);
	else
		CRulerItem::TrackHorzPosTwips(0, bOnRuler);
}


BEGIN_MESSAGE_MAP(CRulerBar, CControlBar)
	//{{AFX_MSG_MAP(CRulerBar)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_SYSCOLORCHANGE()
	ON_WM_WINDOWPOSCHANGING()
	ON_WM_SHOWWINDOW()
	ON_WM_WINDOWPOSCHANGED()
	//}}AFX_MSG_MAP
	ON_MESSAGE(WM_SIZEPARENT, OnSizeParent)
	// Global help commands
END_MESSAGE_MAP()

CRulerBar::CRulerBar(BOOL b3DExt) :
	m_leftmargin(IDB_RULER_BLOCK, IDB_RULER_UP, m_indent),
	m_indent(IDB_RULER_DOWN),
	m_rightmargin(IDB_RULER_UP),
	m_tabItem(IDB_RULER_TAB)
{
	m_bDeferInProgress = FALSE;
	m_leftmargin.SetRuler(this);
	m_indent.SetRuler(this);
	m_rightmargin.SetRuler(this);

	// all of the tab stops share handles
	for (int i=0;i<MAX_TAB_STOPS;i++)
	{
		m_pTabItems[i].m_hbm = m_tabItem.m_hbm;
		m_pTabItems[i].m_hbmMask = m_tabItem.m_hbmMask;
		m_pTabItems[i].m_size = m_tabItem.m_size;
	}

	m_unit.m_nTPU = 0;
	m_nScroll = 0;

	LOGFONT lf;
	memcpy(&lf, &theApp.m_lf, sizeof(LOGFONT));
	lf.lfHeight = -8;
	lf.lfWidth = 0;
	VERIFY(fnt.CreateFontIndirect(&lf));

	m_nTabs = 0;
	m_leftmargin.SetVertPos(9);
	m_indent.SetVertPos(-1);
	m_rightmargin.SetVertPos(9);

	m_cxLeftBorder = 0;
	if (!theApp.m_bWin4)
		m_bDraw3DExt = FALSE;
	else
		m_bDraw3DExt = b3DExt;

	m_cyTopBorder = 4;
	m_cyBottomBorder = 6;

	m_pSelItem = NULL;

	m_logx = theApp.m_dcScreen.GetDeviceCaps(LOGPIXELSX);

	CreateGDIObjects();
}

CRulerBar::~CRulerBar()
{
	// set handles to NULL to avoid deleting twice
	for (int i=0;i<MAX_TAB_STOPS;i++)
	{
		m_pTabItems[i].m_hbm = NULL;
		m_pTabItems[i].m_hbmMask = NULL;
	}
}

void CRulerBar::CreateGDIObjects()
{
	penFocusLine.DeleteObject();
	penBtnHighLight.DeleteObject();
	penBtnShadow.DeleteObject();
	penWindowFrame.DeleteObject();
	penBtnText.DeleteObject();
	penBtnFace.DeleteObject();
	penWindowText.DeleteObject();
	penWindow.DeleteObject();
	brushWindow.DeleteObject();
	brushBtnFace.DeleteObject();

	penFocusLine.CreatePen(PS_DOT, 1,GetSysColor(COLOR_WINDOWTEXT));
	penBtnHighLight.CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNHIGHLIGHT));
	penBtnShadow.CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));
	penWindowFrame.CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOWFRAME));
	penBtnText.CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNTEXT));
	penBtnFace.CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNFACE));
	penWindowText.CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOWTEXT));
	penWindow.CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOW));
	brushWindow.CreateSolidBrush(GetSysColor(COLOR_WINDOW));
	brushBtnFace.CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
}

void CRulerBar::OnUpdateCmdUI(CFrameWnd* /*pTarget*/, BOOL /*bDisableIfNoHndler*/)
{
	ASSERT_VALID(this);
	//Get the page size and see if changed -- from document
	//get margins and tabs and see if changed -- from view
	if (m_pSelItem == NULL) // only update if not in middle of dragging
	{
		CWordPadView* pView = (CWordPadView*)GetView();
		ASSERT(pView != NULL);
		Update(pView->GetPaperSize(), pView->GetMargins());
		Update(pView->GetParaFormatSelection());
		CRect rect;
		pView->GetRichEditCtrl().GetRect(&rect);
		CPoint pt = rect.TopLeft();
		pView->ClientToScreen(&pt);
		ScreenToClient(&pt);
		if (m_cxLeftBorder != pt.x)
		{
			m_cxLeftBorder = pt.x;
			Invalidate();
		}
		int nScroll = pView->GetScrollPos(SB_HORZ);
		if (nScroll != m_nScroll)
		{
			m_nScroll = nScroll;
			Invalidate();
		}
	}
}

CSize CRulerBar::GetBaseUnits()
{
	ASSERT(fnt.GetSafeHandle() != NULL);
	CFont* pFont = theApp.m_dcScreen.SelectObject(&fnt);
	TEXTMETRIC tm;
	VERIFY(theApp.m_dcScreen.GetTextMetrics(&tm) == TRUE);
	theApp.m_dcScreen.SelectObject(pFont);
//  return CSize(tm.tmAveCharWidth, tm.tmHeight+tm.tmDescent);
	return CSize(tm.tmAveCharWidth, tm.tmHeight);
}

BOOL CRulerBar::Create(CWnd* pParentWnd, DWORD dwStyle, UINT nID)
{
	ASSERT_VALID(pParentWnd);   // must have a parent

	dwStyle |= WS_CLIPSIBLINGS;
	// force WS_CLIPSIBLINGS (avoids SetWindowPos bugs)
	m_dwStyle = (dwStyle & CBRS_ALL);

⌨️ 快捷键说明

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