[MSPAINT] Fix Copy-To-File feature (#5877)

The Copy-To-File feature had some bugs that the user couldn't save.
- Modify SelectionModel::GetSelectionContents.
- Delete SelectionModel::CopyBitmap, SelectionModel::LockBitmap,
  and SelectionModel::UnlockBitmap functions.
CORE-19186
This commit is contained in:
Katayama Hirofumi MZ 2023-11-04 05:56:10 +09:00 committed by GitHub
parent e627c3b00e
commit d7ece626cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 51 additions and 59 deletions

View file

@ -361,10 +361,10 @@ void CFontsDialog::InitFontSizes()
HWND hwndSizes = GetDlgItem(IDD_FONTSSIZES);
ComboBox_ResetContent(hwndSizes);
TCHAR szText[16];
WCHAR szText[16];
for (UINT i = 0; i < _countof(s_sizes); ++i)
{
wsprintf(szText, TEXT("%d"), s_sizes[i]);
StringCchPrintfW(szText, _countof(szText), L"%d", s_sizes[i]);
INT iItem = ComboBox_AddString(hwndSizes, szText);
if (s_sizes[i] == (INT)registrySettings.PointSize)
ComboBox_SetCurSel(hwndSizes, iItem);
@ -372,7 +372,7 @@ void CFontsDialog::InitFontSizes()
if (ComboBox_GetCurSel(hwndSizes) == CB_ERR)
{
wsprintf(szText, TEXT("%d"), (INT)registrySettings.PointSize);
StringCchPrintfW(szText, _countof(szText), L"%d", (INT)registrySettings.PointSize);
::SetWindowText(hwndSizes, szText);
}
}

View file

@ -7,6 +7,7 @@
#include "precomp.h"
#include <dlgs.h>
#include <mapi.h>
POINT g_ptStart, g_ptEnd;
@ -55,7 +56,7 @@ FileExtFromFilter(LPTSTR pExt, OPENFILENAME *pOFN)
CharLower(pExt);
return TRUE;
}
pch += lstrlen(pch) + 1;
pch += wcslen(pch) + 1;
}
return FALSE;
}
@ -66,6 +67,7 @@ OFNHookProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HWND hParent;
OFNOTIFY *pon;
WCHAR Path[MAX_PATH];
switch (uMsg)
{
case WM_NOTIFY:
@ -73,11 +75,10 @@ OFNHookProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
if (pon->hdr.code == CDN_TYPECHANGE)
{
hParent = GetParent(hwnd);
TCHAR Path[MAX_PATH];
SendMessage(hParent, CDM_GETFILEPATH, _countof(Path), (LPARAM)Path);
FileExtFromFilter(PathFindExtension(Path), pon->lpOFN);
SendMessage(hParent, CDM_SETCONTROLTEXT, 0x047c, (LPARAM)PathFindFileName(Path));
lstrcpyn(pon->lpOFN->lpstrFile, Path, pon->lpOFN->nMaxFile);
SendMessageW(hParent, CDM_GETFILEPATH, _countof(Path), (LPARAM)Path);
FileExtFromFilter(PathFindExtensionW(Path), pon->lpOFN);
SendMessageW(hParent, CDM_SETCONTROLTEXT, cmb13, (LPARAM)PathFindFileNameW(Path));
StringCchCopyW(pon->lpOFN->lpstrFile, pon->lpOFN->nMaxFile, Path);
}
break;
}
@ -251,7 +252,7 @@ BOOL CMainWindow::GetSaveFileName(IN OUT LPTSTR pszFile, INT cchMaxFile)
if (*pchDotExt == UNICODE_NULL)
{
// Choose PNG
wcscat(pszFile, L".png");
StringCchCatW(pszFile, cchMaxFile, L".png");
for (INT i = 0; i < aguidFileTypesE.GetSize(); ++i)
{
if (aguidFileTypesE[i] == Gdiplus::ImageFormatPNG)

View file

@ -25,6 +25,7 @@
#include <math.h>
#include <shellapi.h>
#include <htmlhelp.h>
#include <strsafe.h>
#include "atlimagedx.h"
#include <debug.h>

View file

@ -123,7 +123,7 @@ void RegistrySettings::Load(INT nCmdShow)
TCHAR szName[64];
for (INT i = 0; i < MAX_RECENT_FILES; ++i)
{
wsprintf(szName, _T("File%u"), i + 1);
StringCchPrintfW(szName, _countof(szName), L"File%u", i + 1);
ReadString(files, szName, strFiles[i]);
}
}
@ -204,10 +204,10 @@ void RegistrySettings::Store()
CRegKey files;
if (files.Create(paint, _T("Recent File List")) == ERROR_SUCCESS)
{
TCHAR szName[64];
WCHAR szName[64];
for (INT iFile = 0; iFile < MAX_RECENT_FILES; ++iFile)
{
wsprintf(szName, _T("File%u"), iFile + 1);
StringCchPrintfW(szName, _countof(szName), L"File%u", iFile + 1);
files.SetStringValue(szName, strFiles[iFile]);
}
}

View file

@ -152,16 +152,19 @@ void SelectionModel::DrawSelection(HDC hDCImage, COLORREF crBg, BOOL bBgTranspar
DeleteDC(hMemDC);
}
void SelectionModel::GetSelectionContents(HDC hDCImage)
HBITMAP SelectionModel::GetSelectionContents()
{
ClearColorImage();
if (m_hbmColor)
return CopyDIBImage(m_hbmColor, m_rc.Width(), m_rc.Height());
HDC hMemDC = ::CreateCompatibleDC(NULL);
m_hbmColor = CreateColorDIB(m_rc.Width(), m_rc.Height(), RGB(255, 255, 255));
HGDIOBJ hbmOld = ::SelectObject(hMemDC, m_hbmColor);
::BitBlt(hMemDC, 0, 0, m_rc.Width(), m_rc.Height(), hDCImage, m_rc.left, m_rc.top, SRCCOPY);
HBITMAP hBitmap = CreateColorDIB(m_rc.Width(), m_rc.Height(), RGB(255, 255, 255));
HGDIOBJ hbmOld = ::SelectObject(hMemDC, hBitmap);
::BitBlt(hMemDC, 0, 0, m_rc.Width(), m_rc.Height(), imageModel.GetDC(), m_rc.left, m_rc.top, SRCCOPY);
::SelectObject(hMemDC, hbmOld);
::DeleteDC(hMemDC);
return hBitmap;
}
BOOL SelectionModel::IsLanded() const
@ -178,7 +181,8 @@ BOOL SelectionModel::TakeOff()
m_rgbBack = paletteModel.GetBgColor();
// Get the contents of the selection area
GetSelectionContents(imageModel.GetDC());
ClearColorImage();
m_hbmColor = GetSelectionContents();
// RectSel doesn't need the mask image
if (toolsModel.GetActiveTool() == TOOL_RECTSEL)
@ -406,13 +410,6 @@ void SelectionModel::StretchSkew(int nStretchPercentX, int nStretchPercentY, int
NotifyContentChanged();
}
HBITMAP SelectionModel::CopyBitmap()
{
if (m_hbmColor == NULL)
GetSelectionContents(imageModel.GetDC());
return CopyDIBImage(m_hbmColor);
}
int SelectionModel::PtStackSize() const
{
return m_iPtSP;
@ -545,18 +542,6 @@ void SelectionModel::SwapWidthAndHeight()
m_rc.bottom = m_rc.top + cx;
}
HBITMAP SelectionModel::LockBitmap()
{
HBITMAP hbm = m_hbmColor;
m_hbmColor = NULL;
return hbm;
}
void SelectionModel::UnlockBitmap(HBITMAP hbmLocked)
{
m_hbmColor = hbmLocked;
}
void SelectionModel::StretchSelection(BOOL bShrink)
{
if (!m_bShow)

View file

@ -40,10 +40,7 @@ public:
void HideSelection();
void DeleteSelection();
HBITMAP CopyBitmap();
HBITMAP LockBitmap();
void UnlockBitmap(HBITMAP hbmLocked);
void GetSelectionContents(HDC hDCImage);
HBITMAP GetSelectionContents();
void DrawFramePoly(HDC hDCImage);
void DrawBackground(HDC hDCImage);
void DrawBackgroundPoly(HDC hDCImage, COLORREF crBg);

View file

@ -342,7 +342,7 @@ void CTextEditWindow::UpdateFont()
lf.lfWeight = (registrySettings.Bold ? FW_BOLD : FW_NORMAL);
lf.lfItalic = (BYTE)registrySettings.Italic;
lf.lfUnderline = (BYTE)registrySettings.Underline;
lstrcpyn(lf.lfFaceName, registrySettings.strFontName, _countof(lf.lfFaceName));
StringCchCopyW(lf.lfFaceName, _countof(lf.lfFaceName), registrySettings.strFontName);
HDC hdc = GetDC();
if (hdc)

View file

@ -29,7 +29,7 @@ static HWND DoHtmlHelpW(HWND hwndCaller, LPCWSTR pszFile, UINT uCommand, DWORD_P
{
// The function loads the system library, not local
GetSystemDirectoryW(szPath, _countof(szPath));
wcscat(szPath, L"\\hhctrl.ocx");
StringCchCatW(szPath, _countof(szPath), L"\\hhctrl.ocx");
s_hHHCTRL_OCX = LoadLibraryW(szPath);
if (s_hHHCTRL_OCX)
s_pHtmlHelpW = (FN_HtmlHelpW)GetProcAddress(s_hHHCTRL_OCX, "HtmlHelpW");
@ -382,8 +382,8 @@ void CMainWindow::ProcessFileMenu(HMENU hPopupMenu)
assert(_tcslen((LPCTSTR)pathFile) <= MAX_RECENT_PATHNAME_DISPLAY);
// Add an accelerator (by '&') to the item number for quick access
TCHAR szText[4 + MAX_RECENT_PATHNAME_DISPLAY + 1];
wsprintf(szText, _T("&%u %s"), iItem + 1, (LPCTSTR)pathFile);
WCHAR szText[4 + MAX_RECENT_PATHNAME_DISPLAY + 1];
StringCchPrintfW(szText, _countof(szText), L"&%u %s", iItem + 1, (LPCWSTR)pathFile);
INT iMenuItem = (cMenuItems - 2) + iItem;
InsertMenu(hPopupMenu, iMenuItem, MF_BYPOSITION | MF_STRING, IDM_FILE1 + iItem, szText);
@ -720,14 +720,13 @@ LRESULT CMainWindow::OnCommand(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bH
selectionModel.TakeOff();
{
HBITMAP hbmLocked = selectionModel.LockBitmap();
if (hbmLocked)
{
HGLOBAL hGlobal = BitmapToClipboardDIB(hbmLocked);
if (hGlobal)
::SetClipboardData(CF_DIB, hGlobal);
selectionModel.UnlockBitmap(hbmLocked);
}
HBITMAP hbmCopy = selectionModel.GetSelectionContents();
HGLOBAL hGlobal = BitmapToClipboardDIB(hbmCopy);
if (hGlobal)
::SetClipboardData(CF_DIB, hGlobal);
else
ShowOutOfMemory();
::DeleteObject(hbmCopy);
}
CloseClipboard();
@ -841,12 +840,18 @@ LRESULT CMainWindow::OnCommand(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bH
}
case IDM_EDITCOPYTO:
{
WCHAR szFileName[MAX_LONG_PATH] = L"*.png";
WCHAR szFileName[MAX_LONG_PATH];
LoadStringW(g_hinstExe, IDS_DEFAULTFILENAME, szFileName, _countof(szFileName));
if (GetSaveFileName(szFileName, _countof(szFileName)))
{
HBITMAP hbmLocked = selectionModel.LockBitmap();
SaveDIBToFile(hbmLocked, szFileName, FALSE);
selectionModel.UnlockBitmap(hbmLocked);
HBITMAP hbmSelection = selectionModel.GetSelectionContents();
if (!hbmSelection)
{
ShowOutOfMemory();
break;
}
SaveDIBToFile(hbmSelection, szFileName, FALSE);
DeleteObject(hbmSelection);
}
break;
}
@ -982,9 +987,12 @@ LRESULT CMainWindow::OnCommand(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bH
toolsModel.SetBackgroundTransparent(!toolsModel.IsBackgroundTransparent());
break;
case IDM_IMAGECROP:
imageModel.PushImageForUndo(selectionModel.CopyBitmap());
{
HBITMAP hbmCopy = selectionModel.GetSelectionContents();
imageModel.PushImageForUndo(hbmCopy);
selectionModel.HideSelection();
break;
}
case IDM_VIEWTOOLBOX:
registrySettings.ShowToolBox = !toolBoxContainer.IsWindowVisible();
toolBoxContainer.ShowWindow(registrySettings.ShowToolBox ? SW_SHOWNOACTIVATE : SW_HIDE);