请帮看下如下的按钮函数怎么不能输出文字?
这个一个基于对话框的程序:
void Cusb03Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CPaintDC dc(this); // 用于绘制的设备上下文
CString str(_T("银颗粒"));
//create a CFont object
CFont font;
//assign parameters font.CreateFont(11, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, 0, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, _T("Times New Roman")); CFont* pFont = dc.SelectObject(&font);

dc.TextOutW(5,5, _T("hello"), 10);
}

解决方案 »

  1.   

    加上:
    dc.SelectObject(pFont);
    pFont.DeleteObject();
    pFont=NULL;
    还有TextOutw第三个参数:
    CDC::TextOutvirtual BOOL TextOut(int x ,int y ,LPCSTR lpszString ,int nCount );
    BOOL TextOut(int x ,int y ,const CString& str);返回值:如果成功,则返回非零值,否则为0。参数: x 指定文本起点的X逻辑坐标。  
    y 指定文本起点的Y逻辑坐标。  
    lpszString 要绘制的字符串的指针。  
    nCount 字符串中的字节数。  
    str 包含字符的CString对象。  
      

  2.   

    多谢大侠的回复。
    但是我只是修改CPaintDC 为CClientDC后问题解决。
      

  3.   

    额,没发现,的确是的
    CPaintDC只能在OnPaint()中使用才有效
    但是,你的程序存在内存泄露
    GDI对象使用完后需要释放资源
      

  4.   

    It performs a CWnd::BeginPaint at construction time and CWnd::EndPaint at destruction time. A CPaintDC object can only be used when responding to a WM_PAINT message, usually in your OnPaint message-handler member function. For more information on using CPaintDC, see Device Contexts.