| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4409 人关注过本帖
标题:c++中如何让图片在窗体中自适应大小!
只看楼主 加入收藏
evileast8586
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2009-4-2
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:6 
c++中如何让图片在窗体中自适应大小!
c++中如何让图片在窗体中自适应大小!
最近在修改一个mfc的软件,该软件需要让图片在窗体自适应大小来显示。希望各位大侠帮忙哦!
搜索更多相关主题的帖子: 图片自适应窗体大小 
2009-08-14 09:17
xufen340
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:166
专家分:1351
注 册:2009-8-7
收藏
得分:20 
mfc 加入wm_paint消息
代码处输入:

CBitmap bmp;
   if (bmp.LoadBitmap(IDB_BITMAP1))
   {
      BITMAP bmpInfo;
      bmp.GetBitmap(&bmpInfo);
      CDC dcMemory;
      CDC* pDC=GetWindowDC();
      dcMemory.CreateCompatibleDC(pDC);
      CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);
      CRect rect;
      GetClientRect(&rect);
      int nX = rect.left + (rect.Width() - bmpInfo.bmWidth) / 2;
      int nY = rect.top + (rect.Height() - bmpInfo.bmHeight) / 2;
      pDC->BitBlt(nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory,  
         0, 0, SRCCOPY);
      dcMemory.SelectObject(pOldBitmap);
      ReleaseDC(pDC);
      }
2009-08-14 11:09
xufen340
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:166
专家分:1351
注 册:2009-8-7
收藏
得分:0 
IDB_BITMAP1是你资源文件里插入的位图名
2009-08-14 11:11
xufen340
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:166
专家分:1351
注 册:2009-8-7
收藏
得分:0 
StretchBlt是根据目标大小放大缩小   
   
// TODO: Add your message handler code here
   CBitmap bmp;
   if (bmp.LoadBitmap(IDB_BITMAP1))
   {
      BITMAP bmpInfo;
      bmp.GetBitmap(&bmpInfo);
      CDC dcMemory;
      CDC* pDC=GetWindowDC();
      dcMemory.CreateCompatibleDC(pDC);
      CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);
      CRect rect;
      GetClientRect(&rect);
      int nX = rect.left + (rect.Width() - bmpInfo.bmWidth) / 2;
      int nY = rect.top + (rect.Height() - bmpInfo.bmHeight) / 2;
      //pDC->BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory, 0, 0, SRCCOPY);  
      pDC->SetStretchBltMode(COLORONCOLOR);
      pDC->StretchBlt(0, 0,rect.Width(),rect.Height(),&dcMemory,0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight,SRCCOPY);
      dcMemory.SelectObject(pOldBitmap);
      ReleaseDC(pDC);
      }
2009-08-14 12:54
evileast8586
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2009-4-2
收藏
得分:0 
大哥,我编写的这个软件不仅仅是图片的自适应问题,因为这个程序需要获取摄像头的,然后在打开摄像头的时候也需要时自适应大小地显示在窗体当中。通过摄像头可以抓取图片或者是拍摄视频哦!
2009-08-19 10:57
evileast8586
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2009-4-2
收藏
得分:0 
大哥,你帮我看看这一段函数代码:

void CImageProcessView::DrawAll(CDC *pDC)
{
     
    CImageProcessDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;
 
    CFont NewFont;
    CFont *pOldFont;
 
    CPen NewPen(PS_SOLID,CPublic::m_LineWidth,CPublic::m_DrawColor);
    CPen *pOldPen;
 
 
     
    NewFont.CreateFontIndirect(&m_LogFont);
    pOldFont = pDC->SelectObject(&NewFont);
    pDC->SetTextColor(CPublic::m_DrawColor);
    pOldPen=pDC->SelectObject(&NewPen);
 
 
 
    if (this->m_IsStatis)
    {
        if (!m_ImageStatis.IsNull())
            {
            SetStretchBltMode(pDC->m_hDC, STRETCH_HALFTONE);
            m_ImageStatis.StretchBlt(pDC->m_hDC,0,0,m_ImageStatis.GetWidth()*CPublic::m_ZoomValue,m_ImageStatis.GetHeight()*CPublic::m_ZoomValue,SRCCOPY);
            }
 
        return;
    }
     
    if (!this->m_IsPrinting)
    {
     
         
        if (!m_ImagetoCompare.IsNull())
        {
            if (!m_Image.IsNull())
            {
 
            SetStretchBltMode(pDC->m_hDC, STRETCH_HALFTONE);
            m_Image.StretchBlt(pDC->m_hDC,0,0,m_Image.GetWidth()/CPublic::zip,m_Image.GetHeight()/CPublic::zip,SRCCOPY);
            }
            
 
            double width = m_ImagetoCompare.GetWidth()*
                        pImageCmparedlg->m_ImageToCompateScale
                        /CPublic::m_CurScaleValue/CPublic::zip;
           double height = m_ImagetoCompare.GetHeight()*
                        pImageCmparedlg->m_ImageToCompateScale
                        /CPublic::m_CurScaleValue/CPublic::zip;
           m_ImagetoCompare.Draw(pDC->m_hDC,m_Image.GetWidth()/CPublic::zip+1,0,width,height);
        }
        else
        {
            if (!m_Image.IsNull())
            {
            SetStretchBltMode(pDC->m_hDC, STRETCH_HALFTONE);
            m_Image.StretchBlt(pDC->m_hDC,0,0,m_Image.GetWidth()*CPublic::m_ZoomValue,m_Image.GetHeight()*CPublic::m_ZoomValue,SRCCOPY);
            }
            
 
        }
 
        POSITION pos = this->m_ComponentList.GetHeadPosition();
        while (pos != NULL)
        {
        CComponent* pCmp =(CComponent *) this->m_ComponentList.GetNext(pos);
        pCmp->ScaleDraw(CPublic::m_ZoomValue);
        pCmp->Draw(pDC);
        }
    }
    else
    {
 
        if (!this->m_ImagetoPrint.IsNull())
        {
         
 
        double width = CPublic::m_CurScaleValue*m_Image.GetWidth()*this->pPrintSetupDlg->m_editMagnification*10;
        double height = CPublic::m_CurScaleValue*m_Image.GetHeight()*this->pPrintSetupDlg->m_editMagnification*10;
         
 
         
        CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));//获取透明画刷对象指针
        CBrush *pOldBrush=pDC->SelectObject(pBrush);//将透明画刷选入DC
        CPen  newPen(PS_SOLID,1,RGB(0,0,0));
        pDC->SetBkMode(TRANSPARENT);  
         
        CPen *pOldPen = pDC->SelectObject(&newPen);
 
        CRect rectCreater(100,-200,2000,-300);
         
        CRect rectMang(rectCreater.left,rectCreater.top-1-abs(rectCreater.Height()),
            rectCreater.right,rectCreater.bottom-1-abs(rectCreater.Height()));
        CRect rectDes(rectMang.left,rectMang.top-1-abs(rectMang.Height()),
            rectMang.right,rectMang.bottom-1-abs(rectMang.Height()));
 
        pDC->DrawText("Creater",&rectCreater,DT_EXTERNALLEADING);
        CString strTemp="Magnification";
        pDC->DrawText(strTemp,&rectMang,DT_EXTERNALLEADING);
        strTemp.Format("%d",pPrintSetupDlg->m_editMagnification);
        pDC->DrawText("Description",&rectDes,DT_EXTERNALLEADING);
 
        pDC->MoveTo(rectCreater.left,rectCreater.top);
        pDC->LineTo(rectCreater.left,rectCreater.top-2500);
        pDC->LineTo(rectCreater.right,rectCreater.top-2500);
        pDC->LineTo(rectCreater.right,rectCreater.top);
        pDC->LineTo(rectCreater.left,rectCreater.top);
 
 
        pDC->MoveTo(rectDes.left,rectDes.bottom);
        pDC->LineTo(rectDes.right,rectDes.bottom);
 
        pDC->MoveTo(rectCreater.left + 300,rectCreater.top);
        pDC->LineTo(rectCreater.left + 300,rectCreater.top-300);
 
        pDC->MoveTo(rectCreater.left,rectCreater.bottom);
        pDC->LineTo(rectCreater.right,rectCreater.bottom);
 
        pDC->MoveTo(rectMang.left,rectMang.bottom);
        pDC->LineTo(rectMang.right,rectMang.bottom);
 
        pDC->MoveTo(rectCreater.left,rectCreater.top+100);
        pDC->LineTo(rectCreater.right,rectCreater.top+100);
 
        pDC->MoveTo(rectCreater.left,rectCreater.top+100);
        pDC->LineTo(rectCreater.left,rectCreater.top);
 
        pDC->MoveTo(rectCreater.right,rectCreater.top+100);
        pDC->LineTo(rectCreater.right,rectCreater.top);
 
        pDC->TextOut(rectCreater.left+800,-102,pPrintSetupDlg->m_editTitle);
        pDC->TextOut(rectCreater.left+302,rectCreater.top-2,pPrintSetupDlg->m_editCreater);
        pDC->TextOut(rectMang.left+302,rectMang.top-2,strTemp);
        pDC->TextOut(rectDes.left+302,rectDes.top-2,pPrintSetupDlg->m_editDescription);
 
 
         
        SetStretchBltMode(pDC->m_hDC, STRETCH_HALFTONE);
        m_ImagetoPrint.StretchBlt(pDC->m_hDC,rectDes.left+10,rectDes.bottom-10,width,-height,SRCCOPY);
 
     
        pDC->SelectObject(pOldBrush);
        pDC->SelectObject(pOldPen);
        }
 
    }
 
    //pDC->SelectObject(pOldFont);
   // pDC->SelectObject(pOldPen);
 
}
2009-08-19 11:02
LOVE110
Rank: 1
等 级:新手上路
帖 子:61
专家分:0
注 册:2008-11-21
收藏
得分:0 
太强了,我当场晕倒
2009-11-19 13:01
快速回复:c++中如何让图片在窗体中自适应大小!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022265 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved