?? bttncur.c
字號:
return FALSE;
}
//Create a dithered bitmap.
hBmpMono=CreateBitmap(dx-2, dy-2, 1, 1, NULL);
if (NULL==hBmpMono)
return FALSE;
hBmpMonoOrg=(HBITMAP)SelectObject(g_hDCMono, hBmpMono);
//Save the DC state before we munge on it.
iSaveDC=SaveDC(hDC);
/*
* Draw a button sans image. This also fills g_crSys with the
* system colors for us which has space for five colors.
* We don't use the fifth, the frame color, in this function.
*/
DrawBlankButton(hDC, x, y, dx, dy
, (BOOL)(uState & BUTTONGROUP_DOWN), g_crSys);
//Shift coordinates to account for the button's border
x++;
y++;
dx-=2;
dy-=2;
/*
* reflect the pushed-in state, which means just adding 1 to
* the up state.
*/
i=(uState & BUTTONGROUP_DOWN) ? 1 : 0;
xOffsetGlyph=((dx-bmx) >> 1)+i;
yOffsetGlyph=((dy-bmy) >> 1)+i;
//Select given image into the glyph DC before calling MaskCreate
if (NULL!=hBmp)
hBmpSave=(HBITMAP)SelectObject(g_hDCGlyphs, hBmp);
/*
* Draw the face on the button. If we have an up or [mouse]down
* button then we can just draw it as-is. For indeterminate,
* disabled, or down disabled we have to gray the image and
* possibly add a white shadow to it (disabled/down disabled).
*
* Also note that for the intermediate state we first draw the
* normal up state, then proceed to add disabling looking
* highlights.
*/
//Up, mouse down, down, indeterminate
if ((uState & BUTTONGROUP_ACTIVE)
&& !(uState & BUTTONGROUP_BLANK))
{
BOOL fColorsSame=TRUE;
/*
* In here we pay close attention to the system colors.
* Where the source image is black, we paint COLOR_BTNTEXT.
* Where light gray, we paint COLOR_BTNFACE. Where dark gray
* we paint COLOR_BTNSHADOW, and where white we paint
* COLOR_BTNHILIGHT.
*
* The uColors variable contains flags to prevent color
* conversion. To do a little optimization, we just do a
* single BitBlt if we're preserving all colors or if no
* colors are different than the standards, which is by far
* the most common case. Otherwise, cycle through the four
* colors we can convert and do a BitBlt that converts it to
* the system color.
*/
//See what colors are different.
for (i=STDCOLOR_BLACK; i<=STDCOLOR_WHITE; i++)
fColorsSame &= (g_crSys[i]==g_crStandard[i]);
if (PRESERVE_ALL==uColors || fColorsSame)
{
BitBlt(hDC, x+xOffsetGlyph, y+yOffsetGlyph, bmx, bmy
, g_hDCGlyphs, iImage*bmx, 0, SRCCOPY);
}
else
{
/*
* Cycle through hard-coded colors and create a mask that
* has all regions of that color in white and all other
* regions black. Then we select a pattern brush of the
* color to convert to: if we aren't converting the color
* then we use a brush of the standard hard-coded color,
* otherwise we use the actual system color. The
* ROP_DSPDxax means that anything that's 1's in the mask
* get the pattern, anything that's 0 is unchanged in
* the destination.
*
* To prevent too many Blts to the screen, we use an
* intermediate bitmap and DC.
*/
hMemDC=CreateCompatibleDC(hDC);
//Make sure conversion of monochrome to color stays B&W
SetTextColor(hMemDC, 0L); //mono 0's->0
SetBkColor(hMemDC, (COLORREF)0x00FFFFFF); //mono 1's->1
hBmpT=CreateCompatibleBitmap(hDC, bmx, bmy);
SelectObject(hMemDC, hBmpT);
//Copy the unmodified bitmap to the temporary bitmap
BitBlt(hMemDC, 0, 0, bmx, bmy, g_hDCGlyphs, iImage*bmx
, 0, SRCCOPY);
for (i=STDCOLOR_BLACK; i<=STDCOLOR_WHITE; i++)
{
//Convert pixels of convert color to 1's in the mask
SetBkColor(g_hDCGlyphs, g_crStandard[i]);
BitBlt(g_hDCMono, 0, 0, bmx, bmy, g_hDCGlyphs, iImage*bmx
, 0, SRCCOPY);
//Preserve or modify the color depending on the flag.
hBR=CreateSolidBrush((uColors & (1 << i))
? g_crStandard[i] : g_crSys[i]);
if (NULL!=hBR)
{
hObj=SelectObject(hMemDC, hBR);
if (NULL!=hObj)
{
BitBlt(hMemDC, 0, 0, dx-1, dy-1, g_hDCMono, 0
, 0, ROP_DSPDxax);
SelectObject(hMemDC, hObj);
}
DeleteObject(hBR);
}
}
//Now put the final version on the display and clean up
BitBlt(hDC, x+xOffsetGlyph, y+yOffsetGlyph, dx-1, dy-1
, hMemDC, 0, 0, SRCCOPY);
DeleteDC(hMemDC);
DeleteObject(hBmpT);
}
}
//Disabled and indeterminate states (unless we're blank)
if ((uState & BUTTONGROUP_DISABLED
|| ATTRIBUTEBUTTON_INDETERMINATE==uState)
&& !(uState & BUTTONGROUP_BLANK))
{
//Grayed state (up or down, no difference)
MaskCreate(iImage, dx, dy, bmx, bmy, xOffsetGlyph
, yOffsetGlyph, 0);
//Make sure conversion of monochrome to color stays B&W
SetTextColor(hDC, 0L); //0's in mono -> 0
SetBkColor(hDC, (COLORREF)0x00FFFFFF); //1's in mono -> 1
//If we're disabled, up or down, draw the highlighted shadow.
if (uState & BUTTONGROUP_DISABLED)
{
hBR=CreateSolidBrush(g_crSys[SYSCOLOR_HILIGHT]);
if (NULL!=hBR)
{
hObj=SelectObject(hDC, hBR);
if (NULL!=hObj)
{
//Draw hilight color where we have 0's in mask
BitBlt(hDC, x+1, y+1, dx-2, dy-2, g_hDCMono, 0, 0
, ROP_PSDPxax);
SelectObject(hDC, hObj);
}
DeleteObject(hBR);
}
}
//Draw the gray image.
hBR=CreateSolidBrush(g_crSys[SYSCOLOR_SHADOW]);
if (NULL!=hBR)
{
hObj=SelectObject(hDC, hBR);
if (NULL!=hObj)
{
//Draw the shadow color where we have 0's in the mask
BitBlt(hDC, x, y, dx-2, dy-2, g_hDCMono, 0, 0
, ROP_PSDPxax);
SelectObject(hDC, hObj);
}
DeleteObject(hBR);
}
}
//If the button is selected do the dither brush avoiding glyph
if (uState & BUTTONGROUP_LIGHTFACE)
{
HBRUSH hBRDither;
/*
* Get the dither brush. This function will recreate it if
* necessary or return the global one if the colors already
* match.
*/
hBRDither=HBrushDitherCreate(g_crSys[SYSCOLOR_FACE]
, g_crSys[SYSCOLOR_HILIGHT]);
hObj=SelectObject(hDC, hBRDither);
if (NULL!=hObj)
{
/*
* The mask we create now determines where the dithering
* ends up. In the down disabled state, we have to
* preserve the highlighted shadow, so the mask we create
* must have two masks of the original glyph, one of them
* offset by one pixel in both x & y. For the
* indeterminate state, we have to mask all highlighted
* areas. The state passed to MaskCreate matters here
* (we've used zero before).
*/
MaskCreate(iImage, dx, dy, bmx, bmy
, xOffsetGlyph-1, yOffsetGlyph-1, uState);
//Convert monochrome masks to B&W color bitmap in BitBlt.
SetTextColor(hDC, 0L);
SetBkColor(hDC, (COLORREF)0x00FFFFFF);
/*
* Only draw the dither brush where the mask is 1's. For
* the indeterminate state we have to not overdraw the
* shadow highlight so we use dx-3, dy-3 instead of dx-1
* and dy-1. We do this whether or not we're blank.
*/
i=(ATTRIBUTEBUTTON_INDETERMINATE==uState
|| BLANKBUTTON_INDETERMINATE==uState) ? 3 : 1;
BitBlt(hDC, x+1, y+1, dx-i, dy-i, g_hDCMono, 0, 0
, ROP_DSPDxax);
SelectObject(hDC, hObj);
}
//DO NOT delete hBRDither! It's a shared global.
}
//Cleanup g_hDCGlyphs: Must do AFTER calling MaskCreate
if (NULL!=hBmpSave)
SelectObject(g_hDCGlyphs, hBmpSave);
SelectObject(g_hDCMono, hBmpMonoOrg);
DeleteObject(hBmpMono);
//Restore everything in the DC.
RestoreDC(hDC, iSaveDC);
return TRUE;
}
/*
* DrawBlankButton
*
* Purpose:
* Draws a button with no face using the current system colors in
* either an up or down state.
*
* Parameters:
* hDC HDC on which to draw
* x, y int coordinates where we start drawing
* dx,dy int size of the button
* fDown BOOL indicating the up or down state of the
* button
* pcr COLORREF * to five colors in which we store text,
* shadow, face, highlight, and frame colors. This
* is a matter of convenience for the caller, since
* we have to load these colors anyway we might as
* well send them back.
*
* Return Value:
* None
*/
void DrawBlankButton(HDC hDC, int x, int y, int dx, int dy
, BOOL fDown, COLORREF *pcr)
{
//Get the current system colors for buttons.
pcr[0]=GetSysColor(COLOR_BTNTEXT);
pcr[1]=GetSysColor(COLOR_BTNSHADOW);
pcr[2]=GetSysColor(COLOR_BTNFACE);
pcr[3]=GetSysColor(COLOR_BTNHIGHLIGHT);
pcr[4]=GetSysColor(COLOR_WINDOWFRAME);
//Draw the border around the button.
PatB(hDC, x+1, y, dx-2, 1, pcr[4]);
PatB(hDC, x+1, y+dy-1, dx-2, 1, pcr[4]);
PatB(hDC, x, y+1, 1, dy-2, pcr[4]);
PatB(hDC, x+dx-1, y+1, 1, dy-2, pcr[4]);
//Shift coordinates to account for the border we just drew
x++;
y++;
dx-=2;
dy-=2;
//Paint the interior grey as a default.
PatB(hDC, x, y, dx, dy, pcr[2]);
/*
* Draw shadows and highlights. The DOWN grouping that contains
* down, mouse down, and down disabled are drawn depressed. Up,
* indeterminate, and disabled are drawn up.
*/
if (fDown)
{
PatB(hDC, x, y, 1, dy, pcr[1]);
PatB(hDC, x, y, dx, 1, pcr[1]);
}
else
{
//Normal button look.
PatB(hDC, x, y, 1, dy-1, pcr[3]);
PatB(hDC, x, y, dx-1, 1, pcr[3]);
PatB(hDC, x+dx-1, y, 1, dy, pcr[1]);
PatB(hDC, x, y+dy-1, dx, 1, pcr[1]);
PatB(hDC, x+1+dx-3, y+1, 1, dy-2, pcr[1]);
PatB(hDC, x+1, y+dy-2, dx-2, 1, pcr[1]);
}
return;
}
/*
* PatB
* Internal
*
* Purpose:
* A more convenient PatBlt operation for drawing button borders and
* highlights.
*
* Parameters:
* hDC HDC on which to paint.
* x, y int coordinates at which to paint.
* dx, dy int dimensions of rectangle to paint.
* rgb COLORREF to use as the background color.
*
* Return Value:
* None
*/
void PatB(HDC hDC, int x, int y, int dx, int dy, COLORREF rgb)
{
RECT rc;
SetBkColor(hDC, rgb);
SetRect(&rc, x, y, x+dx, y+dy);
ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
}
/*
* MaskCreate
* Internal
*
* Purpose:
* Creates a monochrome mask bitmap of the given image at the given
* offset in the global g_hDCMono. Anywhere in the image that you
* have the light gray (STDCOLOR_LTGRAY) or the white highlight
* (STDCOLOR_WHITE) you get get 1's. All other pixels are 0's
*
* Parameters:
* iImage UINT index of the image for which to create a
* mask.
* dx, dy int dimensions of the button.
* bmx, bmy int dimensions of the bitmap to use.
* xOffset int offset for x inside g_hDCMono where we paint.
* yOffset int offset for y inside g_hDCMono where we paint.
* uState UINT state of the image. Special cases are made
* for ATTRIBUTEBUTTON_DOWNDISABLED and
* ATTRIBUTEBUTTON_INDETERMINATE. In any case
* where you do not want a special case, pass zero
* here, regardless of the true button state.
*
* Return Value:
* None
*/
void MaskCreate(UINT iImage, int dx, int dy, int bmx, int bmy
,int xOffset, int yOffset, UINT uState)
{
//Initalize whole area with zeros
PatBlt(g_hDCMono, 0, 0, dx, dy, WHITENESS);
if (uState & BUTTONGROUP_BLANK)
return;
//Convert face colored pixels to 1's. all others to black.
SetBkColor(g_hDCGlyphs, g_crStandard[STDCOLOR_LTGRAY]);
BitBlt(g_hDCMono, xOffset, yOffset, bmx, bmy, g_hDCGlyphs
, iImage*bmx, 0, SRCCOPY);
//Indeterminate state, don't turn highlight's to 1's--leave black
if (ATTRIBUTEBUTTON_INDETERMINATE!=uState)
{
//Convert highlight colored pixels to 1's, OR with previous.
SetBkColor(g_hDCGlyphs, g_crStandard[STDCOLOR_WHITE]);
BitBlt(g_hDCMono, xOffset, yOffset, bmx, bmy, g_hDCGlyphs
, iImage*bmx, 0, SRCPAINT);
}
/*
* For the down disabled state, AND this same mask with itself at
* an offset of 1, which accounts for the highlight shadow.
*/
if (ATTRIBUTEBUTTON_DOWNDISABLED==uState)
BitBlt(g_hDCMono, 1, 1, dx-1, dy-1, g_hDCMono, 0, 0, SRCAND);
return;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -