HBRUSH CffffDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{....
if(pWnd->GetDlgCtrlID()==IDC_EDIT1);
//pDC->SetTextCharacterExtra(20);//正确
(edit.GetDC())->SetTextCharacterExtra(20);//不正确(edit为控件变量)
...
}
pDC和(edit.GetDC())为什么不同.应该相同啊同一个控件的CDC指针啊!

解决方案 »

  1.   

    CDC::GetDC 有一个作用域,离开这个作用域就无效了
    而这里的pDC为参数,在CEdit重画时还有效
      

  2.   

    CDC::GetDC 有一个作用域(具体作用域范围?)e文也行.
      

  3.   

    kongyunzhongque(云雀) 说的好像有道理,本人没有想过,不是太清楚
      

  4.   

    SetTextCharacterExtra
    The SetTextCharacterExtra function sets the intercharacter spacing. Intercharacter spacing is added to each character, including break characters, when the system writes a line of text. int SetTextCharacterExtra(
      HDC hdc,         // handle to DC
      int nCharExtra   // extra-space value
    );
    Parameters
    hdc 
    [in] Handle to the device context. 
    nCharExtra 
    [in] Specifies the amount of extra space, in logical units, to be added to each character. If the current mapping mode is not MM_TEXT, the nCharExtra parameter is transformed and rounded to the nearest pixel. 
    Return Values
    If the function succeeds, the return value is the previous intercharacter spacing. If the function fails, the return value is 0x80000000. Windows NT/2000/XP: To get extended error information, call GetLastError.Requirements 
      Windows NT/2000/XP: Included in Windows NT 3.1 and later.
      Windows 95/98/Me: Included in Windows 95 and later.
      Header: Declared in Wingdi.h; include Windows.h.
      Library: Use Gdi32.lib.
      

  5.   

    楼上给SetTextCharacterExtra的用法什么意思啊?我没问他的用法啊?晕?
      

  6.   

    pDC应该是对话框的DC指针,edit.GetDC()返回的是Edit控件的DC了!
    VC中的GDI操作,操作在什么地方主要还是看DC的类型和DC的所有者。
      

  7.   

    在vc.net中edit.GetDC()返回的是CDC指针.
    大哥都看看msnd才回答,不要乱说啊!
    我查过了也有人问过,只是没人解释过都是骗分的.所以我只好旧时从提了.
    如果pDC一直是对话框的DC指针的话,我在上面if内加入pdc->textout(1,1,"反");
    输出的字符怎么会在控件(1,1)位置显示呢?答案只有一个pdc是控件的.
    edit.GetDC()->textout(1,1,"反");的效果是一样的.请大家思考后回答.
    答对的分数任你要,前提斑竹不反对.
      

  8.   

    这到镇没有研究过。
    不过GetDC是获得的客户区的DC,每一次访问都不一定一样。OnCtlColor参数中的pDC在MSDN中的定义是:
    Contains a pointer to the display context for the child window. May be temporary.并不是客户区的DC,这可能就是差别所在。另外,pDC的句柄和GetDC得到的DC指针的句柄根本不一样,所以在操作上当然会有不同的结果。
      

  9.   

    如果楼主重载了OnCtlColor
    The framework calls this member function when a child control is about to be drawn. Most controls send this message to their parent (usually a dialog box) to prepare the pDC for drawing the control using the correct colors.
    Here pDC may be temporary
    不用pDC的话,可能就用系统默认的,结果
    (edit.GetDC())->SetTextCharacterExtra(20);的效果就看不出
      

  10.   

    而这个pDC实际上是跟消息一起派发的参数wParam中来的,也不知道是怎么获得的。
      

  11.   

    临时设备上下文只在得到上下文的函数(及子函数)中才能生存,而且属性的设置只有对窗体重画才有作用。
    GetDC方法获得的是与该窗口关联的临时的设备上下文对象,不能保存以供长期使用。而设备上下文句柄由windows系统保存着,可以随时从窗口句柄出发获得。这种临时的设备上下文对象在线程进入空闲消息处理时,由MFC自动清理。
      

  12.   

    kongyunzhongque(云雀)说的似乎在理,但是有一点在下不明:就算是临时对象在其有效范围内应该也其作用啊!为什么在上面的代码中用getdc设置控件的字体为什么会失败呢?这个是我提问的根源啊!望指教.
      

  13.   

    诚然:
    afx_msg HBRUSH OnCtlColor(
       CDC* pDC,
       CWnd* pWnd,
       UINT nCtlColor 
    );
    Parameters
    pDC 
    Contains a pointer to the display context for the child window. May be temporary.但是,它们并不是相同的东西!!!!

    TRACE("(%x,%x)",pDC,(m_editCtrl.GetDC()));
    就可以发现了。
      

  14.   

    你可以单步查看一下pDC和edit.GetDC()是否相同