以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 C/C++编程思想 』  (http://bbs.xml.org.cn/list.asp?boardid=61)
----  OpenGL学习之三:等比例缩放  (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=87485)


--  作者:葛靖青001
--  发布时间:11/5/2010 11:14:00 AM

--  OpenGL学习之三:等比例缩放
函数中的第一个参数rcScreen为图像要被显示的矩形大小,第二个参数sizePicture为图像自身大小,第三个参数bCenter为是否居中显示,返回值CRect既为图像最佳显示大小。

  函数如下所示:

  CRect RectSizeWithConstantRatio( CRect* rcScreen,

  CSize sizePicture,

  BOOL bCenter)

  {

  CRect rect(rcScreen);

  double dWidth = rcScreen->Width();

  double dHeight = rcScreen->Height();

  double dAspectRatio = dWidth/dHeight;

  double dPictureWidth = sizePicture.cx;

  double dPictureHeight = sizePicture.cy;

  double dPictureAspectRatio = dPictureWidth/dPictureHeight;

  //If the aspect ratios are the same then the screen rectangle

  // will do, otherwise we need to calculate the new rectangle

  if (dPictureAspectRatio > dAspectRatio)

  {

  int nNewHeight = (int)(dWidth/dPictureWidth*dPictureHeight);

  int nCenteringFactor = (rcScreen->Height() - nNewHeight) / 2;

  rect.SetRect( 0,

  nCenteringFactor,

  (int)dWidth,

  nNewHeight + nCenteringFactor);

  }

  else if (dPictureAspectRatio < dAspectRatio)

  {

  int nNewWidth = (int)(dHeight/dPictureHeight*dPictureWidth);

  int nCenteringFactor = (rcScreen->Width() - nNewWidth) / 2;

  rect.SetRect( nCenteringFactor,

  0,

  nNewWidth + nCenteringFactor,

  (int)(dHeight));

  }

  return rect;

  };

  l 此函数的使用方法如下:

  {

  ……(部分省略)

  //得到屏幕大小

  CRect rcScreen( 0, 0,

  GetSystemMetrics(SM_CXSCREEN),

  GetSystemMetrics(SM_CYSCREEN));

  //得到图像尺寸

  CSize sizePicture(picture.m_Width, picture.m_Height);

  CRect rcNewPictureRect =

  SizeRectWithConstantAspectRatio(&rcScreen,

  sizePicture,

  TRUE);

  picture.Show(&dc, rcNewPictureRect);

  }


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
31.250ms