我在利用组合框做一个小程序的时候,在使用SetItemData,GetItemData时,对m_DrawPattern变量不解,程序如下:
BOOL CComboDlg::OnInitDialog() 
{
CDialog::OnInitDialog();

    CString str[6]={"水平线","竖直线","向下斜线","向上斜线","十字线","交叉线"};
int nIndex;
for(int i=0;i<6;i++)
{
nIndex=m_Pattern.AddString(str[i]);
m_Pattern.SetItemData(nIndex,i);
}
m_Pattern.SetCurSel(0);
m_DrawPattern=0;//变量在此处 return TRUE;  // return TRUE unless you set the focus to a control
              // EXCEPTION: OCX Property Pages should return FALSE
}
void CComboDlg::OnSelchangePattern() 
{
int nIndex=m_Pattern.GetCurSel();
if(nIndex!=CB_ERR)
    m_DrawPattern=m_Pattern.GetItemData(nIndex);
Invalidate( );
}
}
void CComboDlg::OnPaint() 
{
CPaintDC dc(this); // device context for painting

         CWnd* pWnd=GetDlgItem(IDC_DRAW);
pWnd->UpdateWindow( );
CDC* pDC=pWnd->GetDC();
CBrush drawBrush;
drawBrush.CreateHatchBrush(m_DrawPattern,RGB(0,0,0));
CBrush* pOldBrush=pDC->SelectObject(&drawBrush); CRect rcClient;
pWnd->GetClientRect(rcClient);
pDC->Rectangle(rcClient);
pDC->SelectObject(pOldBrush);
// Do not call CDialog::OnPaint() for painting messages
}
m_DrawPattern是如何声明的,它是如何传入void CComboDlg::OnPaint() 中的。
为什么,我在编译时总有m_DrawPattern未定义的提示。

解决方案 »

  1.   

    定义成成员变量DWORD m_DrawPattern;btw:不是你写的代码吧??
      

  2.   

    是你自己写的程序吗?从程序来看这个变量应该是CComboDlg的一个成员变量。
      

  3.   

    根据匈牙利命名法得知m_DrawPattern是类的成员变量
    当你OnSelchangePattern的时候更改了m_DrawPattern并且使用Invalidate,Invalidate使窗口无效,引起OnPaint(),此时,m_DrawPattern已经传入了OnPaint()建议你看看MFC基础编程方面的书吧
      

  4.   

    m_DrawPattern是如何声明的?
    From your code ,I think m_DrawPattern is a member varible of CComboDlg.
    Or it is a global variable.
    Are you not declare this varible in your class?
    Or you copy the code of others ,or something others?