在视类里有一个编辑框,我想让他显示我新创建的字体,代码如下:int CDspEditorView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
if (CFormView::OnCreate(lpCreateStruct) == -1)
return -1;

// TODO: Add your specialized creation code here
CFont font;
LOGFONT new_style;
CFont*pfont=GetFont();
if(pfont!=NULL)
{
new_style.lfUnderline=TRUE;
new_style.lfHeight=17;
new_style.lfWeight=FW_BOLD|FW_BLACK;

font.CreateFontIndirect(&new_style);

}

CEdit   *pEdit   =   (CEdit   *)GetDlgItem(IDC_VIEWUP_EDIT1); 
pEdit->SetFont(&font);
font.DeleteObject();
return 0;
}怎么在运行的时候会出现说内存不能为read;
我调试发现是在pEdit->SetFont(&font);这行出现的,调试时在这里弹出对话框说Unhandled exception in DspEdit0r.exe(MFC42D.DLL):OC00000005:Access  Violation;请指点一下 我该怎么弄啊? 谢谢! 

解决方案 »

  1.   

    font 这个是什么值?
    附上改变字体代码
    CFont *pFont = GetDlgItem(IDC_USERNAME)->GetFont();
    LOGFONT lfont;
    pFont->GetLogFont(&lfont);
    lfont.lfHeight = -18;
    m_Font.CreateFontIndirect(&lfont);
    GetDlgItem(IDC_USERNAME)->SetFont(&m_Font);
      

  2.   

    在这里CDspEditorView::OnCreate(LPCREATESTRUCT lpCreateStruct)。应该是在creat后海没有编辑框呢所以它得不到就出错了。
    我想问一下,在视类里没有初始化函数,又又很多东西要在初始化里做。该怎么办呢?即使自己手动写一个 我该怎么才能让这个工程一打开视类窗口时去响应我手动写的初始化函数呢?