📄 xshadebutton.cpp
字号:
hue = lHSLColor.rgbRed; /* get H, S, and L out of DWORD */
sat = lHSLColor.rgbGreen;
lum = lHSLColor.rgbBlue;
if (sat == 0) { /* achromatic case */
R=G=B=(lum*RGBMAX)/HSLMAX;
} else { /* chromatic case */
/* set up magic numbers */
if (lum <= (HSLMAX/2))
Magic2 = (lum*(HSLMAX + sat) + (HSLMAX/2))/HSLMAX;
else
Magic2 = lum + sat - ((lum*sat) + (HSLMAX/2))/HSLMAX;
Magic1 = 2*lum-Magic2;
/* get RGB, change units from HSLMAX to RGBMAX */
R = (HueToRGB(Magic1,Magic2,(WORD)(hue+(HSLMAX/3)))*RGBMAX+(HSLMAX/2))/HSLMAX;
G = (HueToRGB(Magic1,Magic2,hue)*RGBMAX + (HSLMAX/2)) / HSLMAX;
B = (HueToRGB(Magic1,Magic2,(WORD)(hue-(HSLMAX/3)))*RGBMAX+(HSLMAX/2))/HSLMAX;
}
RGBQUAD rgb={B,G,R,0};
return rgb;
}
/////////////////////////////////////////////////////////////////////
RGBQUAD CxDib::RGB2RGBQUAD(COLORREF cr)
{
RGBQUAD c;
c.rgbRed = GetRValue(cr); /* get R, G, and B out of DWORD */
c.rgbGreen = GetGValue(cr);
c.rgbBlue = GetBValue(cr);
c.rgbReserved=0;
return c;
}
/////////////////////////////////////////////////////////////////////
COLORREF CxDib::RGBQUAD2RGB (RGBQUAD c)
{
return RGB(c.rgbRed,c.rgbGreen,c.rgbBlue);
}
/////////////////////////////////////////////////////////////////////
void CxDib::SetGrayPalette()
{
if ((hDib==NULL)||(m_nColors==0)) return;
RGBQUAD pal[256];
RGBQUAD* ppal;
BYTE* iDst;
int ni;
ppal=(RGBQUAD*)&pal[0];
iDst = (BYTE*)(hDib) + sizeof(BITMAPINFOHEADER);
for (ni=0;ni<m_nColors;ni++){ pal[ni]=RGB2RGBQUAD(RGB(ni,ni,ni));}
pal[0]=RGB2RGBQUAD(RGB(0,0,0));
pal[m_nColors-1]=RGB2RGBQUAD(RGB(255,255,255));
memcpy(iDst,ppal,GetPaletteSize());
}
/////////////////////////////////////////////////////////////////////
void CxDib::BlendPalette(COLORREF cr,long perc)
{
if ((hDib==NULL)||(m_nColors==0)) return;
BYTE* iDst = (BYTE*)(hDib) + sizeof(BITMAPINFOHEADER);
long i,r,g,b;
RGBQUAD* pPal=(RGBQUAD*)iDst;
r = GetRValue(cr);
g = GetGValue(cr);
b = GetBValue(cr);
if (perc>100) perc=100;
for(i=0;i<m_nColors;i++){
pPal[i].rgbBlue=(BYTE)((pPal[i].rgbBlue*(100-perc)+b*perc)/100);
pPal[i].rgbGreen =(BYTE)((pPal[i].rgbGreen*(100-perc)+g*perc)/100);
pPal[i].rgbRed =(BYTE)((pPal[i].rgbRed*(100-perc)+r*perc)/100);
}
}
/////////////////////////////////////////////////////////////////////
long CxDib::WriteBMP(LPSTR bmpFileName)
{
if ((*bmpFileName=='\0')||(hDib==0)) return 0;
BITMAPFILEHEADER hdr;
HANDLE hFile;
DWORD nByteWrite;
hFile=CreateFile( // open if exist ini file
bmpFileName, // pointer to name of the file
GENERIC_WRITE, // access mode
0, // share mode
NULL, // pointer to security descriptor
TRUNCATE_EXISTING, // how to create
FILE_ATTRIBUTE_NORMAL, // file attributes
NULL // handle to file with attributes to copy
);
if (hFile==INVALID_HANDLE_VALUE) return 0; //check if file exist
/* Fill in the fields of the file header */
hdr.bfType = BFT_BITMAP;
hdr.bfSize = GetSize() + sizeof(BITMAPFILEHEADER);
hdr.bfReserved1 = hdr.bfReserved2 = 0;
hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER)+
m_bi.biSize + GetPaletteSize();
// Write the file header
WriteFile( // write ini (sync mode <-> no overlapped)
hFile, // handle of file to write
(LPSTR) &hdr, // address of buffer that contains data
sizeof(BITMAPFILEHEADER), // number of bytes to write
&nByteWrite, // address of number of bytes written
NULL // address of structure for data
);
// Write the DIB header and the bits
WriteFile( // write ini (sync mode <-> no overlapped)
hFile, // handle of file to write
(LPSTR) hDib, // address of buffer that contains data
GetSize(), // number of bytes to write
&nByteWrite, // address of number of bytes written
NULL // address of structure for data
);
CloseHandle(hFile); //free file handle
return 1;
}
/////////////////////////////////////////////////////////////////////
long CxDib::GetSize()
{
return m_bi.biSize + m_bi.biSizeImage + GetPaletteSize();
}
/////////////////////////////////////////////////////////////////////
BOOL CxDib::IsValid()
{
return (hDib!=NULL);
}
/////////////////////////////////////////////////////////////////////
void CxDib::Clone(CxDib *src)
{
Create(src->GetWidth(),src->GetHeight(),src->GetBitCount());
if (hDib) memcpy(hDib,src->hDib,GetSize());
}
/////////////////////////////////////////////////////////////////////
void CxDib::Clear(BYTE bval)
{
if (hDib) memset(GetBits(),bval,m_bi.biSizeImage);
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// CxShadeButton
CxShadeButton::CxShadeButton()
{
m_Border=1; //draw 3D border
m_FocusRectMargin=4; //focus dotted rect margin
m_TextColor=GetSysColor(COLOR_BTNTEXT); // default button text color
m_flat = m_Checked = m_button_down = m_tracking = false;
m_Icon=m_IconDown=m_IconHighLight=NULL; // icon handle
m_IconBox.SetRectEmpty(); //icon location
m_IconAlign=BS_CENTER; //icon alignment
m_TextAlign=DT_CENTER; //text alignment
m_pLF=NULL; //font structure pointer
}
/////////////////////////////////////////////////////////////////////////////
CxShadeButton::~CxShadeButton()
{
m_Font.DeleteObject();
if (m_pLF) free(m_pLF);
if (m_IconDown!=m_Icon && m_IconDown) DestroyIcon(m_IconDown);
if (m_IconHighLight!=m_Icon && m_IconHighLight) DestroyIcon(m_IconHighLight);
if (m_Icon) DestroyIcon(m_Icon);
}
/////////////////////////////////////////////////////////////////////////////
BEGIN_MESSAGE_MAP(CxShadeButton, CButton)
//{{AFX_MSG_MAP(CxShadeButton)
ON_WM_ERASEBKGND()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDBLCLK()
ON_WM_KILLFOCUS()
//ON_CONTROL_REFLECT_EX(BN_CLICKED, OnClicked)
ON_WM_KEYDOWN()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
ON_MESSAGE(WM_CXSHADE_RADIO , OnRadioInfo)
ON_MESSAGE(BM_SETCHECK , OnBMSetCheck)
ON_MESSAGE(BM_GETCHECK , OnBMGetCheck)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CxShadeButton message handlers
/////////////////////////////////////////////////////////////////////////////
void CxShadeButton::PreSubclassWindow()
{
m_Style=GetStyle(); //get general button styles
m_IsPushLike=((m_Style & BS_PUSHLIKE)!=0);
m_flat=((m_Style & BS_FLAT)!=0);
SetTextAlign(m_Style & 0x0300);
m_Style=GetButtonStyle(); ///get specific BS_ styles
if ((m_Style & BS_AUTOCHECKBOX)==BS_AUTOCHECKBOX)
// ||((m_Style & BS_CHECKBOX)==BS_CHECKBOX))
m_Style=BS_CHECKBOX;
else if ((m_Style & BS_AUTORADIOBUTTON)==BS_AUTORADIOBUTTON)
// ||((m_Style & BS_RADIOBUTTON)==BS_RADIOBUTTON))
m_Style=BS_RADIOBUTTON;
else { m_Style=BS_PUSHBUTTON; m_IsPushLike=true;}
//default radio & check-box has no border
if (!m_IsPushLike) m_Border = false;
CButton::PreSubclassWindow();
ModifyStyle(0, BS_OWNERDRAW);
}
/////////////////////////////////////////////////////////////////////////////
BOOL CxShadeButton::OnEraseBkgnd(CDC* pDC)
{ return 1; } // doesn't erase the button background
/////////////////////////////////////////////////////////////////////////////
void CxShadeButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
ASSERT (lpDrawItemStruct);
//TRACE("* Captured: %08X\n", ::GetCapture());
//Check if the button state in not in inconsistent mode...
POINT mouse_position;
if ((m_button_down) && (::GetCapture() == m_hWnd) && (::GetCursorPos(&mouse_position))){
if (::WindowFromPoint(mouse_position) == m_hWnd){
if ((GetState() & BST_PUSHED) != BST_PUSHED) {
//TRACE("* Inconsistency up detected! Fixing.\n");
SetState(TRUE);
return;
}
} else {
if ((GetState() & BST_PUSHED) == BST_PUSHED) {
//TRACE("* Inconsistency up detected! Fixing.\n");
SetState(FALSE);
return;
} } }
//TRACE("* Drawing: %08x\n", lpDrawItemStruct->itemState);
CString sCaption;
CDC *pRealDC = CDC::FromHandle(lpDrawItemStruct->hDC); // get device context
RECT r=lpDrawItemStruct->rcItem; // context rectangle
int cx = r.right - r.left ; // get width
int cy = r.bottom - r.top ; // get height
// get text box position
RECT tr={r.left+m_FocusRectMargin+2,r.top,r.right-m_FocusRectMargin-2,r.bottom};
CDC hdcMem; //create a memory DC to avoid flicker
hdcMem.CreateCompatibleDC(pRealDC);
CDC* pDC = &hdcMem; //(just use pRealDC to paint directly the screen)
CBitmap hBitmap; //create a destination for raster operations
hBitmap.CreateCompatibleBitmap(pRealDC,cx,cy);
CBitmap* pOldBitmap = hdcMem.SelectObject(&hBitmap); //select the destination for MemDC
GetWindowText(sCaption); // get button text
pDC->SetBkMode(TRANSPARENT);
// with MemDC we need to select the font...
//get text font
CFont* pOldFont=NULL;
if (m_Font.m_hObject) pOldFont = pDC->SelectObject(&m_Font);
else pDC->SelectObject(GetStockObject(DEFAULT_GUI_FONT));
// TEXTMETRIC tm;
// pDC->GetTextMetrics(&tm); // get font metrics
// accommodate text location
if (m_Icon){
switch (m_IconAlign){
case BS_LEFT:
tr.left+=m_IconBox.Width(); // shift left
break;
case BS_RIGHT:
tr.right-=m_IconBox.Width(); // shift right
break;
case BS_CENTER:
tr.top+=m_IconBox.bottom; // shift text under the icon
tr.bottom-=m_FocusRectMargin + 1;
}
}
// Select the correct skin
if (lpDrawItemStruct->itemState & ODS_DISABLED){ // DISABLED BUTTON
if(m_dDisabled.IsValid()) // paint the skin
m_dDisabled.Draw(pDC->GetSafeHdc(),0,0);
else // no skin selected for disabled state -> standard button
pDC->FillSolidRect(&r,GetSysColor(COLOR_BTNFACE));
if (m_Icon) //draw the icon
pDC->DrawState(m_IconBox.TopLeft(),m_IconBox.Size(),
m_Icon,DSS_DISABLED,(CBrush*)NULL);
// if needed, draw the standard 3D rectangular border
if ((m_Border)&&(m_flat==FALSE)) pDC->DrawEdge(&r,EDGE_RAISED,BF_RECT);
// paint the etched button text
pDC->SetTextColor(GetSysColor(COLOR_3DHILIGHT));
pDC->DrawText(sCaption,&tr,m_TextAlign);
pDC->SetTextColor(GetSysColor(COLOR_GRAYTEXT));
OffsetRect(&tr,-1,-1);
pDC->DrawText(sCaption,&tr,m_TextAlign);
} else {
//---------------------------------------------------------------------------
if ((lpDrawItemStruct->itemState & ODS_SELECTED)||m_Checked){ //SELECTED (DOWN) BUTTON
if(m_dDown.IsValid()) // paint the skin
m_dDown.Draw(pDC->GetSafeHdc(),m_Border,m_Border);
else // no skin selected for selected state -> standard button
pDC->FillSolidRect(&r,GetSysColor(COLOR_BTNFACE));
if (m_IconDown){ //draw the pushed icon
if (m_IsPushLike) m_IconBox.OffsetRect(1,1);
pDC->DrawState(m_IconBox.TopLeft(),m_IconBox.Size(),
m_IconDown,DSS_NORMAL,(CBrush*)NULL);
if (m_IsPushLike) m_IconBox.OffsetRect(-1,-1);
}
//shift text
if (m_IsPushLike) OffsetRect(&tr,1,1);
// if needed, draw the standard 3D rectangular border
if (m_Border){
if (m_flat) pDC->Draw3dRect(&r,GetSysColor(COLOR_BTNSHADOW),GetSysColor(COLOR_BTNHILIGHT));
else pDC->DrawEdge(&r,EDGE_SUNKEN,BF_RECT);
}
} else {
//---------------------------------------------------------------------------
if(m_dNormal.IsValid()){ // paint the skin // DEFAULT BUTTON
if ((m_tracking)&&(m_dOver.IsValid())){
m_dOver.Draw(pDC->GetSafeHdc(),0,0);
} else {
m_dNormal.Draw(pDC->GetSafeHdc(),0,0);
}
} else // no skin selected for normal state -> standard button
pDC->FillSolidRect(&r,GetSysColor(COLOR_BTNFACE));
if (m_IconHighLight && m_tracking) { // draw the highlighted icon
pDC->DrawState(m_IconBox.TopLeft(),m_IconBox.Size(),
m_IconHighLight,DSS_NORMAL,(CBrush*)NULL);
} else if (m_Icon) //draw the icon
pDC->DrawState(m_IconBox.TopLeft(),m_IconBox.Size(),
m_Icon,DSS_NORMAL,(CBrush*)NULL);
// if needed, draw the standard 3D rectangular border
if ((m_Border)&&((m_tracking)||(m_flat==FALSE))){
if (lpDrawItemStruct->itemState & ODS_DEFAULT){
pDC->DrawEdge(&r,EDGE_SUNKEN,BF_RECT);
InflateRect(&r,-1,-1);
pDC->DrawEdge(&r,EDGE_RAISED,BF_RECT);
} else {
if (m_flat) pDC->Draw3dRect(&r,GetSysColor(COLOR_BTNHILIGHT),GetSysColor(COLOR_BTNSHADOW));
else pDC->DrawEdge(&r,EDGE_RAISED,BF_RECT);
}
}
//---------------------------------------------------------------------------
}
// paint the focus rect
if ((lpDrawItemStruct->itemState & ODS_FOCUS)&&(m_FocusRectMargin>0)){
InflateRect(&r,-m_FocusRectMargin,-m_FocusRectMargin);
m_dh.Draw(pDC->GetSafeHdc(),1+r.left,r.top);
m_dh.Draw(pDC->GetSafeHdc(),1+r.left,r.bottom);
m_dv.Draw(pDC->GetSafeHdc(),r.left,1+r.top);
m_dv.Draw(pDC->GetSafeHdc(),r.right,1+r.top);
}
// paint the enabled button text
pDC->SetTextColor(m_TextColor);
pDC->DrawText(sCaption,&tr,m_TextAlign);
}
if (pOldFont) pDC->SelectObject(pOldFont); //restore the old font object
pRealDC->BitBlt(0,0,cx,cy,&hdcMem,0,0,SRCCOPY); //copy in the real world
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -