CStatic中保持图形比例不变,尽量填充控件空间的代码
CStatic中保持图形比例不变,尽量填充控件空间的代码
先获取控件的高、宽,然后获取图像的高、宽,测试需要调整高还是调整宽
void CImagePreviewStatic::DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/)
{
Unit units;
CRect rect;if (m_img2 !=NULL) {//获取到HDC HWND hWnd = GetParent()->m_hWnd;
HDC hDc=::GetDC(hWnd);//获取画的位置 GetWindowRect(&rect);
::ScreenToClient(hWnd, (LPPOINT)&rect);
::ScreenToClient(hWnd, (LPPOINT)(&rect) + 1);
::SetStretchBltMode(hDc, COLORONCOLOR);int rectWidth = rect.right -rect.left;int rectHeight= rect.bottom -rect.top;//在这儿计算显示区域 int nHeight = m_img2->GetHeight();int nWidth = m_img2->GetWidth();//假设图像高度不变,则在当前RECT下,需要多少的宽度 int nAdjWidth = (rectHeight*nWidth)/nHeight;//如果当前宽度大于现有宽度,则宽度不变,调整高度信息 if (nAdjWidth >rectWidth) {int nAdjHeight = (rectWidth*nHeight)/nWidth;
rect.bottom= rect.top +nAdjHeight;
}else{//只需要调整不等于的情况 if (nAdjWidth !=rectWidth){
rect.right= rect.left +nAdjWidth;
}
}//画图片 m_img2->Draw(hDc,rect);
}
}