我想要通过程序修改CComboBox控件中当前文本的内容:如按别的button后,在CComboBox中光标所在位置输入某个字符。可是无法得到CComboBox失去焦点之前的光标位置。我试过在 CBN_KILLFOCUS 事件中用 GetEditSel 获取光标位置,但这个时候得到的值只会是0,(不管我原来在CComboBox中光标的状态如何。)大家指点一下吧多谢~
折磨我好几天了
:(

解决方案 »

  1.   

    为什么一定要知道知道光标位置?是有好几个combox,而不清楚该去控制哪个么?
      

  2.   

    CEdit* pEdit = (CEdit*)m_combo.GetWindow(GW_CHILD);
    pEdit->GetSel(...)
      

  3.   

    你的combobox的edit已经失去了焦点,怎么还能得到它的光标位置你可以测试以下代码以下代码在combobox的edit获得焦点时按下回车时响应
    BOOL CTest6Dlg::PreTranslateMessage(MSG* pMsg) { if( pMsg->message == WM_KEYDOWN ) {         switch( pMsg->wParam ) { case VK_RETURN: CEdit *pEdit = (CEdit*)m_combo1.GetWindow(GW_CHILD); if(pMsg->hwnd == pEdit->m_hWnd ) {
    int nStart, nEnd;
    pEdit->GetSel(nStart, nEnd);
    CString str;
    str.Format("%d, %d", nStart,nEnd);
    AfxMessageBox(str); } } }
    return CDialog::PreTranslateMessage(pMsg);}
      

  4.   

    用((CEdit*)GetDlgItem())->ReplaceSel
      

  5.   

    在 stdafx.h 中添加 #define WINVER  0x0500      /* version 5.0 */
    ComboBox 控件的 ID 为 TestCboList1 CWnd *pWndCbo = GetDlgItem( TestCboList1 );
    if (pWndCbo == NULL) return; HWND hWndCbo = pWndCbo->GetSafeHwnd(); COMBOBOXINFO ci;
    ci.cbSize = sizeof(ci);
    if ( !GetComboBoxInfo( hWndCbo, &ci ) ) return; CWnd wnd, *pWnd;
    pWnd = &wnd;
    pWnd->Attach( ci.hwndItem ); CPoint pt;
    pt = pWnd->GetCaretPos(); int iRet;
    iRet = pWnd->SendMessage( EM_CHARFROMPOS, (WPARAM) 0, MAKELPARAM(pt.x, pt.y) ); int iPos, iLine;
    iPos  = LOWORD( iRet ); // char pos of edit
    iLine = HIWORD( iRet ); // line no. of edit pWnd->SendMessage( EM_SETSEL, (WPARAM) iPos, (LPARAM) iPos );
    pWnd->SendMessage( EM_REPLACESEL, (WPARAM) FALSE, (LPARAM) "A" );
    pWnd->SendMessage( EM_SETSEL, (WPARAM) -1, (LPARAM) -1 ); pWnd->Detach();
      

  6.   

    一个解决办法,设置一个定时器//xxxdlg.h
    public:
    CEdit *m_pEdit;
    int m_nStart, m_nEnd;//xxxdlg.cpp
    Cxxxdlg::OnTimer(UINT nIDEvent) 
    {
    if( ::GetFocus() == m_pEdit->m_hWnd )
    pEdit->GetSel(m_nStart, m_nEnd); CDialog::OnTimer(nIDEvent);}BOOL Cxxxdlg::OnInitDialog()
    {
    ...
    m_pEdit = (CEdit*)m_combo1.GetWindow(GW_CHILD);
    SetTimer(1, 1, NULL);
    }void Cxxxdlg::OnButton1() 
    {
    m_combo1.SetFocus();
    CString str;
    str.Format("%d, %d", nStart,nEnd);
    AfxMessageBox(str);
    }
      

  7.   

    改一下
    void Cxxxdlg::OnButton1()
    {
    m_combo1.SetFocus();
    CString str;
    str.Format("%d, %d", m_nStart,m_nEnd);//这里改一下
    AfxMessageBox(str);
    }
      

  8.   

    如按别的button后,在CComboBox中光标所在位置输入某个字符。void Cxxxdlg::OnButton1()
    {
    m_combo1.SetFocus();
    m_pEdit->SetSel(m_nStart,m_nEnd);
    }