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

📄 ruler.cpp

📁 VC++写的仿Windows系统的写字板程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	// create the HWND
	CRect rect;
	rect.SetRectEmpty();
	LPCTSTR lpszClass = AfxRegisterWndClass(0, ::LoadCursor(NULL, IDC_ARROW),
		(HBRUSH)(COLOR_BTNFACE+1), NULL);

	if (!CWnd::Create(lpszClass, NULL, dwStyle, rect, pParentWnd, nID))
		return FALSE;
	// NOTE: Parent must resize itself for control bar to be resized

	int i;
	int nMax = 100;
	for (i=0;i<MAX_TAB_STOPS;i++)
	{
		m_pTabItems[i].SetRuler(this);
		m_pTabItems[i].SetVertPos(8);
		m_pTabItems[i].SetHorzPosTwips(0);
		m_pTabItems[i].SetBounds(0, nMax);
	}
	return TRUE;
}

CSize CRulerBar::CalcFixedLayout(BOOL bStretch, BOOL bHorz)
{
	ASSERT(bHorz);
	CSize m_size = CControlBar::CalcFixedLayout(bStretch, bHorz);
	CRect rectSize;
	rectSize.SetRectEmpty();
	CalcInsideRect(rectSize, bHorz);       // will be negative size
	m_size.cy = RULERBARHEIGHT - rectSize.Height();
	return m_size;
}

void CRulerBar::Update(const PARAFORMAT& pf)
{
	ASSERT(pf.cTabCount <= MAX_TAB_STOPS);

	m_leftmargin.SetHorzPosTwips((int)(pf.dxStartIndent + pf.dxOffset));
	m_indent.SetHorzPosTwips((int)pf.dxStartIndent);
	m_rightmargin.SetHorzPosTwips(PrintWidth() - (int) pf.dxRightIndent);

	int i = 0;
	for (i=0;i<pf.cTabCount;i++)
		m_pTabItems[i].SetHorzPosTwips((int)pf.rgxTabs[i]);
	for ( ;i<MAX_TAB_STOPS; i++)
		m_pTabItems[i].SetHorzPosTwips(0);
}

void CRulerBar::Update(CSize sizePaper, const CRect& rectMargins)
{
	if ((sizePaper != m_sizePaper) || (rectMargins != m_rectMargin))
	{
		m_sizePaper = sizePaper;
		m_rectMargin = rectMargins;
		Invalidate();
	}
	if (m_unit.m_nTPU != theApp.GetTPU())
	{
		m_unit = theApp.GetUnit();
		Invalidate();
	}
}

void CRulerBar::FillInParaFormat(PARAFORMAT& pf)
{
	pf.dwMask = PFM_STARTINDENT | PFM_RIGHTINDENT | PFM_OFFSET | PFM_TABSTOPS;
	pf.dxStartIndent = m_indent.GetHorzPosTwips();
	pf.dxOffset = m_leftmargin.GetHorzPosTwips() - pf.dxStartIndent;
	pf.dxRightIndent = PrintWidth() - m_rightmargin.GetHorzPosTwips();
	pf.cTabCount = 0L;
	SortTabs();
	int i, nPos = 0;
	for (i=0;i<MAX_TAB_STOPS;i++)
	{
		// get rid of zeroes and multiples
		// i.e. if we have 0,0,0,1,2,3,4,4,5
		// we will get tabs at 1,2,3,4,5
		if (nPos != m_pTabItems[i].GetHorzPosTwips())
		{
			nPos = m_pTabItems[i].GetHorzPosTwips();
			pf.rgxTabs[pf.cTabCount++] = nPos;
		}
	}
}

// simple bubble sort is adequate for small number of tabs
void CRulerBar::SortTabs()
{
	int i,j, nPos;
	for (i=0;i<MAX_TAB_STOPS - 1;i++)
	{
		for (j=i+1; j < MAX_TAB_STOPS;j++)
		{
			if (m_pTabItems[j].GetHorzPosTwips() < m_pTabItems[i].GetHorzPosTwips())
			{
				nPos = m_pTabItems[j].GetHorzPosTwips();
				m_pTabItems[j].SetHorzPosTwips(m_pTabItems[i].GetHorzPosTwips());
				m_pTabItems[i].SetHorzPosTwips(nPos);
			}
		}
	}
}

void CRulerBar::DoPaint(CDC* pDC)
{
	CControlBar::DoPaint(pDC); // CControlBar::DoPaint -- draws border
	if (m_unit.m_nTPU != 0)
	{
		pDC->SaveDC();
		// offset coordinate system
		CPoint pointOffset(0,0);
		RulerToClient(pointOffset);
		pDC->SetViewportOrg(pointOffset);

		DrawFace(*pDC);
		DrawTickMarks(*pDC);

		DrawTabs(*pDC);
		m_leftmargin.Draw(*pDC);
		m_indent.Draw(*pDC);
		m_rightmargin.Draw(*pDC);

		pDC->RestoreDC(-1);
	}
	// Do not call CControlBar::OnPaint() for painting messages
}

void CRulerBar::DrawTabs(CDC& dc)
{
	int i;
	int nPos = 0;
	for (i=0;i<MAX_TAB_STOPS;i++)
	{
		if (m_pTabItems[i].GetHorzPosTwips() > nPos)
			nPos = (m_pTabItems[i].GetHorzPosTwips());
		m_pTabItems[i].Draw(dc);
	}
	int nPageWidth = PrintWidth();
	nPos = nPos - nPos%720 + 720;
	dc.SelectObject(&penBtnShadow);
	for ( ; nPos < nPageWidth; nPos += 720)
	{
		int nx = XTwipsToRuler(nPos);
		dc.MoveTo(nx, HEIGHT - 1);
		dc.LineTo(nx, HEIGHT + 1);
	}
}

void CRulerBar::DrawFace(CDC& dc)
{
	int nPageWidth = XTwipsToRuler(PrintWidth());
	int nPageEdge = XTwipsToRuler(PrintWidth() + m_rectMargin.right);

	dc.SaveDC();

	dc.SelectObject(&penBtnShadow);
	dc.MoveTo(0,0);
	dc.LineTo(nPageEdge - 1, 0);
	dc.LineTo(nPageEdge - 1, HEIGHT - 2);
	dc.LineTo(nPageWidth - 1, HEIGHT - 2);
	dc.LineTo(nPageWidth - 1, 1);
	dc.LineTo(nPageWidth, 1);
	dc.LineTo(nPageWidth, HEIGHT -2);

	dc.SelectObject(&penBtnHighLight);
	dc.MoveTo(nPageWidth, HEIGHT - 1);
	dc.LineTo(nPageEdge, HEIGHT -1);
	dc.MoveTo(nPageWidth + 1, HEIGHT - 3);
	dc.LineTo(nPageWidth + 1, 1);
	dc.LineTo(nPageEdge - 1, 1);

	dc.SelectObject(&penWindow);
	dc.MoveTo(0, HEIGHT - 1);
	dc.LineTo(nPageWidth, HEIGHT -1);

	dc.SelectObject(&penBtnFace);
	dc.MoveTo(1, HEIGHT - 2);
	dc.LineTo(nPageWidth - 1, HEIGHT - 2);

	dc.SelectObject(&penWindowFrame);
	dc.MoveTo(0, HEIGHT - 2);
	dc.LineTo(0, 1);
	dc.LineTo(nPageWidth - 1, 1);

	dc.FillRect(CRect(1, 2, nPageWidth - 1, HEIGHT-2), &brushWindow);
	dc.FillRect(CRect(nPageWidth + 2, 2, nPageEdge - 1, HEIGHT-2), &brushBtnFace);

	CRect rcClient;
	GetClientRect(&rcClient);
	ClientToRuler(rcClient);
	rcClient.top = HEIGHT;
	rcClient.bottom = HEIGHT + 2;
	dc.FillRect(rcClient, &brushBtnFace);

	CRect rectFill(rcClient.left, HEIGHT+4, rcClient.right, HEIGHT+9);
	dc.FillRect(rectFill, &brushWindow);

	if (m_bDraw3DExt)  // draws the 3D extension into the view
	{
		dc.SelectObject(&penBtnShadow);
		dc.MoveTo(rcClient.left, HEIGHT+8);
		dc.LineTo(rcClient.left, HEIGHT+2);
		dc.LineTo(rcClient.right-1, HEIGHT+2);

		dc.SelectObject(&penWindowFrame);
		dc.MoveTo(rcClient.left+1, HEIGHT+8);
		dc.LineTo(rcClient.left+1, HEIGHT+3);
		dc.LineTo(rcClient.right-2, HEIGHT+3);

		dc.SelectObject(&penBtnHighLight);
		dc.MoveTo(rcClient.right-1, HEIGHT+2);
		dc.LineTo(rcClient.right-1, HEIGHT+8);

		dc.SelectObject(&penBtnFace);
		dc.MoveTo(rcClient.right-2, HEIGHT+3);
		dc.LineTo(rcClient.right-2, HEIGHT+8);
	}
	else
	{
		dc.SelectObject(&penBtnShadow);
		dc.MoveTo(rcClient.left, HEIGHT+2);
		dc.LineTo(rcClient.right, HEIGHT+2);

		dc.SelectObject(&penWindowFrame);
		dc.MoveTo(rcClient.left, HEIGHT+3);
		dc.LineTo(rcClient.right, HEIGHT+3);
	}

	dc.RestoreDC(-1);
}

