15,979
社区成员
发帖
与我相关
我的任务
分享
void GdipButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
int i = 0;
if(lpDIS->itemState & ODS_DISABLED) i = 3;
else if (lpDIS->itemState & ODS_SELECTED) i = 2;
else if (hovering) i = 1;
//else index = 0;
CRect rect;
if(gdipDraw[i] == NULL && image[i] != NULL)
{
GetWindowRect(rect);
GetParent()->ScreenToClient(rect);
gdipDraw[i] = new GdipDraw(lpDIS->hDC,rect.Width(),rect.Height());
gdipDraw[i]->CopyFrom(hBackDC,rect.left,rect.top);//拷入背景图片的HDC hBackDC//加载是在对话框程序里通过按钮类接口函数进行加载的 代码就不复制了
gdipDraw[i]->DrawImage(image[i]);
CString text;
GetWindowText(text);
if(!text.IsEmpty())
{
rect.left = lpDIS->rcItem.left + textRect.left;
rect.right = lpDIS->rcItem.right - textRect.right/*-rect.left*/;
rect.top = lpDIS->rcItem.top + textRect.top;
rect.bottom = lpDIS->rcItem.bottom - textRect.bottom/*-rect.top*/;
gdipDraw[i]->DrawText(text,rect,textFont,textColor,textFormat);
}
}
if(gdipDraw[i] != NULL)
{
::GetClipBox(lpDIS->hDC,rect);
gdipDraw[i]->CopyTo(lpDIS->hDC,rect);//把Png图片画到lpDIS->hdc里
}
}
bool GdipDraw::CopyFrom(HDC hDC,int x /*= 0*/,int y /*= 0*/)
{
if(hDC == 0 || hMemDC == 0)
return false;
return ::BitBlt(hMemDC,0,0,width,height,hDC,x,y,SRCCOPY) == TRUE;
}
bool GdipDraw::DrawImage( Image *image )
{
if(image == 0 || hMemDC == 0)
return false;
Gdiplus::Graphics graphics(hMemDC);
return graphics.DrawImage(image,0,0,width,height) == Gdiplus::Ok;
}
BOOL CGButtonTestDlg::OnEraseBkgnd(CDC* pDC)
{
CRect rect;
pDC->GetClipBox(rect);
gd.CopyTo(pDC->m_hDC,rect);//对话框类的把按钮类把背景图片与png按钮的不同状态png图片处理完成一张图片之后,拷贝到CDC*pDC里
return TRUE;
//是这个步的问题?????
}
bool GdipDraw::CopyTo( HDC hDC,LPRECT rect )
{
if(hDC == 0 || hMemDC == 0 || rect == 0)
return false;
return ::BitBlt(hDC,
rect->left,rect->top,
rect->right - rect->left,
rect->bottom - rect->top,
hMemDC,rect->left,rect->top,SRCCOPY) == TRUE;
}
bool GdipImage::Load( UINT id,LPCSTR type)
{
Clear();
ASSERT(type != 0);
HRSRC res = ::FindResource(NULL, MAKEINTRESOURCE(id), "PNG");
if ( res == NULL) return false;
DWORD size = ::SizeofResource(NULL, res);
if (size == 0) return false;
LPCVOID data = ::LockResource(::LoadResource(NULL, res));
if (data == NULL) return false;
hBuffer = ::GlobalAlloc(GMEM_MOVEABLE, size);
if(hBuffer == NULL) return false;
void* buff = ::GlobalLock(hBuffer);
if (buff != NULL)
{
CopyMemory(buff, data, size);
IStream* pstream = NULL;
if (::CreateStreamOnHGlobal(hBuffer, FALSE, &pstream) == S_OK)
{
image = Gdiplus::Bitmap::FromStream(pstream);
pstream->Release();
if (image != NULL)
{
if (image->GetLastStatus() == Gdiplus::Ok)
return true;
delete image;
image = NULL;
}
}
::GlobalUnlock(hBuffer);
}
::GlobalFree(hBuffer);
hBuffer = NULL;
return false;
}