今天在一个button上自绘图片,缩放时失真了,解决方法是为dc设置的属性,方法如下:
::SetStretchBltMode(lpDrawItemStruct->hDC,HALFTONE);
::SetBrushOrgEx(lpDrawItemStruct->hDC,0,0,NULL);
自绘函数如下:
void MJPicButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
CRect rt;
this->GetClientRect(rt);
switch(m_type)
{
...............
case picbType_pic:
{
CImage im;
if (S_OK==im.Load(m_path))
{
int picH = im.GetHeight();
int picW = im.GetWidth();
CRect lrc(rt);
if (picW>picH)
{
lrc.bottom = picH*lrc.right/picW;
}
else
{
lrc.right = picW*lrc.bottom/picH;
}
::SetStretchBltMode(lpDrawItemStruct->hDC,HALFTONE);
::SetBrushOrgEx(lpDrawItemStruct->hDC,0,0,NULL);
//im.Draw(lpDrawItemStruct->hDC,lrc);//设置属性后,这个也可工作正常
im.StretchBlt(lpDrawItemStruct->hDC,lrc/*DestRect*/,CRect(0,0,im.GetWidth(),im.GetHeight())/*SourceRect*/,SRCCOPY);
}
}
break;
default:
break;
}
}