请问如何截获对CComboBox中的Edit 控件的鼠标消息和焦点转移消息(鼠标左/右键的单/双击) 我想要通过程序修改CComboBox控件中当前文本的内容:如按别的button后,在CComboBox中光标所在位置输入某个字符。可是无法得到CComboBox失去焦点之前的光标位置。我试过在 CBN_KILLFOCUS 事件中用 GetEditSel 获取光标位置,但这个时候得到的值只会是0,(不管我原来在CComboBox中光标的状态如何。)大家指点一下吧多谢~折磨我好几天了:( 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 为什么一定要知道知道光标位置?是有好几个combox,而不清楚该去控制哪个么? CEdit* pEdit = (CEdit*)m_combo.GetWindow(GW_CHILD);pEdit->GetSel(...) 你的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);} 用((CEdit*)GetDlgItem())->ReplaceSel 在 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(); 一个解决办法,设置一个定时器//xxxdlg.hpublic: CEdit *m_pEdit; int m_nStart, m_nEnd;//xxxdlg.cppCxxxdlg::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);} 改一下void Cxxxdlg::OnButton1(){m_combo1.SetFocus();CString str;str.Format("%d, %d", m_nStart,m_nEnd);//这里改一下AfxMessageBox(str);} 如按别的button后,在CComboBox中光标所在位置输入某个字符。void Cxxxdlg::OnButton1(){m_combo1.SetFocus();m_pEdit->SetSel(m_nStart,m_nEnd);} 真心求OutputDebugString怎么用?求你耐心。谢谢 关于ADO调用其他数据库存储过程的问题 发送用户名密码到一个网站,大家有可用的代码么,谢谢 书中示例出现的错误,怎样修改最好??? AfxMessageBox的问题 关于dll调试的问题 怎样在WIN32工程里(不在mfc工程中)用TRACE宏 MFC下怎么使用STL? 两个有关于列表栏控件的问题! 如何定制这样的CFileDialog对话框,请高手指定,高分奉送!急!!! 在COM/ATL中,经常提到的宿主文件是什么意思? 如何创建一个没有地址栏的IE窗口
pEdit->GetSel(...)
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);}
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();
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);
}
void Cxxxdlg::OnButton1()
{
m_combo1.SetFocus();
CString str;
str.Format("%d, %d", m_nStart,m_nEnd);//这里改一下
AfxMessageBox(str);
}
{
m_combo1.SetFocus();
m_pEdit->SetSel(m_nStart,m_nEnd);
}