我用VC开发对话框程序
对话框上的文本框不是在设计时画上去的
而是在对话框启动时自动创建,有三个
他们的文本分别不同
他们获得焦点时响应不同的事件这怎么搞定?

解决方案 »

  1.   

    先获取对话机框的句柄,
    然后用createwindow创建文本框,
      

  2.   

    用createwindow创建文本框时,
    文本框的ID是多少?
    没有ID,我不知道怎么操作这个文本框
      

  3.   

    用CStatic::Create创建
    BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );创建时自己定义nID
      

  4.   

    如果自动创建的文本框的个数在运行时间决定
    我怎么能够自己定义nID呀
      

  5.   

    例如你在设计时资源ID号最大为ID_MAX,那就在动态创建控件时,用ID_MAX+1开始用^_^
      

  6.   

    //resource.h
    #define IDC_STATIC1                     1000
    #define IDC_STATIC2                     1001
    #define IDC_STATIC3                     1002//Test6Dlg.h
    public:
       CStatic  *m_pStatic[3];protected:
    //{{AFX_MSG(CTest6Dlg)
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon(); afx_msg void OnStatic1();
             afx_msg void OnStatic2();
             afx_msg void OnStatic3();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()//Test6Dlg.cpp
    CTest6Dlg::~CTest6Dlg()
    {
         for( int i = 0 ; i < 3 ; i++ )
    delete m_pStatic[3];
    }BEGIN_MESSAGE_MAP(CTest6Dlg, CDialog)
    //{{AFX_MSG_MAP(CTest6Dlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_STATIC1, OnStatic1)
             ON_BN_CLICKED(IDC_STATIC2, OnStatic2)
             ON_BN_CLICKED(IDC_STATIC3, OnStatic3)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    BOOL CTest6Dlg::OnInitDialog()
    {
    CDialog::OnInitDialog();
    for(int i = 0; i < 3; i++)
    {
    m_pStatic[i] = new CStatic;
    ASSERT_VALID( m_pStatic[i] );
    }
    m_pStatic[0]->Create( _T("你"), WS_CHILD | WS_VISIBLE | WS_TABSTOP, 
      CRect(0, 0, 90, 20), this, IDC_STATIC1 ); 
    m_pStatic[1]->Create( _T("好"), WS_CHILD | WS_VISIBLE | WS_TABSTOP, 
      CRect(0, 0, 120, 20), this, IDC_STATIC2 ); 
    m_pStatic[2]->Create( _T("啊"), WS_CHILD | WS_VISIBLE | WS_TABSTOP, 
      CRect(0, 0, 140, 20), this, IDC_STATIC3 );  return TRUE;  // return TRUE  unless you set the focus to a control
    }
    void CTest6Dlg::OnStatic1() 
    {
    // TODO: Add your control notification handler code here

    }
    void CTest6Dlg::OnStatic2() 
    {
    // TODO: Add your control notification handler code here

    }
    void CTest6Dlg::OnStatic3() 
    {
    // TODO: Add your control notification handler code here

    }