[MSPAINT] Simplify RECT/CRect handling

Refactoring. CORE-19094
This commit is contained in:
Katayama Hirofumi MZ 2023-11-23 15:01:29 +09:00
parent f935132fbe
commit 1aeebfdacb
10 changed files with 74 additions and 78 deletions

View file

@ -18,7 +18,7 @@ CCanvasWindow::CCanvasWindow()
, m_ptOrig { -1, -1 }
{
m_ahbmCached[0] = m_ahbmCached[1] = NULL;
::SetRectEmpty(&m_rcResizing);
m_rcResizing.SetRectEmpty();
}
CCanvasWindow::~CCanvasWindow()
@ -34,7 +34,7 @@ RECT CCanvasWindow::GetBaseRect()
CRect rcBase;
GetImageRect(rcBase);
ImageToCanvas(rcBase);
::InflateRect(&rcBase, GRIP_SIZE, GRIP_SIZE);
rcBase.InflateRect(GRIP_SIZE, GRIP_SIZE);
return rcBase;
}
@ -87,8 +87,8 @@ VOID CCanvasWindow::getNewZoomRect(CRect& rcView, INT newZoom, CPoint ptTarget)
INT oldZoom = toolsModel.GetZoom();
GetClientRect(rcView);
LONG cxView = rcView.right * oldZoom / newZoom, cyView = rcView.bottom * oldZoom / newZoom;
::SetRect(&rcView, ptTarget.x - cxView / 2, ptTarget.y - cyView / 2,
ptTarget.x + cxView / 2, ptTarget.y + cyView / 2);
rcView.SetRect(ptTarget.x - cxView / 2, ptTarget.y - cyView / 2,
ptTarget.x + cxView / 2, ptTarget.y + cyView / 2);
// Shift the rectangle if necessary
INT dx = 0, dy = 0;
@ -199,7 +199,7 @@ VOID CCanvasWindow::DoDraw(HDC hDC, RECT& rcClient, RECT& rcPaint)
toolsModel.OnDrawOverlayOnCanvas(hdcMem0);
// Draw new frame on hdcMem0 if any
if (m_hitCanvasSizeBox != HIT_NONE && !::IsRectEmpty(&m_rcResizing))
if (m_hitCanvasSizeBox != HIT_NONE && !m_rcResizing.IsRectEmpty())
DrawXorRect(hdcMem0, &m_rcResizing);
// Transfer the bits (hDC <-- hdcMem0)
@ -434,11 +434,11 @@ LRESULT CCanvasWindow::OnMouseMove(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL
if (!m_drawing)
{
RECT rcImage;
CRect rcImage;
GetImageRect(rcImage);
CStringW strCoord;
if (::PtInRect(&rcImage, pt))
if (rcImage.PtInRect(pt))
strCoord.Format(L"%ld, %ld", pt.x, pt.y);
::SendMessageW(g_hStatusBar, SB_SETTEXT, 1, (LPARAM)(LPCWSTR)strCoord);
}
@ -507,19 +507,19 @@ LRESULT CCanvasWindow::OnMouseMove(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL
switch (m_hitCanvasSizeBox)
{
case HIT_UPPER_LEFT:
::OffsetRect(&rcResizing, cxDelta, cyDelta);
rcResizing.OffsetRect(cxDelta, cyDelta);
break;
case HIT_UPPER_CENTER:
::OffsetRect(&rcResizing, 0, cyDelta);
rcResizing.OffsetRect(0, cyDelta);
break;
case HIT_UPPER_RIGHT:
::OffsetRect(&rcResizing, 0, cyDelta);
rcResizing.OffsetRect(0, cyDelta);
break;
case HIT_MIDDLE_LEFT:
::OffsetRect(&rcResizing, cxDelta, 0);
rcResizing.OffsetRect(cxDelta, 0);
break;
case HIT_LOWER_LEFT:
::OffsetRect(&rcResizing, cxDelta, 0);
rcResizing.OffsetRect(cxDelta, 0);
break;
default:
break;
@ -591,7 +591,7 @@ LRESULT CCanvasWindow::OnButtonUp(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL&
default:
break;
}
::SetRectEmpty(&m_rcResizing);
m_rcResizing.SetRectEmpty();
g_imageSaved = FALSE;
@ -623,7 +623,7 @@ LRESULT CCanvasWindow::OnSetCursor(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL
CRect rcClient;
GetClientRect(&rcClient);
if (!::PtInRect(&rcClient, pt))
if (!rcClient.PtInRect(pt))
{
bHandled = FALSE;
return 0;
@ -641,7 +641,7 @@ LRESULT CCanvasWindow::OnSetCursor(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL
GetImageRect(rcImage);
ImageToCanvas(rcImage);
if (::PtInRect(&rcImage, pt))
if (rcImage.PtInRect(pt))
{
switch (toolsModel.GetActiveTool())
{
@ -680,7 +680,7 @@ LRESULT CCanvasWindow::OnKeyDown(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL&
::ReleaseCapture();
m_nMouseDownMsg = 0;
m_hitCanvasSizeBox = HIT_NONE;
::SetRectEmpty(&m_rcResizing);
m_rcResizing.SetRectEmpty();
Invalidate(TRUE);
}
@ -691,7 +691,7 @@ LRESULT CCanvasWindow::OnCancelMode(UINT nMsg, WPARAM wParam, LPARAM lParam, BOO
{
// Cancel dragging
m_hitCanvasSizeBox = HIT_NONE;
::SetRectEmpty(&m_rcResizing);
m_rcResizing.SetRectEmpty();
Invalidate(TRUE);
return 0;
}

View file

@ -257,8 +257,7 @@ Text(HDC hdc, LONG x1, LONG y1, LONG x2, LONG y2, COLORREF fg, COLORREF bg, LPCW
{
INT iSaveDC = ::SaveDC(hdc); // We will modify the clipping region. Save now.
RECT rc;
::SetRect(&rc, x1, y1, x2, y2);
CRect rc = { x1, y1, x2, y2 };
if (style == 0) // Transparent
{

View file

@ -354,7 +354,7 @@ void ImageModel::UnlockBitmap(HBITMAP hbmLocked)
void ImageModel::SelectionClone(BOOL bUndoable)
{
if (!selectionModel.m_bShow || ::IsRectEmpty(&selectionModel.m_rc))
if (!selectionModel.m_bShow || selectionModel.m_rc.IsRectEmpty())
return;
if (bUndoable)

View file

@ -229,8 +229,8 @@ struct RectSelTool : ToolBase
if (canvasWindow.m_drawing)
{
RECT rc = selectionModel.m_rc;
if (!::IsRectEmpty(&rc))
CRect& rc = selectionModel.m_rc;
if (!rc.IsRectEmpty())
RectSel(hdc, rc.left, rc.top, rc.right, rc.bottom);
}
}
@ -727,8 +727,8 @@ struct TextTool : ToolBase
{
if (canvasWindow.m_drawing)
{
RECT rc = selectionModel.m_rc;
if (!::IsRectEmpty(&rc))
CRect& rc = selectionModel.m_rc;
if (!rc.IsRectEmpty())
RectSel(hdc, rc.left, rc.top, rc.right, rc.bottom);
}
}
@ -760,10 +760,10 @@ struct TextTool : ToolBase
CStringW szText;
textEditWindow.GetWindowText(szText);
RECT rc;
CRect rc;
textEditWindow.InvalidateEditRect();
textEditWindow.GetEditRect(&rc);
::InflateRect(&rc, -GRIP_SIZE / 2, -GRIP_SIZE / 2);
rc.InflateRect(-GRIP_SIZE / 2, -GRIP_SIZE / 2);
// Draw the text
INT style = (toolsModel.IsBackgroundTransparent() ? 0 : 1);
@ -792,7 +792,7 @@ struct TextTool : ToolBase
imageModel.PushImageForUndo();
draw(m_hdc);
}
if (::IsRectEmpty(&selectionModel.m_rc))
if (selectionModel.m_rc.IsRectEmpty())
{
quit();
return TRUE;
@ -807,13 +807,13 @@ struct TextTool : ToolBase
fontsDialog.ShowWindow(SW_SHOWNOACTIVATE);
}
RECT rc = selectionModel.m_rc;
CRect rc = selectionModel.m_rc;
// Enlarge if tool small
INT cxMin = CX_MINTEXTEDIT, cyMin = CY_MINTEXTEDIT;
if (selectionModel.m_rc.IsRectEmpty())
{
SetRect(&rc, x, y, x + cxMin, y + cyMin);
rc.SetRect(x, y, x + cxMin, y + cyMin);
}
else
{

View file

@ -93,7 +93,7 @@ LRESULT CPaletteWindow::OnPaint(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& b
::FillRect(hMemDC, &rcClient, (HBRUSH)(COLOR_3DFACE + 1));
/* Draw the big box that contains the black box and the white box */
::SetRect(&rc, X_MARGIN, Y_MARGIN, X_MARGIN + CXY_BIGBOX, Y_MARGIN + CXY_BIGBOX);
rc = { X_MARGIN, Y_MARGIN, X_MARGIN + CXY_BIGBOX, Y_MARGIN + CXY_BIGBOX };
::DrawEdge(hMemDC, &rc, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
COLORREF rgbLight = ::GetSysColor(COLOR_3DHIGHLIGHT);
for (INT y = rc.top; y < rc.bottom; ++y)

View file

@ -21,8 +21,8 @@ SelectionModel::SelectionModel()
, m_bShow(FALSE)
, m_bContentChanged(FALSE)
{
::SetRectEmpty(&m_rc);
::SetRectEmpty(&m_rcOld);
m_rc.SetRectEmpty();
m_rcOld.SetRectEmpty();
m_ptHit = { -1, -1 };
}
@ -97,7 +97,7 @@ void SelectionModel::BuildMaskFromPtStack()
void SelectionModel::DrawBackgroundPoly(HDC hDCImage, COLORREF crBg)
{
if (::IsRectEmpty(&m_rcOld))
if (m_rcOld.IsRectEmpty())
return;
HGDIOBJ hPenOld = ::SelectObject(hDCImage, ::GetStockObject(NULL_PEN));
@ -109,7 +109,7 @@ void SelectionModel::DrawBackgroundPoly(HDC hDCImage, COLORREF crBg)
void SelectionModel::DrawBackgroundRect(HDC hDCImage, COLORREF crBg)
{
if (::IsRectEmpty(&m_rcOld))
if (m_rcOld.IsRectEmpty())
return;
Rect(hDCImage, m_rcOld.left, m_rcOld.top, m_rcOld.right, m_rcOld.bottom, crBg, crBg, 0, 1);
@ -126,7 +126,7 @@ void SelectionModel::DrawBackground(HDC hDCImage)
void SelectionModel::DrawSelection(HDC hDCImage, COLORREF crBg, BOOL bBgTransparent)
{
CRect rc = m_rc;
if (::IsRectEmpty(&rc))
if (rc.IsRectEmpty())
return;
BITMAP bm;
@ -161,7 +161,7 @@ BOOL SelectionModel::IsLanded() const
BOOL SelectionModel::TakeOff()
{
if (!IsLanded() || ::IsRectEmpty(&m_rc))
if (!IsLanded() || m_rc.IsRectEmpty())
return FALSE;
// The background color is needed for transparency of selection
@ -204,7 +204,7 @@ void SelectionModel::Landing()
m_bShow = FALSE;
if (m_bContentChanged ||
(!::EqualRect(m_rc, m_rcOld) && !::IsRectEmpty(m_rc) && !::IsRectEmpty(m_rcOld)))
(!m_rc.EqualRect(m_rcOld) && !m_rc.IsRectEmpty() && !m_rcOld.IsRectEmpty()))
{
imageModel.PushImageForUndo();
@ -410,10 +410,8 @@ void SelectionModel::DrawFramePoly(HDC hDCImage)
void SelectionModel::SetRectFromPoints(const POINT& ptFrom, const POINT& ptTo)
{
m_rc.left = min(ptFrom.x, ptTo.x);
m_rc.top = min(ptFrom.y, ptTo.y);
m_rc.right = max(ptFrom.x, ptTo.x);
m_rc.bottom = max(ptFrom.y, ptTo.y);
m_rc = CRect(ptFrom, ptTo);
m_rc.NormalizeRect();
}
void SelectionModel::Dragging(HITTEST hit, POINT pt)
@ -452,7 +450,7 @@ void SelectionModel::Dragging(HITTEST hit, POINT pt)
break;
case HIT_BORDER:
case HIT_INNER:
OffsetRect(&m_rc, pt.x - m_ptHit.x, pt.y - m_ptHit.y);
m_rc.OffsetRect(pt.x - m_ptHit.x, pt.y - m_ptHit.y);
break;
}
m_ptHit = pt;
@ -481,8 +479,8 @@ void SelectionModel::HideSelection()
m_bShow = m_bContentChanged = FALSE;
ClearColorImage();
ClearMaskImage();
::SetRectEmpty(&m_rc);
::SetRectEmpty(&m_rcOld);
m_rc.SetRectEmpty();
m_rcOld.SetRectEmpty();
imageModel.NotifyImageChanged();
}
@ -533,9 +531,9 @@ HITTEST SelectionModel::hitTest(POINT ptCanvas)
if (!m_bShow)
return HIT_NONE;
RECT rcSelection = m_rc;
CRect rcSelection = m_rc;
canvasWindow.ImageToCanvas(rcSelection);
::InflateRect(&rcSelection, GRIP_SIZE, GRIP_SIZE);
rcSelection.InflateRect(GRIP_SIZE, GRIP_SIZE);
return getSizeBoxHitTest(ptCanvas, &rcSelection);
}
@ -544,10 +542,9 @@ void SelectionModel::drawFrameOnCanvas(HDC hCanvasDC)
if (!m_bShow)
return;
RECT rcSelection = m_rc;
CRect rcSelection = m_rc;
canvasWindow.ImageToCanvas(rcSelection);
::InflateRect(&rcSelection, GRIP_SIZE, GRIP_SIZE);
rcSelection.InflateRect(GRIP_SIZE, GRIP_SIZE);
drawSizeBoxes(hCanvasDC, &rcSelection, TRUE);
}
@ -557,7 +554,7 @@ void SelectionModel::moveSelection(INT xDelta, INT yDelta)
return;
TakeOff();
::OffsetRect(&m_rc, xDelta, yDelta);
m_rc.OffsetRect(xDelta, yDelta);
canvasWindow.Invalidate();
}

View file

@ -79,21 +79,21 @@ BOOL getSizeBoxRect(LPRECT prc, HITTEST hit, LPCRECT prcBase)
HITTEST getSizeBoxHitTest(POINT pt, LPCRECT prcBase)
{
RECT rc;
CRect rc;
if (!::PtInRect(prcBase, pt))
return HIT_NONE;
rc = *prcBase;
::InflateRect(&rc, -GRIP_SIZE, -GRIP_SIZE);
if (::PtInRect(&rc, pt))
rc.InflateRect(-GRIP_SIZE, -GRIP_SIZE);
if (rc.PtInRect(pt))
return HIT_INNER;
for (INT i = HIT_UPPER_LEFT; i <= HIT_LOWER_RIGHT; ++i)
{
HITTEST hit = (HITTEST)i;
getSizeBoxRect(&rc, hit, prcBase);
if (::PtInRect(&rc, pt))
if (rc.PtInRect(pt))
return hit;
}
@ -110,7 +110,7 @@ VOID drawSizeBoxes(HDC hdc, LPCRECT prcBase, BOOL bDrawFrame, LPCRECT prcPaint)
if (bDrawFrame)
{
rc = *prcBase;
::InflateRect(&rc, -GRIP_SIZE / 2, -GRIP_SIZE / 2);
rc.InflateRect(-GRIP_SIZE / 2, -GRIP_SIZE / 2);
LOGBRUSH logBrush = { BS_HOLLOW, 0, 0 };
COLORREF rgbHighlight = ::GetSysColor(COLOR_HIGHLIGHT);

View file

@ -52,7 +52,7 @@ void CTextEditWindow::FixEditPos(LPCWSTR pszOldText)
RECT rcParent;
::GetWindowRect(m_hwndParent, &rcParent);
RECT rc, rcWnd, rcText;
CRect rc, rcWnd, rcText;
GetWindowRect(&rcWnd);
rcText = rcWnd;
@ -76,7 +76,7 @@ void CTextEditWindow::FixEditPos(LPCWSTR pszOldText)
rcWnd = rc;
::GetClientRect(m_hwndParent, &rcParent);
IntersectRect(&rc, &rcParent, &rcWnd);
rc.IntersectRect(&rcParent, &rcWnd);
MoveWindow(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, FALSE);
@ -158,13 +158,13 @@ LRESULT CTextEditWindow::OnPaint(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL&
LRESULT CTextEditWindow::OnNCPaint(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
RECT rc;
CRect rc;
GetWindowRect(&rc);
HDC hDC = GetDCEx(NULL, DCX_WINDOW | DCX_PARENTCLIP);
if (hDC)
{
OffsetRect(&rc, -rc.left, -rc.top);
rc.OffsetRect(-rc.left, -rc.top);
DrawGrip(hDC, rc);
ReleaseDC(hDC);
}

View file

@ -169,8 +169,8 @@ VOID CToolSettingsWindow::drawLine(HDC hdc, LPCRECT prc)
for (INT i = 0; i < 5; i++)
{
INT penWidth = i + 1;
RECT rcLine = rects[i];
::InflateRect(&rcLine, -2, 0);
CRect rcLine = rects[i];
rcLine.InflateRect(-2, 0);
rcLine.top = (rcLine.top + rcLine.bottom - penWidth) / 2;
rcLine.bottom = rcLine.top + penWidth;
if (toolsModel.GetLineWidth() == penWidth)
@ -242,17 +242,17 @@ static inline INT getBoxRects(RECT rects[3], LPCRECT prc, LPPOINT ppt = NULL)
VOID CToolSettingsWindow::drawBox(HDC hdc, LPCRECT prc)
{
RECT rects[3];
CRect rects[3];
getBoxRects(rects, prc);
for (INT iItem = 0; iItem < 3; ++iItem)
{
RECT& rcItem = rects[iItem];
CRect& rcItem = rects[iItem];
if (toolsModel.GetShapeStyle() == iItem)
::FillRect(hdc, &rcItem, ::GetSysColorBrush(COLOR_HIGHLIGHT));
::InflateRect(&rcItem, -5, -5);
rcItem.InflateRect(-5, -5);
if (iItem <= 1)
{
@ -289,9 +289,9 @@ LRESULT CToolSettingsWindow::OnCreate(UINT nMsg, WPARAM wParam, LPARAM lParam, B
m_hTranspIcon = (HICON)LoadImageW(g_hinstExe, MAKEINTRESOURCEW(IDI_TRANSPARENT),
IMAGE_ICON, CX_TRANS_ICON, CY_TRANS_ICON, LR_DEFAULTCOLOR);
RECT trackbarZoomPos, rect2;
CRect trackbarZoomPos, rect2;
calculateTwoBoxes(trackbarZoomPos, rect2);
::InflateRect(&trackbarZoomPos, -1, -1);
trackbarZoomPos.InflateRect(-1, -1);
trackbarZoom.Create(TRACKBAR_CLASS, m_hWnd, trackbarZoomPos, NULL, WS_CHILD | TBS_VERT | TBS_AUTOTICKS);
trackbarZoom.SendMessage(TBM_SETRANGE, TRUE, MAKELPARAM(MIN_ZOOM_TRACK, MAX_ZOOM_TRACK));
@ -336,23 +336,23 @@ LRESULT CToolSettingsWindow::OnNotify(UINT nMsg, WPARAM wParam, LPARAM lParam, B
return 0;
}
VOID CToolSettingsWindow::calculateTwoBoxes(RECT& rect1, RECT& rect2)
VOID CToolSettingsWindow::calculateTwoBoxes(CRect& rect1, CRect& rect2)
{
RECT rcClient;
CRect rcClient;
GetClientRect(&rcClient);
::InflateRect(&rcClient, -MARGIN1, -MARGIN1);
rcClient.InflateRect(-MARGIN1, -MARGIN1);
INT yCenter = (rcClient.top + rcClient.bottom) / 2;
::SetRect(&rect1, rcClient.left, rcClient.top, rcClient.right, yCenter);
::SetRect(&rect2, rcClient.left, yCenter, rcClient.right, rcClient.bottom);
rect1.SetRect(rcClient.left, rcClient.top, rcClient.right, yCenter);
rect2.SetRect(rcClient.left, yCenter, rcClient.right, rcClient.bottom);
::InflateRect(&rect1, -MARGIN2, -MARGIN2);
::InflateRect(&rect2, -MARGIN2, -MARGIN2);
rect1.InflateRect(-MARGIN2, -MARGIN2);
rect2.InflateRect(-MARGIN2, -MARGIN2);
}
LRESULT CToolSettingsWindow::OnPaint(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
RECT rect1, rect2;
CRect rect1, rect2;
calculateTwoBoxes(rect1, rect2);
PAINTSTRUCT ps;
@ -364,8 +364,8 @@ LRESULT CToolSettingsWindow::OnPaint(UINT nMsg, WPARAM wParam, LPARAM lParam, BO
if (toolsModel.GetActiveTool() >= TOOL_RECT)
::DrawEdge(hdc, &rect2, BDR_SUNKENOUTER, BF_RECT | BF_MIDDLE);
::InflateRect(&rect1, -MARGIN2, -MARGIN2);
::InflateRect(&rect2, -MARGIN2, -MARGIN2);
rect1.InflateRect(-MARGIN2, -MARGIN2);
rect2.InflateRect(-MARGIN2, -MARGIN2);
switch (toolsModel.GetActiveTool())
{
case TOOL_FREESEL:
@ -407,7 +407,7 @@ LRESULT CToolSettingsWindow::OnLButtonDown(UINT nMsg, WPARAM wParam, LPARAM lPar
{
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
RECT rect1, rect2;
CRect rect1, rect2;
calculateTwoBoxes(rect1, rect2);
RECT rects[12];

View file

@ -37,7 +37,7 @@ private:
VOID drawLine(HDC hdc, LPCRECT prc);
VOID drawBox(HDC hdc, LPCRECT prc);
VOID drawAirBrush(HDC hdc, LPCRECT prc);
VOID calculateTwoBoxes(RECT& rect1, RECT& rect2);
VOID calculateTwoBoxes(CRect& rect1, CRect& rect2);
LRESULT OnCreate(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnDestroy(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);