void CRulerBar::DrawTickMarks(CDC& dc)
{
	dc.SaveDC();

	dc.SelectObject(&penWindowText);
	dc.SelectObject(&fnt);
	dc.SetTextColor(GetSysColor(COLOR_WINDOWTEXT));
	dc.SetBkMode(TRANSPARENT);

	DrawDiv(dc, m_unit.m_nSmallDiv, m_unit.m_nLargeDiv, 2);
	DrawDiv(dc, m_unit.m_nMediumDiv, m_unit.m_nLargeDiv, 5);
	DrawNumbers(dc, m_unit.m_nLargeDiv, m_unit.m_nTPU);

	dc.RestoreDC(-1);
}

void CRulerBar::DrawNumbers(CDC& dc, int nInc, int nTPU)
{
	int nPageWidth = PrintWidth();
	int nPageEdge = nPageWidth + m_rectMargin.right;
	TCHAR buf[10];

	int nTwips, nPixel, nLen;

	for (nTwips = nInc; nTwips < nPageEdge; nTwips += nInc)
	{
		if (nTwips == nPageWidth)
			continue;
		nPixel = XTwipsToRuler(nTwips);
		wsprintf(buf, _T("%d"), nTwips/nTPU);
		nLen = lstrlen(buf);
		CSize sz = dc.GetTextExtent(buf, nLen);
		dc.ExtTextOut(nPixel - sz.cx/2, HEIGHT/2 - sz.cy/2, 0, NULL, buf, nLen, NULL);
	}
}

void CRulerBar::DrawDiv(CDC& dc, int nInc, int nLargeDiv, int nLength)
{
	int nPageWidth = PrintWidth();
	int nPageEdge = nPageWidth + m_rectMargin.right;

	int nTwips, nPixel;

	for (nTwips = nInc; nTwips < nPageEdge; nTwips += nInc)
	{
		if (nTwips == nPageWidth || nTwips%nLargeDiv == 0)
			continue;
		nPixel = XTwipsToRuler(nTwips);
		dc.MoveTo(nPixel, HEIGHT/2 - nLength/2);
		dc.LineTo(nPixel, HEIGHT/2 - nLength/2 + nLength);
	}
}

void CRulerBar::OnLButtonDown(UINT nFlags, CPoint point)
{
	CPoint pt = point;
	ClientToRuler(pt);

	m_pSelItem = NULL;
	if (m_leftmargin.HitTestPix(pt))
		m_pSelItem = &m_leftmargin;
	else if (m_indent.HitTestPix(pt))
		m_pSelItem = &m_indent;
	else if (m_rightmargin.HitTestPix(pt))
		m_pSelItem = &m_rightmargin;
	else
		m_pSelItem = GetHitTabPix(pt);
	if (m_pSelItem == NULL)
		m_pSelItem = GetFreeTab();
	if (m_pSelItem == NULL)
		return;
	SetCapture();

	m_pSelItem->SetTrack(TRUE);
	SetMarginBounds();
	OnMouseMove(nFlags, point);
}

void CRulerBar::SetMarginBounds()
{
	m_leftmargin.SetBounds(0, m_rightmargin.GetHorzPosTwips());
	m_indent.SetBounds(0, m_rightmargin.GetHorzPosTwips());

	int nMin = (m_leftmargin.GetHorzPosTwips() > m_indent.GetHorzPosTwips()) ?
		m_leftmargin.GetHorzPosTwips() : m_indent.GetHorzPosTwips();
	int nMax = PrintWidth() + m_rectMargin.right;
	m_rightmargin.SetBounds(nMin, nMax);

	// tabs can go from zero to the right page edge
	for (int i=0;i<MAX_TAB_STOPS;i++)
		m_pTabItems[i].SetBounds(0, nMax);
}

CRulerItem* CRulerBar::GetFreeTab()
{
	int i;
	for (i=0;i<MAX_TAB_STOPS;i++)
	{
		if (m_pTabItems[i].GetHorzPosTwips() == 0)
			return &m_pTabItems[i];
	}
	return NULL;
}

CTabRulerItem* CRulerBar::GetHitTabPix(CPoint point)
{
	int i;
	for (i=0;i<MAX_TAB_STOPS;i++)
	{
		if (m_pTabItems[i].HitTestPix(point))
			return &m_pTabItems[i];
	}
	return NULL;
}

void CRulerBar::OnLButtonUp(UINT nFlags, CPoint point)
{
	if (::GetCapture() != m_hWnd)
		return;
	OnMouseMove(nFlags, point);
	m_pSelItem->SetTrack(FALSE);
	ReleaseCapture();
	CWordPadView* pView = (CWordPadView*)GetView();
	ASSERT(pView != NULL);
#if _MSC_VER >= 1400
	PARAFORMAT2& pf =
#else
	PARAFORMAT& pf =
#endif
	pView->GetParaFormatSelection();
	FillInParaFormat(pf);
	pView->SetParaFormat(pf);
	m_pSelItem = NULL;
}

void CRulerBar::OnMouseMove(UINT nFlags, CPoint point)
{
	CControlBar::OnMouseMove(nFlags, point);
// use ::GetCapture to avoid creating temporaries
	if (::GetCapture() != m_hWnd)
		return;
	ASSERT(m_pSelItem != NULL);
	CRect rc(0,0, XTwipsToRuler(PrintWidth() + m_rectMargin.right), HEIGHT);
	RulerToClient(rc);
	BOOL bOnRuler = rc.PtInRect(point);

// snap to minimum movement
	point.x = XClientToTwips(point.x);
	point.x += m_unit.m_nMinMove/2;
	point.x -= point.x%m_unit.m_nMinMove;

	m_pSelItem->TrackHorzPosTwips(point.x, bOnRuler);
	UpdateWindow();
}

void CRulerBar::OnSysColorChange()
{
	CControlBar::OnSysColorChange();
	CreateGDIObjects();
	Invalidate();
}

void CRulerBar::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{
	CControlBar::OnWindowPosChanging(lpwndpos);
	CRect rect;
	GetClientRect(rect);
	int minx = min(rect.Width(), lpwndpos->cx);
	int maxx = max(rect.Width(), lpwndpos->cx);
	rect.SetRect(minx-2, rect.bottom - 6, minx, rect.bottom);
	InvalidateRect(rect);
	rect.SetRect(maxx-2, rect.bottom - 6, maxx, rect.bottom);
	InvalidateRect(rect);
}

void CRulerBar::OnShowWindow(BOOL bShow, UINT nStatus)
{
	CControlBar::OnShowWindow(bShow, nStatus);
	m_bDeferInProgress = FALSE;
}

void CRulerBar::OnWindowPosChanged(WINDOWPOS FAR* lpwndpos)
{
	CControlBar::OnWindowPosChanged(lpwndpos);
	m_bDeferInProgress = FALSE;
}

LRESULT CRulerBar::OnSizeParent(WPARAM wParam, LPARAM lParam)
{
	BOOL bVis = GetStyle() & WS_VISIBLE;
	if ((bVis && (m_nStateFlags & delayHide)) ||
		(!bVis && (m_nStateFlags & delayShow)))
	{
		m_bDeferInProgress = TRUE;
	}
	return CControlBar::OnSizeParent(wParam, lParam);
}

⌨️ 快捷键说明

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