这是该代码的代码,用于维护控件转到int WindowRatio=WidthOfPreviewPane/HeightOfPreviewPanel时崩溃的图像的纵横比;有人能说出原因吗??
int WidthOfPreviewPane = RECTWIDTH(m_rcParent);
int HeightOfPreviewPane = RECTHEIGHT(m_rcParent) ;
int ImageRatio = WidthOfImage / HeightOfImage;
int WindowRatio = WidthOfPreviewPane / HeightOfPreviewPane;
if (WindowRatio > ImageRatio && WidthOfPreviewPane< WidthOfImage)
{
m_iFinalHeight = HeightOfPreviewPane;
m_iFinalWidth = m_iFinalHeight * ImageRatio;
MessageBox(NULL, L"1",L"Error",
MB_ICONERROR | MB_OK);
}
else if (WindowRatio < ImageRatio && WidthOfPreviewPane< WidthOfImage)
{
m_iFinalWidth = WidthOfPreviewPane;
m_iFinalHeight = m_iFinalWidth / ImageRatio;
MessageBox(NULL, L"2",L"Error",
MB_ICONERROR | MB_OK);
}
else if(WindowRatio > ImageRatio && WidthOfPreviewPane> WidthOfImage)
{
m_iFinalHeight = HeightOfImage;
m_iFinalWidth = WidthOfImage;
MessageBox(NULL, L"3",L"Error",
MB_ICONERROR | MB_OK);
}
else if(WindowRatio < ImageRatio && WidthOfPreviewPane> WidthOfImage)
{
m_iFinalHeight = HeightOfImage;
m_iFinalWidth = WidthOfImage;
MessageBox(NULL, L"4",L"Error",
MB_ICONERROR | MB_OK);
}
这个算法的逻辑是正确的。最后我发现WidthOfPreviewPane和HeightOfPreviewPane=0是因为我写这段代码的函数最后初始化了,所以当我调试它们时,这两个函数没有初始化。我通过将它们放在if条件中来避免这个问题,如果它们的值不为0,它会让控件进入内部工作得很好。看到这个-
if(WidthOfPreviewPane!= 0 && HeightOfPreviewPane!=0 )
{
conditions here......
}
这就解决了。