VC/MFC 如何将位图缩放显示在Static控件中

//在Static控件内显示位图
void C**Dlg::ShowBmpInStaic()
{
    HBITMAP hbitmap;
    //将pStatic指向要显示的地方
    CStatic *pStatic = NULL;

    //根据ID获取Static控件
    pStatic=(CStatic*)GetDlgItem(IDC_IMAGE);
    //装载资源*.bmp
    hbitmap=(HBITMAP)::LoadImage (::AfxGetInstanceHandle(),"*.bmp",
        IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);


    CBitmap hbmp;
    hbmp.Attach(hbitmap);
    //获取图片内容
    BITMAP bm;
    hbmp.GetBitmap(&bm);
    CDC dcMem;

    //创建兼容DC
    dcMem.CreateCompatibleDC(GetDC());
    CBitmap *pOldBitmap=(CBitmap*)dcMem.SelectObject(hbmp);
    CRect lRect;

    //获取Static控件的大小范围
    pStatic->GetClientRect(&lRect);
    //在Static控件上显示位图
    pStatic->GetDC()->StretchBlt(lRect.left ,lRect.top ,lRect.Width(),lRect.Height(),
        &dcMem,0 ,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
    dcMem.SelectObject(&pOldBitmap);
}

PS:举一反三,同理可将位图显示在其他控件中

你可能感兴趣的:(c,image)