请问如何截获对CComboBox中的Edit 控件的鼠标消息和焦点转移消息(鼠标左/右键的单/双击)

shadowWind 2005-11-10 01:54:05
我想要通过程序修改CComboBox控件中当前文本的内容:如按别的button后,在CComboBox中光标所在位置输入某个字符。

可是无法得到CComboBox失去焦点之前的光标位置。

我试过在 CBN_KILLFOCUS 事件中用 GetEditSel 获取光标位置,但这个时候得到的值只会是0,(不管我原来在CComboBox中光标的状态如何。)

大家指点一下吧

多谢~
折磨我好几天了
:(
...全文
443 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
shadowWind 2005-11-11
  • 打赏
  • 举报
回复
多谢小三

我怎么没想到定时器呢,-_-!

lixiaosan 2005-11-10
  • 打赏
  • 举报
回复
如按别的button后,在CComboBox中光标所在位置输入某个字符。

void Cxxxdlg::OnButton1()
{
m_combo1.SetFocus();
m_pEdit->SetSel(m_nStart,m_nEnd);
}


lixiaosan 2005-11-10
  • 打赏
  • 举报
回复
改一下
void Cxxxdlg::OnButton1()
{
m_combo1.SetFocus();
CString str;
str.Format("%d, %d", m_nStart,m_nEnd);//这里改一下
AfxMessageBox(str);
}
lixiaosan 2005-11-10
  • 打赏
  • 举报
回复
一个解决办法,设置一个定时器

//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);
}


qc_jrj 2005-11-10
  • 打赏
  • 举报
回复
在 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();
goodboyws 2005-11-10
  • 打赏
  • 举报
回复
用((CEdit*)GetDlgItem())->ReplaceSel
lixiaosan 2005-11-10
  • 打赏
  • 举报
回复
你的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);

}
lixiaosan 2005-11-10
  • 打赏
  • 举报
回复
CEdit* pEdit = (CEdit*)m_combo.GetWindow(GW_CHILD);
pEdit->GetSel(...)
yoogle 2005-11-10
  • 打赏
  • 举报
回复
为什么一定要知道知道光标位置?是有好几个combox,而不清楚该去控制哪个么?

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