使用下面的函数,可实现将指定文件缩放后显示到按钮上。
前提是先将图片按钮的Bitmap属性设置为True。
#include <atlimage.h>
void CtestAppDlg::DrawPictureOnButton(CWnd *pBtn, const CString &sFilePath)
{
CBitmapButton *pBmpBtn = (CBitmapButton *)pBtn;
CImage img;
img.Load(sFilePath);
HBITMAP bitmap = img.Detach();
RECT rc;
pBmpBtn->GetWindowRect(&rc);
bitmap = (HBITMAP)CopyImage(
bitmap,
IMAGE_BITMAP,
rc.right - rc.left,
rc.bottom - rc.top,
0);
pBmpBtn->SetBitmap(bitmap);
pBmpBtn->SetButtonStyle(BS_BITMAP, FALSE);
}
调用时的代码为:
DrawPictureOnButton(GetDlgItem(IDC_BUTTON1), _T("C:\\picture\\10.bmp"));
DrawPictureOnButton(GetDlgItem(IDC_BUTTON2), _T("C:\\picture\\9.jpg"));
DrawPictureOnButton(GetDlgItem(IDC_BUTTON3), _T("C:\\picture\\head.gif"));
DrawPictureOnButton(GetDlgItem(IDC_BUTTON4), _T("C:\\picture\\2.PNG"));
DrawPictureOnButton(GetDlgItem(IDC_BUTTON5), _T("C:\\picture\\1.tif"));