我有一个Windows(XP)应用程序需要在一个窗口内显示一个二维矩形。该矩形不能被剪裁(即必须总是完全躺在视口内),并且必须保持其调整大小的纵横比。目前,处理布局的方法会扭曲矩形的长宽比以匹配窗口。我想要矩形缩放到窗口并居中在窗口中(再次,没有剪辑)。现在的方法如下。 lWinDist和lMaxDepth是要被显示的矩形的宽度和高度(在一英寸的48ths,如果它的事项):在调整窗口大小时保留二维对象的纵横比
void CRoRRecView::RedoLayout(long lWinDist, long lMaxDepth)
{
CDC* pDC = GetDC() ;
if (pDC != NULL)
{
m_lWinDist = lWinDist;
GetClientRect(m_rectClient) ;
int nClientWidth = m_rectClient.Width();
int nClientHeight = m_rectClient.Height();
glViewport(0, 0, nClientWidth, nClientHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
m_fWinXDist = (float) lWinDist ;
m_fWinYDist = lMaxDepth ;
m_fAspectRatio = m_fWinXDist/m_fWinYDist;
glOrtho(0.0, m_fWinXDist, 0.0, m_fWinYDist, -1, 1) ;
glRotatef(180.0, 0,1,0);
glTranslatef((float)(-1 * lWinDist),0,0); // Translate across the x axis
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
ReleaseDC(pDC) ;
}
}
谢谢。但是,虽然保持对象的纵横比和居中,但窗口大小不会调整大小。也就是说,如果物体开始占据窗口高度的3/4,则在调整大小后,它总是占据窗口高度的3/4。 – PSU 2010-12-03 22:21:19
@PSU:是的,你说得对。我有一个稍微不同的版本,做了正确的事情,我会看看我是否可以把它拉起来。 – genpfault 2010-12-04 00:26:15