📄 trckrect.cpp
字号:
// 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"
#ifdef AFX_CORE4_SEG
#pragma code_seg(AFX_CORE4_SEG)
#endif
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define new DEBUG_NEW
/////////////////////////////////////////////////////////////////////////////
// CRectTracker global state
// various GDI objects we need to draw
AFX_STATIC_DATA HCURSOR _afxCursors[10] = { 0, };
AFX_STATIC_DATA HBRUSH _afxHatchBrush = 0;
AFX_STATIC_DATA HPEN _afxBlackDottedPen = 0;
AFX_STATIC_DATA int _afxHandleSize = 0;
void AFX_CDECL AfxTrackerTerm()
{
AfxDeleteObject((HGDIOBJ*)&_afxHatchBrush);
AfxDeleteObject((HGDIOBJ*)&_afxBlackDottedPen);
}
char _afxTrackerTerm = (char)atexit(&AfxTrackerTerm);
// the struct below is used to determine the qualities of a particular handle
struct AFX_HANDLEINFO
{
size_t nOffsetX; // offset within RECT for X coordinate
size_t nOffsetY; // offset within RECT for Y coordinate
int nCenterX; // adjust X by Width()/2 * this number
int nCenterY; // adjust Y by Height()/2 * this number
int nHandleX; // adjust X by handle size * this number
int nHandleY; // adjust Y by handle size * this number
int nInvertX; // handle converts to this when X inverted
int nInvertY; // handle converts to this when Y inverted
};
// this array describes all 8 handles (clock-wise)
AFX_STATIC_DATA const AFX_HANDLEINFO _afxHandleInfo[] =
{
// corner handles (top-left, top-right, bottom-right, bottom-left
{ offsetof(RECT, left), offsetof(RECT, top), 0, 0, 0, 0, 1, 3 },
{ offsetof(RECT, right), offsetof(RECT, top), 0, 0, -1, 0, 0, 2 },
{ offsetof(RECT, right), offsetof(RECT, bottom), 0, 0, -1, -1, 3, 1 },
{ offsetof(RECT, left), offsetof(RECT, bottom), 0, 0, 0, -1, 2, 0 },
// side handles (top, right, bottom, left)
{ offsetof(RECT, left), offsetof(RECT, top), 1, 0, 0, 0, 4, 6 },
{ offsetof(RECT, right), offsetof(RECT, top), 0, 1, -1, 0, 7, 5 },
{ offsetof(RECT, left), offsetof(RECT, bottom), 1, 0, 0, -1, 6, 4 },
{ offsetof(RECT, left), offsetof(RECT, top), 0, 1, 0, 0, 5, 7 }
};
// the struct below gives us information on the layout of a RECT struct and
// the relationship between its members
struct AFX_RECTINFO
{
size_t nOffsetAcross; // offset of opposite point (ie. left->right)
int nSignAcross; // sign relative to that point (ie. add/subtract)
};
// this array is indexed by the offset of the RECT member / sizeof(int)
AFX_STATIC_DATA const AFX_RECTINFO _afxRectInfo[] =
{
{ offsetof(RECT, right), +1 },
{ offsetof(RECT, bottom), +1 },
{ offsetof(RECT, left), -1 },
{ offsetof(RECT, top), -1 },
};
/////////////////////////////////////////////////////////////////////////////
// CRectTracker intitialization
CRectTracker::CRectTracker(LPCRECT lpSrcRect, UINT nStyle)
{
ASSERT(AfxIsValidAddress(lpSrcRect, sizeof(RECT), FALSE));
Construct();
m_rect.CopyRect(lpSrcRect);
m_nStyle = nStyle;
}
void CRectTracker::Construct()
{
// do one-time initialization if necessary
AfxLockGlobals(CRIT_RECTTRACKER);
static BOOL bInitialized;
if (!bInitialized)
{
// sanity checks for assumptions we make in the code
ASSERT(sizeof(((RECT*)NULL)->left) == sizeof(int));
ASSERT(offsetof(RECT, top) > offsetof(RECT, left));
ASSERT(offsetof(RECT, right) > offsetof(RECT, top));
ASSERT(offsetof(RECT, bottom) > offsetof(RECT, right));
if (_afxHatchBrush == NULL)
{
// create the hatch pattern + bitmap
WORD hatchPattern[8];
WORD wPattern = 0x1111;
for (int i = 0; i < 4; i++)
{
hatchPattern[i] = wPattern;
hatchPattern[i+4] = wPattern;
wPattern <<= 1;
}
HBITMAP hatchBitmap = CreateBitmap(8, 8, 1, 1, &hatchPattern);
if (hatchBitmap == NULL)
{
AfxUnlockGlobals(CRIT_RECTTRACKER);
AfxThrowResourceException();
}
// create black hatched brush
_afxHatchBrush = CreatePatternBrush(hatchBitmap);
DeleteObject(hatchBitmap);
if (_afxHatchBrush == NULL)
{
AfxUnlockGlobals(CRIT_RECTTRACKER);
AfxThrowResourceException();
}
}
if (_afxBlackDottedPen == NULL)
{
// create black dotted pen
_afxBlackDottedPen = CreatePen(PS_DOT, 0, RGB(0, 0, 0));
if (_afxBlackDottedPen == NULL)
{
AfxUnlockGlobals(CRIT_RECTTRACKER);
AfxThrowResourceException();
}
}
// Note: all track cursors must live in same module
HINSTANCE hInst = AfxFindResourceHandle(
MAKEINTRESOURCE(AFX_IDC_TRACK4WAY), RT_GROUP_CURSOR);
// initialize the cursor array
_afxCursors[0] = ::LoadCursor(hInst, MAKEINTRESOURCE(AFX_IDC_TRACKNWSE));
_afxCursors[1] = ::LoadCursor(hInst, MAKEINTRESOURCE(AFX_IDC_TRACKNESW));
_afxCursors[2] = _afxCursors[0];
_afxCursors[3] = _afxCursors[1];
_afxCursors[4] = ::LoadCursor(hInst, MAKEINTRESOURCE(AFX_IDC_TRACKNS));
_afxCursors[5] = ::LoadCursor(hInst, MAKEINTRESOURCE(AFX_IDC_TRACKWE));
_afxCursors[6] = _afxCursors[4];
_afxCursors[7] = _afxCursors[5];
_afxCursors[8] = ::LoadCursor(hInst, MAKEINTRESOURCE(AFX_IDC_TRACK4WAY));
_afxCursors[9] = ::LoadCursor(hInst, MAKEINTRESOURCE(AFX_IDC_MOVE4WAY));
// get default handle size from Windows profile setting
static const TCHAR szWindows[] = _T("windows");
static const TCHAR szInplaceBorderWidth[] =
_T("oleinplaceborderwidth");
_afxHandleSize = GetProfileInt(szWindows, szInplaceBorderWidth, 4);
bInitialized = TRUE;
}
AfxUnlockGlobals(CRIT_RECTTRACKER);
m_nStyle = 0;
m_nHandleSize = _afxHandleSize;
m_sizeMin.cy = m_sizeMin.cx = m_nHandleSize*2;
m_rectLast.SetRectEmpty();
m_sizeLast.cx = m_sizeLast.cy = 0;
m_bErase = FALSE;
m_bFinalErase = FALSE;
}
CRectTracker::~CRectTracker()
{
}
/////////////////////////////////////////////////////////////////////////////
// CRectTracker operations
void CRectTracker::Draw(CDC* pDC) const
{
// set initial DC state
VERIFY(pDC->SaveDC() != 0);
pDC->SetMapMode(MM_TEXT);
pDC->SetViewportOrg(0, 0);
pDC->SetWindowOrg(0, 0);
// get normalized rectangle
CRect rect = m_rect;
rect.NormalizeRect();
CPen* pOldPen = NULL;
CBrush* pOldBrush = NULL;
CGdiObject* pTemp;
int nOldROP;
// draw lines
if ((m_nStyle & (dottedLine|solidLine)) != 0)
{
if (m_nStyle & dottedLine)
pOldPen = pDC->SelectObject(CPen::FromHandle(_afxBlackDottedPen));
else
pOldPen = (CPen*)pDC->SelectStockObject(BLACK_PEN);
pOldBrush = (CBrush*)pDC->SelectStockObject(NULL_BRUSH);
nOldROP = pDC->SetROP2(R2_COPYPEN);
rect.InflateRect(+1, +1); // borders are one pixel outside
pDC->Rectangle(rect.left, rect.top, rect.right, rect.bottom);
pDC->SetROP2(nOldROP);
}
// if hatchBrush is going to be used, need to unrealize it
if ((m_nStyle & (hatchInside|hatchedBorder)) != 0)
UnrealizeObject(_afxHatchBrush);
// hatch inside
if ((m_nStyle & hatchInside) != 0)
{
pTemp = pDC->SelectStockObject(NULL_PEN);
if (pOldPen == NULL)
pOldPen = (CPen*)pTemp;
pTemp = pDC->SelectObject(CBrush::FromHandle(_afxHatchBrush));
if (pOldBrush == NULL)
pOldBrush = (CBrush*)pTemp;
pDC->SetBkMode(TRANSPARENT);
nOldROP = pDC->SetROP2(R2_MASKNOTPEN);
pDC->Rectangle(rect.left+1, rect.top+1, rect.right, rect.bottom);
pDC->SetROP2(nOldROP);
}
// draw hatched border
if ((m_nStyle & hatchedBorder) != 0)
{
pTemp = pDC->SelectObject(CBrush::FromHandle(_afxHatchBrush));
if (pOldBrush == NULL)
pOldBrush = (CBrush*)pTemp;
pDC->SetBkMode(OPAQUE);
CRect rectTrue;
GetTrueRect(&rectTrue);
pDC->PatBlt(rectTrue.left, rectTrue.top, rectTrue.Width(),
rect.top-rectTrue.top, 0x000F0001 /* Pn */);
pDC->PatBlt(rectTrue.left, rect.bottom,
rectTrue.Width(), rectTrue.bottom-rect.bottom, 0x000F0001 /* Pn */);
pDC->PatBlt(rectTrue.left, rect.top, rect.left-rectTrue.left,
rect.Height(), 0x000F0001 /* Pn */);
pDC->PatBlt(rect.right, rect.top, rectTrue.right-rect.right,
rect.Height(), 0x000F0001 /* Pn */);
}
// draw resize handles
if ((m_nStyle & (resizeInside|resizeOutside)) != 0)
{
UINT mask = GetHandleMask();
for (int i = 0; i < 8; ++i)
{
if (mask & (1<<i))
{
GetHandleRect((TrackerHit)i, &rect);
pDC->FillSolidRect(rect, RGB(0, 0, 0));
}
}
}
// cleanup pDC state
if (pOldPen != NULL)
pDC->SelectObject(pOldPen);
if (pOldBrush != NULL)
pDC->SelectObject(pOldBrush);
VERIFY(pDC->RestoreDC(-1));
}
BOOL CRectTracker::SetCursor(CWnd* pWnd, UINT nHitTest) const
{
// trackers should only be in client area
if (nHitTest != HTCLIENT)
return FALSE;
// convert cursor position to client co-ordinates
CPoint point;
GetCursorPos(&point);
pWnd->ScreenToClient(&point);
// do hittest and normalize hit
int nHandle = HitTestHandles(point);
if (nHandle < 0)
return FALSE;
// need to normalize the hittest such that we get proper cursors
nHandle = NormalizeHit(nHandle);
// handle special case of hitting area between handles
// (logically the same -- handled as a move -- but different cursor)
if (nHandle == hitMiddle && !m_rect.PtInRect(point))
{
// only for trackers with hatchedBorder (ie. in-place resizing)
if (m_nStyle & hatchedBorder)
nHandle = (TrackerHit)9;
}
ASSERT(nHandle < _countof(_afxCursors));
::SetCursor(_afxCursors[nHandle]);
return TRUE;
}
int CRectTracker::HitTest(CPoint point) const
{
TrackerHit hitResult = hitNothing;
CRect rectTrue;
GetTrueRect(&rectTrue);
ASSERT(rectTrue.left <= rectTrue.right);
ASSERT(rectTrue.top <= rectTrue.bottom);
if (rectTrue.PtInRect(point))
{
if ((m_nStyle & (resizeInside|resizeOutside)) != 0)
hitResult = (TrackerHit)HitTestHandles(point);
else
hitResult = hitMiddle;
}
return hitResult;
}
int CRectTracker::NormalizeHit(int nHandle) const
{
ASSERT(nHandle <= 8 && nHandle >= -1);
if (nHandle == hitMiddle || nHandle == hitNothing)
return nHandle;
const AFX_HANDLEINFO* pHandleInfo = &_afxHandleInfo[nHandle];
if (m_rect.Width() < 0)
{
nHandle = (TrackerHit)pHandleInfo->nInvertX;
pHandleInfo = &_afxHandleInfo[nHandle];
}
if (m_rect.Height() < 0)
nHandle = (TrackerHit)pHandleInfo->nInvertY;
return nHandle;
}
BOOL CRectTracker::Track(CWnd* pWnd, CPoint point, BOOL bAllowInvert,
CWnd* pWndClipTo)
{
// perform hit testing on the handles
int nHandle = HitTestHandles(point);
if (nHandle < 0)
{
// didn't hit a handle, so just return FALSE
return FALSE;
}
// otherwise, call helper function to do the tracking
m_bAllowInvert = bAllowInvert;
return TrackHandle(nHandle, pWnd, point, pWndClipTo);
}
BOOL CRectTracker::TrackRubberBand(CWnd* pWnd, CPoint point, BOOL bAllowInvert)
{
// simply call helper function to track from bottom right handle
m_bAllowInvert = bAllowInvert;
m_rect.SetRect(point.x, point.y, point.x, point.y);
return TrackHandle(hitBottomRight, pWnd, point, NULL);
}
void CRectTracker::DrawTrackerRect(
LPCRECT lpRect, CWnd* pWndClipTo, CDC* pDC, CWnd* pWnd)
{
// first, normalize the rectangle for drawing
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -