Mfc GetTextExtentPoint32(m_hAttribDC、lpszString、nCount和大小))

Mfc GetTextExtentPoint32(m_hAttribDC、lpszString、nCount和大小)),mfc,Mfc,我的程序是在图形上显示一些点及其位置(x,y)。当我用鼠标拖动任何一点时,它的位置会自动改变。更新的位置按照以下代码实现(使用线程): 同时: void CChartLabel<PointType>::SetLabelText(const TChartString& strText) { m_strLabelText = strText; m_pParentCtrl->RefreshCtrlAtw(); } 你能帮我解决这个问题吗?我试图找到一些

我的程序是在图形上显示一些点及其位置(x,y)。当我用鼠标拖动任何一点时,它的位置会自动改变。更新的位置按照以下代码实现(使用线程):

同时:

void CChartLabel<PointType>::SetLabelText(const TChartString& strText)  
{  
   m_strLabelText = strText; 
   m_pParentCtrl->RefreshCtrlAtw();
}

你能帮我解决这个问题吗?我试图找到一些解决方法,但没有成功(

我的答案只是一个猜测,但原因可能是在第二个线程中使用一个线程(创建者)的MFC对象。这是一个猜测,因为您没有告诉我们断言说了什么以及您使用的VS版本

问题是:在MFC中创建某些对象时,句柄值保存在一个映射中,该映射允许MFC仅使用句柄值查找对象。 此句柄映射存储在每个线程中

此外,如果窗口对象存储了与这些句柄映射关联的其他对象,则来自另一个线程的使用将失败


因此,答案可以在调用堆栈中找到。它告诉您是谁使用了这样的对象。而导致问题的对象只是通过断言来确定的。

您可能希望进一步扩展您的问题。看起来您并没有投入太多的工作。如果您不愿意投入工作来解释和澄清您的问题,h你怎么能指望其他人在回答你的问题时投入精力呢?我制作了一个图表来显示许多点及其位置信息。当我用鼠标拖动这些点时,它们显示的信息也会相应地改变。但在这一行中发生了错误:GetTextExtentPoint32(m_hAttribDC,lpszString,nCount,&size))。我试图调试,但无法解决问题。
ASSERT
s总是准确地告诉您问题所在。在这种情况下,您可能是在
CWnd
派生类上调用函数,而没有首先在窗口上调用
Create
!你能告诉我更多关于Create的信息吗?并举一个简单的例子?在afxwin1.inl的639行是什么?那会有帮助的。我正在使用VisualStudio2010。ASSERT发生在这个函数中:_AFXWIN_INLINE CSize CDC::GetTextExtent(LPCTSTR lpszString,int nCount)const{ASSERT(m_hAttribDC!=NULL);SIZE SIZE;VERIFY(::GetTextExtentPoint32(m_hAttribDC,lpszString,nCount,&SIZE));//ASSERT返回SIZE;}查看堆栈跟踪。那么对GetTextExtentPoint32的调用失败了?或者之前的任何一行?如果GettextExtentPoint32失败,原因一定与我建议的不同。可能字符串参数或大小值无效。如果您之前的任何行没有有效的DC!
void CChartLabel<PointType>::SetLabelText(const TChartString& strText)  
{  
   m_strLabelText = strText; 
   m_pParentCtrl->RefreshCtrlAtw();
}
void CChartCtrl::RefreshCtrlAtw()
{
// Window is not created yet, so skip the refresh.
if (!GetSafeHwnd())
    return;
if (m_iEnableRefresh < 1)
{
    m_bPendingRefresh = true;
    return;
}

// Retrieve the client rect and initialize the
// plotting rect
CClientDC dc(this) ;  
CRect ClientRect;
GetClientRect(&ClientRect);
m_PlottingRect = ClientRect;        

// If the backgroundDC was not created yet, create it (it
// is used to avoid flickering).
if (!m_BackgroundDC.GetSafeHdc() )
{
    CBitmap memBitmap;
    m_BackgroundDC.CreateCompatibleDC(&dc) ;
    memBitmap.CreateCompatibleBitmap(&dc, ClientRect.Width(),ClientRect.Height()) ;
    m_BackgroundDC.SelectObject(&memBitmap) ;
}

// Draw the chart background, which is not part of
// the DrawChart function (to avoid a background when
// printing).
DrawBackground(&m_BackgroundDC, ClientRect);
ClientRect.DeflateRect(3,3);
DrawChart(&m_BackgroundDC,ClientRect);
for (int i=0; i<4 ;i++)
{
    if (m_pAxes[i])
        m_pAxes[i]->UpdateScrollBarPos();
}

Invalidate();
_AFXWIN_INLINE CSize CDC::GetTextExtent(LPCTSTR lpszString, int nCount) const
{
    ASSERT(m_hAttribDC != NULL);
    SIZE size;
    VERIFY(::GetTextExtentPoint32(m_hAttribDC, lpszString, nCount, &size));
    return size;
}
_AFXWIN_INLINE CSize CDC::GetTextExtent(const CString& str) const
{
    ASSERT(m_hAttribDC != NULL);
    SIZE size;
    VERIFY(::GetTextExtentPoint32(m_hAttribDC, str, (int)str.GetLength(), &size));
    return size;
}