?? bttncur.c
字號:
/*
* BTTNCUR.C
* Buttons & Cursors
*
* Public functions to generate different states of toolbar buttons
* from a single bitmap. States are normal, pressed, checked, and
* disabled.
*
* Copyright (c)1992-1995 Microsoft Corporation, All Rights Reserved
*
* Kraig Brockschmidt, Microsoft
* Internet : kraigb@microsoft.com
* Compuserve: >INTERNET:kraigb@microsoft.com
*/
#include "inoledll.h"
//Cache GDI objects to speed drawing.
HDC g_hDCGlyphs = NULL;
HDC g_hDCMono = NULL;
HBRUSH g_hBrushDither = NULL;
//Standard images to use in case caller doesn't provide them
HBITMAP g_rghBmpStandardImages[3];
//Standard button colors.
const COLORREF g_crStandard[4]={ RGB(0, 0, 0) //STDCOLOR_BLACK
, RGB(128, 128, 128) //STDCOLOR_DKGRAY
, RGB(192, 192, 192) //STDCOLOR_LTGRAY
, RGB(255, 255, 255)}; //STDCOLOR_WHITE
COLORREF g_crSys[5];
/*
* Mapping from image identifier to button type (command/attribute).
* Version 1.00 of this DLL has no attribute images defined, so
* the code will only support three states for each command
* button. Any state is, however, valid for an application
* defined image.
*/
UINT mpButtonType[TOOLIMAGE_MAX-TOOLIMAGE_MIN+1]=
{
BUTTONTYPE_COMMAND, BUTTONTYPE_COMMAND, BUTTONTYPE_COMMAND,
BUTTONTYPE_COMMAND, BUTTONTYPE_COMMAND, BUTTONTYPE_COMMAND,
BUTTONTYPE_COMMAND, BUTTONTYPE_COMMAND, BUTTONTYPE_COMMAND
};
/*
* UIToolConfigureForDisplay
* Public API
*
* Purpose:
* Initializes the library to scale button images for the display
* type. Without calling this function the library defaults to 96
* DPI (VGA). By calling this function an application acknowledges
* that it must use the data returned from this function to
* configure itself for the display.
*
* Parameters:
* lpDD LPTOOLDISPLAYDATA to fill with the display-
* sensitive size values.
*
* Return Value:
* BOOL TRUE if the sizes were obtained, FALSE otherwise.
*/
BOOL WINAPI UIToolConfigureForDisplay(LPTOOLDISPLAYDATA lpDD)
{
int cy;
HDC hDC;
if (NULL==lpDD || IsBadWritePtr(lpDD, sizeof(TOOLDISPLAYDATA)))
return FALSE;
/*
* Determine the aspect ratio of the display we're currently
* running on and calculate the necessary information.
*
* By retrieving the logical Y extent of the display driver, you
* only have limited possibilities:
* LOGPIXELSY Display
* ----------------------------------------
* 48 CGA (unsupported)
* 72 EGA
* 96 VGA
* 120 8514/a (i.e. HiRes VGA)
*/
hDC=GetDC(NULL);
if (NULL==hDC)
return FALSE;
cy=GetDeviceCaps(hDC, LOGPIXELSY);
ReleaseDC(NULL, hDC);
/*
* Instead of single comparisons, check ranges instead, so in
* case we get something funky, we'll act reasonable.
*/
if (72 >=cy)
{
lpDD->uDPI =72;
lpDD->cyBar =CYBUTTONBAR72;
lpDD->cxButton =TOOLBUTTON_STD72WIDTH;
lpDD->cyButton =TOOLBUTTON_STD72HEIGHT;
lpDD->cxImage =TOOLBUTTON_STD72IMAGEWIDTH;
lpDD->cyImage =TOOLBUTTON_STD72IMAGEHEIGHT;
lpDD->uIDImages=IDB_STANDARDIMAGES72;
}
else
{
if (72 < cy && 120 > cy)
{
lpDD->uDPI =96;
lpDD->cyBar =CYBUTTONBAR96;
lpDD->cxButton =TOOLBUTTON_STD96WIDTH;
lpDD->cyButton =TOOLBUTTON_STD96HEIGHT;
lpDD->cxImage =TOOLBUTTON_STD96IMAGEWIDTH;
lpDD->cyImage =TOOLBUTTON_STD96IMAGEHEIGHT;
lpDD->uIDImages=IDB_STANDARDIMAGES96;
}
else
{
lpDD->uDPI =120;
lpDD->cyBar =CYBUTTONBAR120;
lpDD->cxButton =TOOLBUTTON_STD120WIDTH;
lpDD->cyButton =TOOLBUTTON_STD120HEIGHT;
lpDD->cxImage =TOOLBUTTON_STD120IMAGEWIDTH;
lpDD->cyImage =TOOLBUTTON_STD120IMAGEHEIGHT;
lpDD->uIDImages=IDB_STANDARDIMAGES120;
}
}
return TRUE;
}
/*
* ToolButtonInit
* Internal
*
* Purpose:
* Initializes GDI objects for drawing images through
* UIToolButtonDraw. If the function fails, the function has
* already performed proper cleanup.
*
* Parameters:
* hInst HINSTANCE of the DLL
*
* Return Value:
* BOOL TRUE if initialization succeeded. FALSE
* otherwise.
*/
BOOL ToolButtonInit(HINSTANCE hInst)
{
UINT i;
//DC for BitBltting the image (the glyph)
g_hDCGlyphs=CreateCompatibleDC(NULL);
//Create a monochrome DC and a brush for doing pattern dithering.
g_hDCMono=CreateCompatibleDC(NULL);
g_hBrushDither=HBrushDitherCreate(GetSysColor(COLOR_BTNFACE)
, GetSysColor(COLOR_BTNHIGHLIGHT));
for (i=0; i < 3; i++)
{
g_rghBmpStandardImages[i]=LoadBitmap(hInst
, MAKEINTRESOURCE(IDB_STANDARDIMAGESMIN+i));
}
if (NULL==g_hDCGlyphs || NULL==g_hDCMono
|| NULL==g_hBrushDither || NULL==g_rghBmpStandardImages[0])
{
//On failure, cleanup whatever might have been allocated.
ToolButtonFree();
return FALSE;
}
return TRUE;
}
/*
* ToolButtonFree
* Internal
*
* Purpose:
* Free all GDI allocations made during initialization.
*
* Parameters:
* None
*
* Return Value:
* None
*/
void ToolButtonFree(void)
{
UINT i;
if (NULL!=g_hDCMono)
DeleteDC(g_hDCMono);
g_hDCMono=NULL;
if (NULL!=g_hDCGlyphs)
DeleteDC(g_hDCGlyphs);
g_hDCGlyphs=NULL;
if (NULL!=g_hBrushDither)
DeleteObject(g_hBrushDither);
g_hBrushDither=NULL;
for (i=0; i < 3; i++)
{
if (NULL!=g_rghBmpStandardImages[i])
DeleteObject(g_rghBmpStandardImages[i]);
g_rghBmpStandardImages[i]=NULL;
}
return;
}
/*
* HBrushDitherCreate
* Internal
*
* Purpose:
* Creates and returns a handle to a pattern brush created from
* an 8*8 monochrome pattern bitmap. We use the button face and
* highlight colors to indicate the resulting colors of a PatBlt
* using this brush.
*
* Parameters:
* rgbFace COLORREF of the button face color.
* rgbHilight COLORREF of the button highlight color.
*
* Return Value:
* HBITMAP Handle to the dither bitmap.
*/
HBRUSH HBrushDitherCreate(COLORREF rgbFace, COLORREF rgbHilight)
{
struct //BITMAPINFO with 16 colors
{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[16];
} bmi;
HBRUSH hBrush=NULL;
DWORD patGray[8];
HDC hDC;
HBITMAP hBmp;
static COLORREF rgbFaceOld =0xFFFFFFFF; //Initially impossible
static COLORREF rgbHilightOld=0xFFFFFFFF; //Initially impossible
/*
* If the colors haven't changed from last time, just return the
* existing brush.
*/
if (rgbFace==rgbFaceOld && rgbHilight==rgbHilightOld)
return g_hBrushDither;
rgbFaceOld=rgbFace;
rgbHilightOld=rgbHilight;
/*
* We're going to create an 8*8 brush for PatBlt using the
* button face color and button highlight color. We use this
* brush to affect the pressed state and the disabled state.
*/
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = 8;
bmi.bmiHeader.biHeight = 8;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 1;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = 0;
bmi.bmiHeader.biXPelsPerMeter= 0;
bmi.bmiHeader.biYPelsPerMeter= 0;
bmi.bmiHeader.biClrUsed = 0;
bmi.bmiHeader.biClrImportant = 0;
bmi.bmiColors[0].rgbBlue = GetBValue(rgbFace);
bmi.bmiColors[0].rgbGreen = GetGValue(rgbFace);
bmi.bmiColors[0].rgbRed = GetRValue(rgbFace);
bmi.bmiColors[0].rgbReserved = 0;
bmi.bmiColors[1].rgbBlue = GetBValue(rgbHilight);
bmi.bmiColors[1].rgbGreen = GetGValue(rgbHilight);
bmi.bmiColors[1].rgbRed = GetRValue(rgbHilight);
bmi.bmiColors[1].rgbReserved = 0;
//Create the byte array for CreateDIBitmap.
patGray[6]=patGray[4]=patGray[2]=patGray[0]=0x5555AAAAL;
patGray[7]=patGray[5]=patGray[3]=patGray[1]=0xAAAA5555L;
//Create the bitmap
hDC=GetDC(NULL);
hBmp=CreateDIBitmap(hDC, &bmi.bmiHeader, CBM_INIT, patGray
, (LPBITMAPINFO)&bmi, DIB_RGB_COLORS);
ReleaseDC(NULL, hDC);
//Create the brush from the bitmap
if (NULL!=hBmp)
{
hBrush=CreatePatternBrush(hBmp);
DeleteObject(hBmp);
}
/*
* If we could recreate a brush, clean up and make it the current
* pattern. Otherwise the best we can do it return the old one,
* which will be colored wrong, but at least it works.
*/
if (NULL!=hBrush)
{
if (NULL!=g_hBrushDither)
DeleteObject(g_hBrushDither);
g_hBrushDither=hBrush;
}
return g_hBrushDither;
}
/*
* UIToolButtonDraw
* Public API
*
* Purpose:
* Draws the complete image of a toolbar-style button with a given
* image in the center and in a specific state. The button is drawn
* on a specified hDC at a given location, so this function is
* useful on standard owner-draw buttons as well as on toolbar
* controls that have only one window but show images of multiple
* buttons.
*
* Parameters:
* hDC HDC on which to draw.
* x, y int coordinates at which to draw.
* dx, dy int dimensions of the *button*, not the image.
* hBmp HBITMAP from which to draw the image.
* bmx, bmy int dimensions of each bitmap in hBmp. If hBmp
* is NULL then these are forced to the standard
* sizes.
* iImage int index to the image to draw in the button
* uStateIn UINT containing the state index for the button
* and the color control bits.
* pTDD LPTOOLDISPLAYDATA containing display
* configuration. Can be NULL if hBmp is non-NULL.
*
* Return Value:
* BOOL TRUE if drawing succeeded, FALSE otherwise
* meaning that hDC is NULL or hBmp is NULL and
* iImage is not a valid index for a standard image.
*/
BOOL WINAPI UIToolButtonDraw(HDC hDC, int x, int y, int dx, int dy
, HBITMAP hBmp, int bmx, int bmy, int iImage, UINT uStateIn
, LPTOOLDISPLAYDATA pTDD)
{
UINT uState=(UINT)LOBYTE((WORD)uStateIn);
UINT uColors=(UINT)HIBYTE((WORD)uStateIn
& PRESERVE_ALL);
int xOffsetGlyph, yOffsetGlyph;
int i, iSaveDC;
HDC hMemDC;
HGDIOBJ hObj;
HBRUSH hBR;
HBITMAP hBmpT;
HBITMAP hBmpMono;
HBITMAP hBmpMonoOrg;
HBITMAP hBmpSave=NULL;
TOOLDISPLAYDATA tdd;
if (NULL==hDC)
return FALSE;
if (NULL==pTDD)
{
pTDD=&tdd;
UIToolConfigureForDisplay(pTDD);
}
/*
* If we're given no image bitmap, then use the standard and
* validate the image index. We also enforce the standard
* bitmap size and the size of the button (as requested by
* User Interface designers).
*/
if (NULL==hBmp && !(uState & BUTTONGROUP_BLANK))
{
i=pTDD->uIDImages-IDB_STANDARDIMAGESMIN;
hBmp=g_rghBmpStandardImages[i];
bmx=pTDD->cxImage; //Force bitmap dimensions
bmy=pTDD->cyImage;
dx=pTDD->cxButton; //Force button dimensions
dy=pTDD->cyButton;
if (iImage > TOOLIMAGE_MAX)
return FALSE;
/*
* If we are using a standard command button, verify that
* the state does not contain the LIGHTFACE group which
* only applies to attribute buttons.
*/
if (BUTTONTYPE_COMMAND==mpButtonType[iImage]
&& (uState & BUTTONGROUP_LIGHTFACE))
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -