Skip to content

Commit 3971d69

Browse files
committed
refactor: modernize RecalcZoom
- Guard RecalcZoom early when magnification is disabled - Replace raw main-window handle lookup with GetSafeHwnd() - Add null checks for HDC and memory DCs - Preserve and restore selected bitmaps before StretchBlt - Delete DCs conditionally and release window DC safely - Improve variable naming and formatting for clarity
1 parent ca643d6 commit 3971d69

1 file changed

Lines changed: 32 additions & 29 deletions

File tree

ColorCopDlg.cpp

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -710,44 +710,52 @@ void CColorCopDlg::OnPaint()
710710
}
711711
}
712712

713-
714-
715-
716713
void CColorCopDlg::RecalcZoom()
717714
{
718-
// zoom level has changed while not magnifying..
715+
if (m_MagLevel <= 0)
716+
return;
719717

720-
int magwidth = magrect.Width() / m_MagLevel;
718+
int magwidth = magrect.Width() / m_MagLevel;
721719
int magheight = magrect.Height() / m_MagLevel;
722-
HWND CCopHWNDtemp=AfxGetApp()->GetMainWnd()->m_hWnd;
720+
721+
HWND hwnd = AfxGetMainWnd()->GetSafeHwnd();
723722

724723
if (hZoomBitmap)
725724
{
726-
hdc = ::GetDC(CCopHWNDtemp); // needed only for color depth
725+
hdc = ::GetDC(hwnd);
726+
if (!hdc)
727+
return;
727728

728-
hdcMem = ::CreateCompatibleDC(hdc);
729+
hdcMem = ::CreateCompatibleDC(hdc);
729730
hdcZoomMem = ::CreateCompatibleDC(hdc);
730731

731-
::SelectObject(hdcMem, hBitmap);
732-
::SelectObject(hdcZoomMem, hZoomBitmap);
733-
734-
::StretchBlt(hdcMem, // destination DC
735-
0, 0, // upper left dest
736-
magrect.Width(), magrect.Height(), // width of dest rect
737-
hdcZoomMem, // source DC
738-
((magrect.right - magrect.left)/2) - (magwidth/2), // x coordinate of source
739-
((magrect.bottom - magrect.top)/2) - (magheight/2), // y coordinate of source
740-
magwidth, magheight, // width of source
741-
SRCCOPY); // raster mode
742-
732+
if (hdcMem && hdcZoomMem)
733+
{
734+
HGDIOBJ oldMemBmp = ::SelectObject(hdcMem, hBitmap);
735+
HGDIOBJ oldZoomBmp = ::SelectObject(hdcZoomMem, hZoomBitmap);
736+
737+
::StretchBlt(
738+
hdcMem,
739+
0, 0,
740+
magrect.Width(), magrect.Height(),
741+
hdcZoomMem,
742+
((magrect.Width()) / 2) - (magwidth / 2),
743+
((magrect.Height()) / 2) - (magheight / 2),
744+
magwidth, magheight,
745+
SRCCOPY
746+
);
747+
748+
::SelectObject(hdcMem, oldMemBmp);
749+
::SelectObject(hdcZoomMem, oldZoomBmp);
750+
}
743751

744-
::DeleteDC(hdcZoomMem);// kill the temporary DC
745-
::DeleteDC(hdcMem); // kill the temporary DC
752+
if (hdcZoomMem) ::DeleteDC(hdcZoomMem);
753+
if (hdcMem) ::DeleteDC(hdcMem);
746754

747-
::ReleaseDC(CCopHWNDtemp, hdc); // let go of the memory
755+
::ReleaseDC(hwnd, hdc);
748756
}
757+
749758
InvalidateRect(&buttonrect, FALSE);
750-
return;
751759
}
752760

753761
void CColorCopDlg::OnconvertRGB() {
@@ -772,15 +780,10 @@ void CColorCopDlg::OnconvertRGB() {
772780
}
773781

774782
} else if (m_Appflags & ModePowerBuilder) {
775-
776783
m_Hexcolor.Format("%d", (65536 * m_Bluedec) + (256 * m_Greendec) + (m_Reddec));
777-
778784
} else if (m_Appflags & ModeVisualBasic) {
779-
780785
m_Hexcolor.Format("&H%.2x%.2x%.2x", m_Bluedec, m_Greendec, m_Reddec);
781-
782786
} else if (m_Appflags & ModeClarion) {
783-
784787
// same as VB, but in addition
785788
//1) remove the &H at start.
786789
//2) prefix with 0

0 commit comments

Comments
 (0)