15,981
社区成员
发帖
与我相关
我的任务
分享
void CMyButton::PreSubclassWindow()
{
this->ModifyStyle(0, BS_OWNERDRAW); //修改为自绘button
//设置按钮的有效区域
CRgn rgn;
CRect rc;
GetClientRect(&rc);
//有效区域为一个角半径为5的圆角矩形
rgn.CreateRoundRectRgn(rc.left,rc.top,rc.right,rc.bottom,10,10);
SetWindowRgn(rgn,TRUE);
rgn.DeleteObject();
CButton::PreSubclassWindow();
}
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
HDC hdc=lpDrawItemStruct->hDC;
CDC *pDC=CDC::FromHandle(hdc);
DrawAlphaButton(pDC);
}
void CMyButton::DrawAlphaButton(CDC *pDC)
{
CRect parantrect;
CRect rect;
CRect winrect;
GetClientRect(&rect);
GetWindowRect(&winrect);
GetParent()->GetClientRect(¶ntrect);
GetParent()->ScreenToClient(&winrect);//获取button控件相对于父窗口的坐标位置
CDC dcMem;
CDC dcMem2;//绘制纯色位图的内存DC
CDC dcMem3;//获取父窗口背景的内存DC
dcMem.CreateCompatibleDC(pDC);
dcMem2.CreateCompatibleDC(pDC);
dcMem3.CreateCompatibleDC(pDC);
CBitmap bkimg;
CBitmap bkimg2;
CBitmap bkimg3;
bkimg.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
bkimg2.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
bkimg3.CreateCompatibleBitmap(pDC, parantrect.Width(), parantrect.Height());
CBitmap *pImgOld=dcMem.SelectObject(&bkimg);
CBitmap *pImgOld2=dcMem2.SelectObject(&bkimg2);
CBitmap *pImgOld3=dcMem3.SelectObject(&bkimg3);
HBRUSH hbr=CreateSolidBrush(RGB(255,0,0));
FillRect(dcMem2,&rect,hbr);
DeleteObject(hbr);
HRGN hRgn=CreateRectRgn(winrect.left, winrect.top,winrect.right,winrect.bottom);
SelectClipRgn (dcMem3.m_hDC,hRgn);
GetParent()->SendMessage( WM_ERASEBKGND, (WPARAM)dcMem3.m_hDC, 0);
dcMem.BitBlt(0, 0, rect.Width(), rect.Height(), &dcMem3, winrect.left, winrect.top, SRCCOPY);
//将内存DC上的图象拷贝到前台
BLENDFUNCTION blend;
memset( &blend, 0, sizeof( blend) );
blend.BlendOp= AC_SRC_OVER;
blend.SourceConstantAlpha= 150; // 透明度 最大255
AlphaBlend (dcMem.m_hDC,0,0,rect.Width(), rect.Height(),dcMem2.m_hDC, 0,0,rect.Width(), rect.Height(),blend);
pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &dcMem, 0, 0, SRCCOPY);
CString strText;
GetWindowText(strText);
SetBkMode(pDC->m_hDC,TRANSPARENT);
if (isbuttondown)
{
CRect rectdown=rect;
rectdown.OffsetRect(0,1);
DrawText(pDC->m_hDC,strText,-1,&rectdown,DT_SINGLELINE|DT_CENTER|DT_VCENTER|DT_WORD_ELLIPSIS);
}
else
{
DrawText(pDC->m_hDC,strText,-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER|DT_WORD_ELLIPSIS);
}
dcMem.DeleteDC();
dcMem2.DeleteDC();
dcMem3.DeleteDC();
bkimg.DeleteObject();
bkimg2.DeleteObject();
bkimg3.DeleteObject();
}