int nCount=listRect.GetCount();
if(listRect.GetCount()>0)
{
CRect rectDlgNow;
m_pWnd->GetWindowRect(&rectDlgNow);
POSITION mP = listRect.GetHeadPosition();
CRect rectDlgSaved;
rectDlgSaved = listRect.GetNext(mP);
m_pWnd->ScreenToClient(rectDlgSaved);
m_pWnd->ScreenToClient(rectDlgNow);
float fRateScaleX = (float)(rectDlgNow.right-rectDlgNow.left)/(rectDlgSaved.right-rectDlgSaved.left);
float fRateScaleY = (float)(rectDlgNow.bottom-rectDlgNow.top)/(rectDlgSaved.bottom-rectDlgSaved.top);
m_pWnd->ClientToScreen(rectDlgSaved);
m_pWnd->ClientToScreen(rectDlgNow); LOGFONT stFont;
::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT), sizeof(stFont), &stFont);
strcpy(stFont.lfFaceName,"Times New Roman");
stFont.lfHeight = (long)(fRateScaleY*14);
stFont.lfWidth = (long)(fRateScaleX*7);
CFont * m_pCFont;
m_pCFont = new CFont;//这是别人写的程序,我下载下来参考。他此处new了一个,但是往下没有delete
//运行这个程序之后系统总是无缘无故随机蓝屏,我怀疑是这个new没有delete。
m_pCFont->CreateFontIndirect(&stFont); CRect rectChildSaved;
CWnd *pWndChild = m_pWnd->GetWindow(GW_CHILD);
while(pWndChild)
{
rectChildSaved = listRect.GetNext(mP);
rectChildSaved.left = rectDlgNow.left+(int)((rectChildSaved.left-rectDlgSaved.left)* fRateScaleX);
rectChildSaved.top = rectDlgNow.top+(int)((rectChildSaved.top-rectDlgSaved.top)*fRateScaleY);
rectChildSaved.right = rectDlgNow.right+(int)((rectChildSaved.right-rectDlgSaved.right)* fRateScaleX);
rectChildSaved.bottom = rectDlgNow.bottom+(int)((rectChildSaved.bottom-rectDlgSaved.bottom)* fRateScaleY);
m_pWnd->ScreenToClient(rectChildSaved);
pWndChild->MoveWindow(rectChildSaved); pWndChild->SetFont(m_pCFont); pWndChild = pWndChild->GetNextWindow();
}
delete m_pCFont;//我在此处加了一个delete,但是程序运行后字体变成粗体了,不是以前对话框刚创建时的那样~
//如果不加这个delete字体倒是比较正常,
//请问我到底该加不加这个delete?加在什么位置?加载之后字体变得粗体了怎么办?
}

解决方案 »

  1.   

    m_pCFont应该是成员变量,可以考虑在析构时释放运行这个程序之后系统总是无缘无故随机蓝屏
    看看有没有资源泄漏
    如果多次new,设计有问题
      

  2.   

    这段代码是放在onsize中,每次对话框大小改变就会触发这段代码。请问资源泄漏怎么查?给个提示呗?
      

  3.   

    推荐一个在vs2010下能用的吧,公司让用vs2010……
      

  4.   


    呵呵~我找了~没有~
    原来2010直接就可以检测内存泄露~~检测我上面的代码后有这个提示Detected memory leaks!
    Dumping objects ->
    d:\users\ww\desktop\dialogex\dialogex\1 (4).net\dialogex - 副本\dialogex.cpp(116) : {498} client block at 0x0008A610, subtype c0, 8 bytes long.
    a CFont object at $0008A610, 8 bytes long
    Object dump complete.就是说m_pCFont = new CFont;这句存在泄露~~我把delete m_pCFont放在析构函数中还不行啊~还提示泄露~
      

  5.   

    OnSize里么?没必要吧,你不需要每次都创建一个CFont。不用new,直接把CFont做成成员变量,在类构造的时候初始化它就行了,类析构时会自动释放CFont对象。
    就像这样:
    http://support.microsoft.com/kb/85518/zh-cn
      

  6.   

    如果你想修改字体,直接先将CFont里面的字体DeleteObject,然后重新CreateFontIndirect,然后再SetFont