editor控件中,如何能在鼠标点击后知道点击在哪一行?
在edit的父窗体中重载PreTranslateMessage,假设编辑框为IDC_EDIT3
BOOL CTestView::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message == WM_LBUTTONDOWN)
{
CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT3);
if(pEdit->m_hWnd == pMsg->hwnd)
GetLineIndexOfEdit(pMsg->lParam);
}
return CFormView::PreTranslateMessage(pMsg);
}
然后编写函数
void CTestView::GetLineIndexOfEdit(DWORD pos)
{
CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT3);
WORD xPos = LOWORD(pos); // horizontal position of cursor
WORD yPos = HIWORD(pos); // vertical position of cursor
CPoint pt(xPos,yPos);
int ret = pEdit->CharFromPos(pt);
int charindex = LOWORD(ret);//这个是点击位置的字符在整个文本中的序号
int lineindex = HIWORD(ret);//这个就是行号
}
用这些函数
1. BOOL GetCursorPos(LPPOINT lpPoint);
2. int CharFromPos( CPoint pt ) const;
3. int LineFromChar( int nIndex = -1 ) const;
EM_CHARFROMPOS
EM_LINEFROMCHAR