[MSPAINT]

- change type of haccel from HANDLE to HACCEL
- port registry code to ATL::CRegKey, which -- through its destructor -- should prevent handle leaks
- rename some parameters and variables
- incorporate some changes suggested by Carlo Bramini in CORE-12080

svn path=/trunk/; revision=72883
This commit is contained in:
Benedikt Freisen
2016-10-01 20:04:43 +00:00
parent fb8f6fd2aa
commit 66a9815a4e
3 changed files with 78 additions and 90 deletions
+2 -2
View File
@@ -93,7 +93,7 @@ _tWinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPTSTR lpszArgument
MSG messages; /* Here messages to the application are saved */
HMENU menu;
HANDLE haccel;
HACCEL haccel;
TCHAR sfnFilename[1000];
TCHAR sfnFiletitle[256];
@@ -303,7 +303,7 @@ _tWinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPTSTR lpszArgument
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage(&messages, NULL, 0, 0))
{
TranslateAccelerator(hwnd, (HACCEL) haccel, &messages);
TranslateAccelerator(hwnd, haccel, &messages);
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
+74 -86
View File
@@ -13,45 +13,48 @@
#include <winreg.h>
/* FUNCTIONS ********************************************************/
static DWORD ReadDWORD(HKEY hKey, LPCTSTR lpName, DWORD *pdwValue, BOOL bCheckForDef)
static DWORD ReadDWORD(CRegKey &key, LPCTSTR lpName, DWORD &dwValue, BOOL bCheckForDef)
{
DWORD dwPrev = *pdwValue;
DWORD cbData = sizeof(DWORD);
DWORD dwPrev = dwValue;
if (ERROR_SUCCESS != RegQueryValueEx(hKey, lpName, 0, NULL, (LPBYTE) pdwValue, &cbData))
*pdwValue = dwPrev;
if (key.QueryDWORDValue(lpName, dwValue) != ERROR_SUCCESS)
dwValue = dwPrev;
if (bCheckForDef && *pdwValue == 0)
*pdwValue = dwPrev;
if (bCheckForDef && dwValue == 0)
dwValue = dwPrev;
return dwPrev;
}
void RegistrySettings::SetWallpaper(TCHAR * FileName, DWORD dwStyle, DWORD dwTile) //FIXME: Has to be called 2x to apply the pattern (tiled/stretched) too
static void ReadFileHistory(CRegKey &key, LPCTSTR lpName, CString &strFile)
{
HKEY hDesktop;
TCHAR szStyle[3], szTile[3];
ULONG nChars = MAX_PATH;
LPTSTR szFile = strFile.GetBuffer(nChars);
if (key.QueryStringValue(lpName, szFile, &nChars) != ERROR_SUCCESS)
szFile[0] = '\0';
strFile.ReleaseBuffer();
}
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID) FileName, SPIF_UPDATEINIFILE);
void RegistrySettings::SetWallpaper(LPCTSTR szFileName, DWORD dwStyle, DWORD dwTile)
{
// TODO: find out whether this line still serves a purpose
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID) szFileName, SPIF_UPDATEINIFILE);
if ((dwStyle > 2) || (dwTile > 2))
return;
if (RegOpenKeyEx(HKEY_CURRENT_USER,
_T("Control Panel\\Desktop"), 0, KEY_READ | KEY_SET_VALUE, &hDesktop) == ERROR_SUCCESS)
CRegKey desktop;
if (desktop.Open(HKEY_CURRENT_USER, _T("Control Panel\\Desktop")) == ERROR_SUCCESS)
{
RegSetValueEx(hDesktop, _T("Wallpaper"), 0, REG_SZ, (LPBYTE) FileName,
_tcslen(FileName) * sizeof(TCHAR));
TCHAR szStyle[3], szTile[3];
desktop.SetStringValue(_T("Wallpaper"), szFileName);
_stprintf(szStyle, _T("%lu"), dwStyle);
_stprintf(szTile, _T("%lu"), dwTile);
RegSetValueEx(hDesktop, _T("WallpaperStyle"), 0, REG_SZ, (LPBYTE) szStyle,
_tcslen(szStyle) * sizeof(TCHAR));
RegSetValueEx(hDesktop, _T("TileWallpaper"), 0, REG_SZ, (LPBYTE) szTile,
_tcslen(szTile) * sizeof(TCHAR));
RegCloseKey(hDesktop);
desktop.SetStringValue(_T("WallpaperStyle"), szStyle);
desktop.SetStringValue(_T("TileWallpaper"), szTile);
}
}
@@ -81,107 +84,92 @@ void RegistrySettings::LoadPresets()
void RegistrySettings::Load()
{
HKEY hView;
LoadPresets();
if (RegOpenKeyEx(HKEY_CURRENT_USER,
_T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\View"),
0, KEY_READ | KEY_SET_VALUE, &hView) == ERROR_SUCCESS)
CRegKey view;
if (view.Open(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\View"), KEY_READ) == ERROR_SUCCESS)
{
DWORD cbData;
ReadDWORD(view, _T("BMPHeight"), BMPHeight, TRUE);
ReadDWORD(view, _T("BMPWidth"), BMPWidth, TRUE);
ReadDWORD(view, _T("GridExtent"), GridExtent, FALSE);
ReadDWORD(view, _T("NoStretching"), NoStretching, FALSE);
ReadDWORD(view, _T("ShowThumbnail"), ShowThumbnail, FALSE);
ReadDWORD(view, _T("SnapToGrid"), SnapToGrid, FALSE);
ReadDWORD(view, _T("ThumbHeight"), ThumbHeight, TRUE);
ReadDWORD(view, _T("ThumbWidth"), ThumbWidth, TRUE);
ReadDWORD(view, _T("ThumbXPos"), ThumbXPos, TRUE);
ReadDWORD(view, _T("ThumbYPos"), ThumbYPos, TRUE);
ReadDWORD(view, _T("UnitSetting"), UnitSetting, FALSE);
ReadDWORD(hView, _T("BMPHeight"), &BMPHeight, TRUE);
ReadDWORD(hView, _T("BMPWidth"), &BMPWidth, TRUE);
ReadDWORD(hView, _T("GridExtent"), &GridExtent, FALSE);
ReadDWORD(hView, _T("NoStretching"), &NoStretching, FALSE);
ReadDWORD(hView, _T("ShowThumbnail"), &ShowThumbnail, FALSE);
ReadDWORD(hView, _T("SnapToGrid"), &SnapToGrid, FALSE);
ReadDWORD(hView, _T("ThumbHeight"), &ThumbHeight, TRUE);
ReadDWORD(hView, _T("ThumbWidth"), &ThumbWidth, TRUE);
ReadDWORD(hView, _T("ThumbXPos"), &ThumbXPos, TRUE);
ReadDWORD(hView, _T("ThumbYPos"), &ThumbYPos, TRUE);
ReadDWORD(hView, _T("UnitSetting"), &UnitSetting, FALSE);
cbData = sizeof(WINDOWPLACEMENT);
RegQueryValueEx(hView, _T("WindowPlacement"), 0, NULL, (LPBYTE) &WindowPlacement, &cbData);
ULONG pnBytes = sizeof(WINDOWPLACEMENT);
view.QueryBinaryValue(_T("WindowPlacement"), &WindowPlacement, &pnBytes);
}
CRegKey hKey;
if (hKey.Open(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\Recent File List"), KEY_READ) == ERROR_SUCCESS)
CRegKey files;
if (files.Open(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\Recent File List"), KEY_READ) == ERROR_SUCCESS)
{
ULONG nChars = MAX_PATH;
LPTSTR pszFile1 = strFile1.GetBuffer(nChars);
hKey.QueryStringValue(_T("File1"), pszFile1, &nChars);
strFile1.ReleaseBuffer();
nChars = MAX_PATH;
LPTSTR pszFile2 = strFile2.GetBuffer(nChars);
hKey.QueryStringValue(_T("File2"), pszFile2, &nChars);
strFile2.ReleaseBuffer();
nChars = MAX_PATH;
LPTSTR pszFile3 = strFile3.GetBuffer(nChars);
hKey.QueryStringValue(_T("File3"), pszFile3, &nChars);
strFile3.ReleaseBuffer();
nChars = MAX_PATH;
LPTSTR pszFile4 = strFile4.GetBuffer(nChars);
hKey.QueryStringValue(_T("File4"), pszFile4, &nChars);
strFile4.ReleaseBuffer();
ReadFileHistory(files, _T("File1"), strFile1);
ReadFileHistory(files, _T("File2"), strFile2);
ReadFileHistory(files, _T("File3"), strFile3);
ReadFileHistory(files, _T("File4"), strFile4);
}
}
void RegistrySettings::Store()
{
HKEY hView;
if (RegCreateKeyEx(HKEY_CURRENT_USER,
_T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\View"),
0, NULL, 0, KEY_READ | KEY_SET_VALUE, NULL, &hView, NULL) == ERROR_SUCCESS)
CRegKey view;
if (view.Create(HKEY_CURRENT_USER,
_T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\View")) == ERROR_SUCCESS)
{
RegSetValueEx(hView, _T("BMPHeight"), 0, REG_DWORD, (LPBYTE) &BMPHeight, sizeof(DWORD));
RegSetValueEx(hView, _T("BMPWidth"), 0, REG_DWORD, (LPBYTE) &BMPWidth, sizeof(DWORD));
RegSetValueEx(hView, _T("GridExtent"), 0, REG_DWORD, (LPBYTE) &GridExtent, sizeof(DWORD));
RegSetValueEx(hView, _T("NoStretching"), 0, REG_DWORD, (LPBYTE) &NoStretching, sizeof(DWORD));
RegSetValueEx(hView, _T("ShowThumbnail"), 0, REG_DWORD, (LPBYTE) &ShowThumbnail, sizeof(DWORD));
RegSetValueEx(hView, _T("SnapToGrid"), 0, REG_DWORD, (LPBYTE) &SnapToGrid, sizeof(DWORD));
RegSetValueEx(hView, _T("ThumbHeight"), 0, REG_DWORD, (LPBYTE) &ThumbHeight, sizeof(DWORD));
RegSetValueEx(hView, _T("ThumbWidth"), 0, REG_DWORD, (LPBYTE) &ThumbWidth, sizeof(DWORD));
RegSetValueEx(hView, _T("ThumbXPos"), 0, REG_DWORD, (LPBYTE) &ThumbXPos, sizeof(DWORD));
RegSetValueEx(hView, _T("ThumbYPos"), 0, REG_DWORD, (LPBYTE) &ThumbYPos, sizeof(DWORD));
RegSetValueEx(hView, _T("UnitSetting"), 0, REG_DWORD, (LPBYTE) &UnitSetting, sizeof(DWORD));
RegSetValueEx(hView, _T("WindowPlacement"), 0, REG_BINARY, (LPBYTE) &WindowPlacement, sizeof(WINDOWPLACEMENT));
view.SetDWORDValue(_T("BMPHeight"), BMPHeight);
view.SetDWORDValue(_T("BMPWidth"), BMPWidth);
view.SetDWORDValue(_T("GridExtent"), GridExtent);
view.SetDWORDValue(_T("NoStretching"), NoStretching);
view.SetDWORDValue(_T("ShowThumbnail"), ShowThumbnail);
view.SetDWORDValue(_T("SnapToGrid"), SnapToGrid);
view.SetDWORDValue(_T("ThumbHeight"), ThumbHeight);
view.SetDWORDValue(_T("ThumbWidth"), ThumbWidth);
view.SetDWORDValue(_T("ThumbXPos"), ThumbXPos);
view.SetDWORDValue(_T("ThumbYPos"), ThumbYPos);
view.SetDWORDValue(_T("UnitSetting"), UnitSetting);
view.SetBinaryValue(_T("WindowPlacement"), &WindowPlacement, sizeof(WINDOWPLACEMENT));
}
CRegKey hKey;
if (hKey.Create(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\Recent File List")) == ERROR_SUCCESS)
CRegKey files;
if (files.Create(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Paint\\Recent File List")) == ERROR_SUCCESS)
{
if (!strFile1.IsEmpty())
hKey.SetStringValue(_T("File1"), strFile1);
files.SetStringValue(_T("File1"), strFile1);
if (!strFile2.IsEmpty())
hKey.SetStringValue(_T("File2"), strFile2);
files.SetStringValue(_T("File2"), strFile2);
if (!strFile3.IsEmpty())
hKey.SetStringValue(_T("File3"), strFile3);
files.SetStringValue(_T("File3"), strFile3);
if (!strFile4.IsEmpty())
hKey.SetStringValue(_T("File4"), strFile4);
files.SetStringValue(_T("File4"), strFile4);
}
}
void RegistrySettings::SetMostRecentFile(LPCTSTR pszPathName)
void RegistrySettings::SetMostRecentFile(LPCTSTR szPathName)
{
if (strFile1 == pszPathName)
if (strFile1 == szPathName)
{
// do nothing
}
else if (strFile2 == pszPathName)
else if (strFile2 == szPathName)
{
CString strTemp = strFile2;
strFile2 = strFile1;
strFile1 = strTemp;
}
else if (strFile3 == pszPathName)
else if (strFile3 == szPathName)
{
CString strTemp = strFile3;
strFile3 = strFile2;
strFile2 = strFile1;
strFile1 = strTemp;
}
else if (strFile4 == pszPathName)
else if (strFile4 == szPathName)
{
CString strTemp = strFile4;
strFile4 = strFile3;
@@ -194,6 +182,6 @@ void RegistrySettings::SetMostRecentFile(LPCTSTR pszPathName)
strFile4 = strFile3;
strFile3 = strFile2;
strFile2 = strFile1;
strFile1 = pszPathName;
strFile1 = szPathName;
}
}
+2 -2
View File
@@ -30,9 +30,9 @@ public:
CString strFile3;
CString strFile4;
static void SetWallpaper(TCHAR *szFileName, DWORD dwStyle, DWORD dwTile);
static void SetWallpaper(LPCTSTR szFileName, DWORD dwStyle, DWORD dwTile);
void Load();
void Store();
void SetMostRecentFile(LPCTSTR pszPathName);
void SetMostRecentFile(LPCTSTR szPathName);
};