📄 drawex.cpp
字号:
//SetWindowPos(pWndFrom->GetSafeHwnd(), NULL, 0,0,0,0,SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOZORDER|SWP_HIDEWINDOW);
return 1;
}
bool CFadeWindow::IsFastEnough()
{
bool bRet = false;
if ( intel_wincpuidsupport() ) // Determine whether CPUID
{
if (intel_check_IDProc()>5) // Pentium Pro or later;
bRet = true;
}else // else not even pentium
{
// Check if is K6
if (amd_isK6orLater())
bRet = true;
}
/*
DWORD data;
DWORD dataSize;
LONG result;
HKEY hKey;
result = ::RegOpenKeyEx (HKEY_LOCAL_MACHINE,
"Hardware\\Description\\System\\CentralProcessor\\0", 0, KEY_QUERY_VALUE, &hKey);
if (result == ERROR_SUCCESS) {
result = ::RegQueryValueEx (hKey, _T("~MHz"), NULL, NULL,
(LPBYTE)&data, &dataSize);
if (data < 233)
bRet = false;
RegCloseKey (hKey);
}else
bRet = false;*/
CWindowDC dc(AfxGetMainWnd());
if( bRet && dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
bRet = false;
return bRet;
}
void FillBmpRect(CDC* pDC, const CRect& rect, CBitmap* pBmp, int nBmpX, int nBmpY)
{
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap* pOldBitmap = (CBitmap*)memDC.SelectObject (pBmp);
int nWidth = rect.Width();
int nHeight = rect.Height();
int nX = rect.left;
int nY = rect.top;
int cx = (nWidth / nBmpX) + 1;
int cy = (nHeight / nBmpY) + 1;
int i, j, x1, y1;
for (i=0; i<cx; i++)
{
for (j=0; j<cy; j++)
{
x1 = i * nBmpX;
y1 = j * nBmpY;
pDC->BitBlt (x1+nX, y1+nY, ((nWidth-x1)<nBmpX)?nWidth-x1:nBmpX,
((nHeight-y1)<nBmpY)?nHeight-y1:nBmpY, &memDC,
0, 0, SRCCOPY);
}
}
memDC.SelectObject (pOldBitmap);
}
void DrawPercentBar(CDC* pDC, const CRect& rect, const double dPercent,
const CString& strText, CFont* pFont)
{
pDC->FillSolidRect(rect, RGB(255,255,255));
CRect rc(rect);
rc.right = rc.left + (int)(double(rect.Width())*dPercent);
COLORREF color;
if (dPercent<0.65)
color = RGB(128,255,128);
else if (dPercent<0.75)
color = RGB(0,128,255);
else if (dPercent<0.90)
color = RGB(255,255,128);
else
color = RGB(255,128,128);
/*if (pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
{
color = RGB(192, 192, 192);
pDC->FillSolidRect(rc, color);
}
else*/
FillGradient(pDC, rc, RGB(255,255,255), color);
CFont* pOldFont;
if (pFont)
pOldFont = pDC->SelectObject(pFont);
COLORREF oldTextColor = pDC->SetTextColor(RGB(0,0,0));
pDC->SetBkMode(TRANSPARENT);
//pDC->SetBkColor(RGB(0,0,0));
CSize szText = pDC->GetTextExtent(strText);
pDC->TextOut(rect.left+(rect.Width()-szText.cx)/2,
rect.top+(rect.Height()-szText.cy)/2, strText);
//pDC->DrawText(strText, rectText, DT_CENTER|DT_VCENTER);
pDC->Draw3dRect(rect, RGB(96,96,96),
RGB(255, 255, 255));
if (pFont)
pDC->SelectObject(pOldFont);
pDC->SetTextColor(oldTextColor);
}
void TransparentBlt (HDC hdc, HBITMAP hBitmap, int xStart,
int yStart, COLORREF cTransparentColor)
{
BITMAP bm;
COLORREF cColor;
HBITMAP bmAndBack, bmAndObject, bmAndMem, bmSave;
HBITMAP bmBackOld, bmObjectOld, bmMemOld, bmSaveOld;
HDC hdcMem, hdcBack, hdcObject, hdcTemp, hdcSave;
POINT ptSize;
hdcTemp = CreateCompatibleDC(hdc);
SelectObject(hdcTemp, hBitmap); // Select the bitmap
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
ptSize.x = bm.bmWidth; // Get width of bitmap
ptSize.y = bm.bmHeight; // Get height of bitmap
DPtoLP(hdcTemp, &ptSize, 1); // Convert from device
// to logical points
// Create some DCs to hold temporary data.
hdcBack = CreateCompatibleDC(hdc);
hdcObject = CreateCompatibleDC(hdc);
hdcMem = CreateCompatibleDC(hdc);
hdcSave = CreateCompatibleDC(hdc);
// Create a bitmap for each DC.
// Monochrome DC
bmAndBack = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
// Monochrome DC
bmAndObject = CreateBitmap(ptSize.x, ptSize.y, 1, 1, NULL);
bmAndMem = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
bmSave = CreateCompatibleBitmap(hdc, ptSize.x, ptSize.y);
// Each DC must select a bitmap object to store pixel data.
bmBackOld = (HBITMAP) SelectObject(hdcBack, bmAndBack);
bmObjectOld = (HBITMAP) SelectObject(hdcObject, bmAndObject);
bmMemOld = (HBITMAP) SelectObject(hdcMem, bmAndMem);
bmSaveOld = (HBITMAP) SelectObject(hdcSave, bmSave);
// Set proper mapping mode.
SetMapMode(hdcTemp, GetMapMode(hdc));
// Save the bitmap sent here, because it will be overwritten.
BitBlt(hdcSave, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCCOPY);
// Set the background color of the source DC to the color.
// contained in the parts of the bitmap that should be transparent
cColor = SetBkColor(hdcTemp, cTransparentColor);
// Create the object mask for the bitmap by performing a BitBlt
// from the source bitmap to a monochrome bitmap.
BitBlt(hdcObject, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0,
SRCCOPY);
// Set the background color of the source DC back to the original
// color.
SetBkColor(hdcTemp, cColor);
// Create the inverse of the object mask.
BitBlt(hdcBack, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0,
NOTSRCCOPY);
// Copy the background of the main DC to the destination.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdc, xStart, yStart,
SRCCOPY);
// Mask out the places where the bitmap will be placed.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcObject, 0, 0, SRCAND);
// Mask out the transparent colored pixels on the bitmap.
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcBack, 0, 0, SRCAND);
// XOR the bitmap with the background on the destination DC.
BitBlt(hdcMem, 0, 0, ptSize.x, ptSize.y, hdcTemp, 0, 0, SRCPAINT);
// Copy the destination to the screen.
BitBlt(hdc, xStart, yStart, ptSize.x, ptSize.y, hdcMem, 0, 0,
SRCCOPY);
// Place the original bitmap back into the bitmap sent here.
BitBlt(hdcTemp, 0, 0, ptSize.x, ptSize.y, hdcSave, 0, 0, SRCCOPY);
// Delete the memory bitmaps.
DeleteObject(SelectObject(hdcBack, bmBackOld));
DeleteObject(SelectObject(hdcObject, bmObjectOld));
DeleteObject(SelectObject(hdcMem, bmMemOld));
DeleteObject(SelectObject(hdcSave, bmSaveOld));
// Delete the memory DCs.
DeleteDC(hdcMem);
DeleteDC(hdcBack);
DeleteDC(hdcObject);
DeleteDC(hdcSave);
DeleteDC(hdcTemp);
}
void FillGradient(CDC *pDC, const CRect& rectClient, COLORREF m_clrStart, COLORREF m_clrEnd)
{
RECT rectFill; // Rectangle for filling band
float fStep; // How wide is each band?
CBrush brush; // Brush to fill in the bar
// First find out the largest color distance between the start and end colors. This distance
// will determine how many steps we use to carve up the client region and the size of each
// gradient rect.
int r, g, b; // First distance, then starting value
float rStep, gStep, bStep; // Step size for each color
// Get the color differences
r = (GetRValue(m_clrEnd) - GetRValue(m_clrStart));
g = (GetGValue(m_clrEnd) - GetGValue(m_clrStart));
b = (GetBValue(m_clrEnd) - GetBValue(m_clrStart));
// Make the number of steps equal to the greatest distance
//int nSteps = max(abs(r), max(abs(g), abs(b)));
//TRACE("%d\n", nSteps);
int nSteps = 10;
// Determine how large each band should be in order to cover the
// client with nSteps bands (one for every color intensity level)
fStep = (float)rectClient.Width() / (float)nSteps;
// Calculate the step size for each color
rStep = r/(float)nSteps;
gStep = g/(float)nSteps;
bStep = b/(float)nSteps;
// Reset the colors to the starting position
r = GetRValue(m_clrStart);
g = GetGValue(m_clrStart);
b = GetBValue(m_clrStart);
// Start filling bands
for (int iOnBand = 0; iOnBand < nSteps; iOnBand++)
{
::SetRect(&rectFill,
rectClient.left + (int)(iOnBand * fStep), // Upper left X
rectClient.top, // Upper left Y
rectClient.left + (int)((iOnBand+1) * fStep), // Lower right X
rectClient.bottom); // Lower right Y
if (rectFill.right > rectClient.right)
rectFill.right = rectClient.right;
// CDC::FillSolidRect is faster, but it does not handle 8-bit color depth
VERIFY(brush.CreateSolidBrush(RGB(r+rStep*iOnBand, g + gStep*iOnBand, b + bStep *iOnBand)));
pDC->FillRect(&rectFill,&brush);
VERIFY(brush.DeleteObject());
//pDC->FillSolidRect(&rectFill, RGB(r+rStep*iOnBand, g + gStep*iOnBand, b + bStep *iOnBand));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -