码迷,mamicode.com
首页 > 编程语言 > 详细

MFC图片自适应

时间:2017-05-18 16:43:10      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:paint   控件   getheight   picture控件   void   dex   方法   rect   添加   

MFC图片自适应方法:

void CSimMachineLogin::OnPaint()
{
 CPaintDC dc(this); // device context for painting
 // TODO:  在此处添加消息处理程序代码
 int height, width;
 CRect rect;//定义矩形类
 CRect rect1;
 CImage image; //创建图片类

 CString workPath = Common::GetAppInstallDir();
 CString path = workPath + _T("\\logo.png");
 image.Load(path);
 height = image.GetHeight();
 width = image.GetWidth();

 p_PICTURE.GetClientRect(&rect); //获得pictrue控件所在的矩形区域
 CDC *pDc = p_PICTURE.GetDC();//获得pictrue控件的Dc
 SetStretchBltMode(pDc->m_hDC, STRETCH_HALFTONE);

 if (width <= rect.Width() && height <= rect.Width()) //小图片,不缩放
 {
  rect1 = CRect(rect.TopLeft(), CSize(width, height));
  image.StretchBlt(pDc->m_hDC, rect1, SRCCOPY); //将图片画到Picture控件表示的矩形区域
 }
 else
 {
  //float xScale = (float)rect.Width() / (float)width;
  //float yScale = (float)rect.Height() / (float)height;
  //float ScaleIndex = (xScale >= yScale) ? xScale : yScale;
  //rect1 = CRect(rect.TopLeft(), CSize((int)width*ScaleIndex, (int)height*ScaleIndex));
  //
  CRect clientRect, tabRect;
  this->GetClientRect(&clientRect);    // 获取标签控件客户区Rect  
  tabRect.left = clientRect.left;
  tabRect.right = clientRect.right;
  tabRect.top = clientRect.top;
  tabRect.bottom = clientRect.top + 0.45 * clientRect.Height();
  p_PICTURE.MoveWindow(&tabRect);
  tabRect.top = clientRect.top+30;
  image.StretchBlt(pDc->m_hDC, tabRect, SRCCOPY); //将图片画到Picture控件表示的矩形区域
 }
 ReleaseDC(pDc);//释放picture控件的Dc
 
 // 不为绘图消息调用 CDialogEx::OnPaint()
}

                                                                                           SYQ

 

MFC图片自适应

标签:paint   控件   getheight   picture控件   void   dex   方法   rect   添加   

原文地址:http://www.cnblogs.com/sheyingqi/p/6873777.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!