📄 cpgpdiskappwinutils.cpp
字号:
push 1
push 0x0000FFFF
push largeDrive
push parentHwnd
call [SHFormatDriveFunc]
sub esp, 0x10
}
}
__except(ExceptionFilter(GetExceptionCode()))
{
derr = DualErr(kPGDMinorError_SHFormatDriveFailed);
}
}
if (loadedShell32)
FreeLibrary(shell32Handle);
return derr;
}
//////////////////////////////
// Interface Utility Functions
//////////////////////////////
// TweakOnTopAttribute makes the selected window "on top" if the main window's
// "on-top" preference is set.
void
CPGPdiskApp::TweakOnTopAttribute(CWnd *pWnd)
{
PGPUInt32 checkState;
PGPdiskWin32Prefs prefs;
pgpAssertAddrValid(pWnd, CWnd);
if (GetPGPdiskWin32Prefs(prefs).IsntError())
{
checkState = prefs.mainStayOnTop;
if (checkState == MF_CHECKED)
{
pWnd->SetWindowPos(&CWnd::wndTopMost, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE);
}
}
}
// CreateDIBPalette is a helper function for GetBitmapAndPalette.
HPALETTE
CPGPdiskApp::CreateDIBPalette(LPBITMAPINFO lpbmi, LPINT lpiNumColors)
{
LPBITMAPINFOHEADER lpbi;
LPLOGPALETTE lpPal;
HANDLE hLogPal;
HPALETTE hPal = NULL;
PGPInt32 i;
lpbi = (LPBITMAPINFOHEADER) lpbmi;
if (lpbi->biBitCount <= 8)
{
(* lpiNumColors) = (1 << lpbi->biBitCount);
}
else
{
(* lpiNumColors = 0); // No palette needed for 24 BPP DIB
}
if (* lpiNumColors)
{
hLogPal = GlobalAlloc(GHND, sizeof(LOGPALETTE) +
sizeof(PALETTEENTRY) * (* lpiNumColors));
lpPal = (LPLOGPALETTE) GlobalLock(hLogPal);
lpPal->palVersion = 0x300;
lpPal->palNumEntries = *lpiNumColors;
for (i = 0; i < (* lpiNumColors); i++)
{
lpPal->palPalEntry[i].peRed = lpbmi->bmiColors[i].rgbRed;
lpPal->palPalEntry[i].peGreen = lpbmi->bmiColors[i].rgbGreen;
lpPal->palPalEntry[i].peBlue = lpbmi->bmiColors[i].rgbBlue;
lpPal->palPalEntry[i].peFlags = 0;
}
hPal = CreatePalette(lpPal);
GlobalUnlock(hLogPal);
GlobalFree(hLogPal);
}
return hPal;
}
// GetBitmapAndPalette loads a 256-color bitmap as a resource and constructs
// the necessary palette to draw it with.
BOOL
CPGPdiskApp::GetBitmapAndPalette(
UINT nIDResource,
CBitmap *bitmap,
CPalette *pal)
{
HRSRC hRsrc;
HGLOBAL hGlobal;
HBITMAP hBitmapFinal = NULL;
LPBITMAPINFOHEADER lpbi;
HDC hdc;
INT iNumColors;
hRsrc = FindResource(AfxGetInstanceHandle(),
MAKEINTRESOURCE(nIDResource), RT_BITMAP);
if (IsntNull(hRsrc))
{
hGlobal = LoadResource(AfxGetInstanceHandle(), hRsrc);
lpbi = (LPBITMAPINFOHEADER) LockResource(hGlobal);
hdc = GetDC(NULL);
if (pal->Attach(CreateDIBPalette((LPBITMAPINFO) lpbi, &iNumColors)))
{
SelectPalette(hdc, (* pal), FALSE);
RealizePalette(hdc);
}
hBitmapFinal = CreateDIBitmap(hdc, (LPBITMAPINFOHEADER)lpbi,
(LONG) CBM_INIT,
(LPSTR) lpbi + lpbi->biSize + iNumColors * sizeof(RGBQUAD),
(LPBITMAPINFO) lpbi, DIB_RGB_COLORS);
bitmap->Attach(hBitmapFinal);
ReleaseDC(NULL,hdc);
UnlockResource(hGlobal);
FreeResource(hGlobal);
return TRUE;
}
else
{
return FALSE;
}
}
// PaintRegistrationInfo paints the user registration info on an about box or
// splash screen of the given size using the given DC in the given window.
void
CPGPdiskApp::PaintRegistrationInfo(
CDC *pDC,
PGPUInt32 x,
PGPUInt32 y,
COLORREF textColor,
CWnd *pWnd)
{
try
{
CSize licenseToSize;
CString orgText, userText;
HFONT oldFont;
LPSTR licenseToText;
PGPInt32 maxPixelLength, widthLicenseTo;
RECT windowRect;
pgpAssertAddrValid(pDC, CDC);
pgpAssertAddrValid(pWnd, CWnd);
pDC->SetTextColor(textColor);
pDC->SetBkMode(TRANSPARENT);
// MFC bug - CDC::SelectObject returns incorrect pointers/handles so
// don't fool with it.
oldFont = (HFONT) SelectObject(pDC->GetSafeHdc(),
GetStockObject(DEFAULT_GUI_FONT));
// Determine the maximum pixel string width we can draw.
pWnd->GetClientRect(&windowRect);
maxPixelLength = windowRect.right - x - 5;
pgpAssert(maxPixelLength > 0);
// Truncate the strings with periods if they are too long.
TruncateDisplayString(App->mOrgText, &orgText, pDC, maxPixelLength);
TruncateDisplayString(App->mUserText, &userText, pDC, maxPixelLength);
licenseToText = (LPSTR) GetCommonString(kPGPdiskLicenseToText);
// Draw the static string.
licenseToSize = pDC->GetOutputTextExtent(licenseToText,
strlen(licenseToText));
pDC->LPtoDP(&licenseToSize);
widthLicenseTo = licenseToSize.cx;
pgpAssert(x > widthLicenseTo + kLicenseToGap);
pDC->TextOut(x - widthLicenseTo - kLicenseToGap, y, licenseToText,
strlen(licenseToText));
// Draw the variable strings.
if (!userText.IsEmpty())
{
pDC->TextOut(x, y, userText, userText.GetLength());
y += kRegTextInc;
}
if (!orgText.IsEmpty())
{
pDC->TextOut(x, y, orgText, orgText.GetLength());
y += kRegTextInc;
}
pDC->SelectObject(oldFont);
}
catch (CMemoryException *ex)
{
ex->Delete();
}
}
// PaintVersionInfo paints the PGPdisk version info on an about box or splash
// screen of the given size using the given DC in the given window at the
// given location.
void
CPGPdiskApp::PaintVersionInfo(
CDC *pDC,
PGPUInt32 x,
PGPUInt32 y,
COLORREF textColor,
CWnd *pWnd)
{
HFONT oldFont;
pgpAssertAddrValid(pDC, CDC);
pgpAssertAddrValid(pWnd, CWnd);
pDC->SetTextColor(textColor);
pDC->SetBkMode(TRANSPARENT);
// MFC bug - CDC::SelectObject returns incorrect pointers/handles so don't
// fool with it.
oldFont = (HFONT) SelectObject(pDC->GetSafeHdc(),
GetStockObject(DEFAULT_GUI_FONT));
pDC->TextOut(x, y, kVersionTextString, strlen(kVersionTextString));
pDC->SelectObject(oldFont);
}
// GetTextRect returns the size of the rectangle the given text would occupy
// if drawn in the given window.
void
CPGPdiskApp::GetTextRect(LPCSTR text, CWnd *pWnd, RECT *pRect)
{
CDC memDC;
CDC *pDC;
CSize textSize;
HFONT oldFont;
pgpAssertStrValid(text);
pgpAssertAddrValid(pWnd, CWnd);
pgpAssertAddrValid(pRect, RECT);
pDC = pWnd->GetDC();
memDC.CreateCompatibleDC(pDC);
// MFC bug - CDC::SelectObject returns incorrect pointers/handles so
// don't fool with it.
oldFont = (HFONT) SelectObject(memDC.GetSafeHdc(),
GetStockObject(DEFAULT_GUI_FONT));
// Get dimensions of text.
textSize = memDC.GetOutputTextExtent(text, strlen(text));
memDC.LPtoDP(&textSize);
pRect->left = 0;
pRect->right = textSize.cx;
pRect->top = 0;
pRect->bottom = textSize.cy;
memDC.SelectObject(oldFont);
memDC.DeleteDC();
pWnd->ReleaseDC(pDC);
}
// TruncateDisplayString will, given a string, a DC, and a maximum length
// (in pixels), return a string truncated with dots that will fit within that
// length.
void
CPGPdiskApp::TruncateDisplayString(
LPCSTR inString,
CString *outString,
CDC *pDC,
PGPInt32 maxPixelLength)
{
try
{
PGPBoolean didWeTruncateString = FALSE;
PGPUInt32 i, numInputChars;
pgpAssertStrValid(inString);
pgpAssertAddrValid(outString, CString);
pgpAssertAddrValid(pDC, CDC);
pgpAssert(maxPixelLength > 0);
numInputChars = strlen(inString);
// Calculate the length of each substring that begins at position 0.
// Once we reach a substring that exceeds our maximum pixel length,
// we truncate it with dots.
for (i=0; i < numInputChars; i++)
{
CSize stringRect;
CString csInString;
csInString = inString;
// Calculate pixel width of string.
stringRect = pDC->GetOutputTextExtent(inString, i);
pDC->LPtoDP(&stringRect);
// If too large, ditch last two chars and tack on 3 dots.
if (stringRect.cx > maxPixelLength)
{
didWeTruncateString = TRUE;
if (i > 3)
{
(* outString) = csInString.Left(i - 3);
(* outString) += "...";
}
else
{
(* outString) = csInString.Left(i - 1);
}
break;
}
}
if (!didWeTruncateString)
{
(* outString) = inString;
}
}
catch (CMemoryException *ex)
{
ex->Delete();
}
}
// FitStringToWindow truncates the string so it fits within the given window
// using the default display font.
void
CPGPdiskApp::FitStringToWindow(
LPCSTR inString,
CString *outString,
CWnd *pWnd)
{
CDC memDC;
CDC *pDC;
CSize messageSize;
HFONT oldFont;
PGPUInt32 widthMessage, xWindow;
RECT windowRect;
pgpAssertStrValid(inString);
pgpAssertAddrValid(outString, CString);
pgpAssertAddrValid(pWnd, CWnd);
// Get dimensions of window.
pWnd->GetWindowRect(&windowRect);
pgpAssert(windowRect.right - windowRect.left > 3);
xWindow = windowRect.right - windowRect.left - 3;
pDC = pWnd->GetDC();
memDC.CreateCompatibleDC(pDC);
// MFC bug - CDC::SelectObject returns incorrect pointers/handles so
// don't fool with it.
oldFont = (HFONT) SelectObject(memDC.GetSafeHdc(),
GetStockObject(DEFAULT_GUI_FONT));
// Get dimensions of text to be written.
messageSize = memDC.GetOutputTextExtent(inString, strlen(inString));
memDC.LPtoDP(&messageSize);
widthMessage = messageSize.cx;
// If too big truncate string.
if (widthMessage > xWindow)
TruncateDisplayString(inString, outString, &memDC, xWindow);
memDC.SelectObject(oldFont);
memDC.DeleteDC();
pWnd->ReleaseDC(pDC);
}
// FormatFitStringToWindow takes a string of the form "...%s..." and a
// substring and truncates the substring so the entire formatted message will
// fit inside the specified window.
void
CPGPdiskApp::FormatFitStringToWindow(
LPCSTR inString,
LPCSTR subString,
CString *outString,
CWnd *pWnd)
{
CDC memDC;
CDC *pDC;
CSize messageSizeWoSub, substringSize;
CString temp;
HFONT oldFont;
PGPUInt32 widthSubString, widthMessageWoSub, xForSubstring, xWindow;
RECT windowRect;
pgpAssertStrValid(inString);
pgpAssertStrValid(subString);
pgpAssertAddrValid(outString, CString);
pgpAssertAddrValid(pWnd, CWnd);
// Get dimensions of window.
pWnd->GetWindowRect(&windowRect);
pgpAssert(windowRect.right - windowRect.left > 3);
xWindow = windowRect.right - windowRect.left - 3;
pDC = pWnd->GetDC();
memDC.CreateCompatibleDC(pDC);
// MFC bug - CDC::SelectObject returns incorrect pointers/handles so
// don't fool with it.
oldFont = (HFONT) SelectObject(memDC.GetSafeHdc(),
GetStockObject(DEFAULT_GUI_FONT));
// Get dimensions of text without the substring.
try
{
temp.Format(inString, "");
}
catch (CMemoryException *ex)
{
ex->Delete();
}
messageSizeWoSub = memDC.GetOutputTextExtent(temp, temp.GetLength());
memDC.LPtoDP(&messageSizeWoSub);
widthMessageWoSub = messageSizeWoSub.cx;
if (widthMessageWoSub < xWindow)
{
// Get dimensions of substring.
substringSize = memDC.GetOutputTextExtent(subString,
strlen(subString));
memDC.LPtoDP(&substringSize);
widthSubString = substringSize.cx;
xForSubstring = xWindow - widthMessageWoSub;
TruncateDisplayString(subString, &temp, &memDC, xForSubstring);
outString->Format(inString, temp);
}
else
{
TruncateDisplayString(temp, outString, &memDC, xWindow);
}
memDC.SelectObject(oldFont);
memDC.DeleteDC();
pWnd->ReleaseDC(pDC);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -