[MSPAINT] Improve GetSelectionContents for free-shape selection (#6554)

Follow-up to #6552. There was
a bug that the cropped selection
image is not the shape of selection.
JIRA issue: CORE-19466
- Extend SelectionModel::
  DrawSelection for drawing
  selection flexibly.
- Improve SelectionModel::
  GetSelectionContents method.
This commit is contained in:
Katayama Hirofumi MZ 2024-02-28 22:01:08 +09:00 committed by GitHub
parent f0e45f07e1
commit e928b42758
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 34 additions and 11 deletions

View file

@ -1056,8 +1056,8 @@ LRESULT CMainWindow::OnCommand(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bH
HBITMAP hbmSelection = selectionModel.GetSelectionContents();
if (hbmSelection)
{
selectionModel.HideSelection();
imageModel.PushImageForUndo(hbmSelection);
selectionModel.HideSelection();
imageModel.NotifyImageChanged();
}
break;

View file

@ -58,20 +58,21 @@ void SelectionModel::DrawBackground(HDC hDCImage, COLORREF crBg)
DrawBackgroundRect(hDCImage, crBg);
}
void SelectionModel::DrawSelection(HDC hDCImage, COLORREF crBg, BOOL bBgTransparent)
void
SelectionModel::DrawSelection(HDC hDCImage, COLORREF crBg, BOOL bBgTransparent, const CRect& rc,
HBITMAP hbm)
{
CRect rc = m_rc;
if (rc.IsRectEmpty())
return;
BITMAP bm;
if (!GetObjectW(m_hbmColor, sizeof(BITMAP), &bm))
if (!GetObjectW(hbm, sizeof(BITMAP), &bm))
return;
COLORREF keyColor = (bBgTransparent ? crBg : CLR_INVALID);
HDC hMemDC = CreateCompatibleDC(hDCImage);
HGDIOBJ hbmOld = SelectObject(hMemDC, m_hbmColor);
HGDIOBJ hbmOld = SelectObject(hMemDC, hbm);
ColorKeyedMaskBlt(hDCImage, rc.left, rc.top, rc.Width(), rc.Height(),
hMemDC, 0, 0, bm.bmWidth, bm.bmHeight, m_hbmMask, keyColor);
SelectObject(hMemDC, hbmOld);
@ -89,13 +90,23 @@ void SelectionModel::setMask(const CRect& rc, HBITMAP hbmMask)
HBITMAP SelectionModel::GetSelectionContents()
{
if (m_hbmColor)
return CopyDIBImage(m_hbmColor, m_rc.Width(), m_rc.Height());
HBITMAP hbmWhole = imageModel.LockBitmap();
HBITMAP hbmPart = getSubImage(hbmWhole, m_rc);
HBITMAP hbmPart = getSubImage(hbmWhole, (IsLanded() ? m_rc : m_rcOld));
imageModel.UnlockBitmap(hbmWhole);
return hbmPart;
if (!hbmPart)
return NULL;
CRect rc = { 0, 0, m_rc.Width(), m_rc.Height() };
HDC hdcMem = ::CreateCompatibleDC(NULL);
HBITMAP hbmNew = CreateColorDIB(rc.Width(), rc.Height(), paletteModel.GetBgColor());
HGDIOBJ hbmOld = ::SelectObject(hdcMem, hbmNew);
selectionModel.DrawSelection(hdcMem, paletteModel.GetBgColor(), TRUE, rc, hbmPart);
::SelectObject(hdcMem, hbmOld);
::DeleteDC(hdcMem);
::DeleteObject(hbmPart);
return hbmNew;
}
BOOL SelectionModel::IsLanded() const

View file

@ -41,7 +41,19 @@ public:
void DrawBackground(HDC hDCImage, COLORREF crBg);
void DrawBackgroundPoly(HDC hDCImage, COLORREF crBg);
void DrawBackgroundRect(HDC hDCImage, COLORREF crBg);
void DrawSelection(HDC hDCImage, COLORREF crBg = 0, BOOL bBgTransparent = FALSE);
void DrawSelection(HDC hDCImage, COLORREF crBg, BOOL bBgTransparent, const CRect& rc, HBITMAP hbm);
void DrawSelection(HDC hDCImage, COLORREF crBg, BOOL bBgTransparent)
{
return DrawSelection(hDCImage, crBg, bBgTransparent, m_rc);
}
void DrawSelection(HDC hDCImage, COLORREF crBg, BOOL bBgTransparent, const CRect& rc)
{
return DrawSelection(hDCImage, crBg, bBgTransparent, rc, m_hbmColor);
}
void InsertFromHBITMAP(HBITMAP hbmColor, INT x = 0, INT y = 0, HBITMAP hbmMask = NULL);
// operation