[CRichEditCtrl] GetCaretPos在OnKeyDown中不起作用 [英] [CRichEditCtrl] GetCaretPos not working in OnKeyDown

查看:58
本文介绍了[CRichEditCtrl] GetCaretPos在OnKeyDown中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我在CRichEditCtrl插入符号位置有严重问题.
这在常规EditCtrl中可以正常工作.

在OnKeyDown中,我想使用:: GetCaretPos
获得插入符号的位置 或EM_CHARFROMPOS.
在这种情况下,它总是返回上一个位置.
我想突出显示在Ultra Edit中应用的任意键按下时的焦点线.
下面的示例显示了编辑控件上的确切用法,并在WM_KEYDOWN中使用CWnd :: Default(). http://www.codeproject.com/Articles/2797/Hilite-Edit-View

在我的丰富编辑控件中,在GetCaretPos引起另一种副作用之前调用CWnd :: Default(). (突出显示的栏和插入符号不同).
我想在WM_KEYDOWN中获取当前的插入符号位置或字符索引,以绘制突出显示的栏.

Dear all,

I have a serious problem in CRichEditCtrl caret position.
This works fine in the general EditCtrl.

In OnKeyDown, I would like to get the caret position using ::GetCaretPos
or EM_CHARFROMPOS.
In this case, it always returns the previous position.
I would like to highlight the focused line when the any key is down as applied in the Ultra Edit.
The following example shows the exact usage on the edit control and uses CWnd::Default() in WM_KEYDOWN. http://www.codeproject.com/Articles/2797/Hilite-Edit-View

In my rich edit control, to call CWnd::Default() before GetCaretPos causes another side effect. (Highlighted bar and caret are different).
I would like to get the current caret position or character index in WM_KEYDOWN to draw the highlighted bar.

void CWinCommView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    POINT pt;
    int nCaretPos;

    // http://msdn.microsoft.com/en-US/library/ww9f7hst(v=vs.80).aspx
    // CWnd::Default();

    // Set current caret location.
    ::GetCaretPos(&pt);
    nCaretPos = SendMessage(EM_CHARFROMPOS, (WPARAM)0, (LPARAM)&pt);
    m_charRange.cpMin = nCaretPos;
    m_charRange.cpMax = nCaretPos;
    ...
}



而在OnLButtonDown中,CPoint如下显示插入符的确切位置.
在这种情况下,如果我们使用:: GetCaretPos(& pt)代替point,它还会指出先前的位置,如在OnKeyDown中一样.



Whereas in OnLButtonDown, CPoint shows exact caret position as follows.
In this case, if we use ::GetCaretPos(&pt) instead of point, it also point out the previous position as in OnKeyDown.

void CWinCommView::OnLButtonDown(UINT nFlags, CPoint point)
{
	POINT pt;
	int nCaretPos;

	pt.x = point.x;
	pt.y = point.y;

	nCaretPos = SendMessage(EM_CHARFROMPOS, (WPARAM)0, (LPARAM)&pt);
	m_charRange.cpMin = nCaretPos;
	m_charRange.cpMax = nCaretPos;

	...
}


您能给我一个准确的答案,以解决CRichEditCtrl中此插入符号的位置吗?

谢谢和问候,
Jinyup


Could you please give me an exact answer to solve this caret position in CRichEditCtrl?

Thanks and regards,
Jinyup

推荐答案

也许我遗漏了一些东西,但是我对您的帖子感到困惑.
GetCaretPos和使用EM_CHARFROMPOS发送消息中的返回值不相同.

顾名思义,GetCaretPos返回插入符号的位置.
使用EM_CHARFROMPOS发送消息会返回当前插入符号位置的字符索引.

所以你的声明:
在OnKeyDown中,我想使用:: GetCaretPos
获得插入符的位置. 或EM_CHARFROMPOS

尚不清楚.

在丰富的编辑控件中:
在WM_KEYUP和WM_KEYDOWN以及WM_LBUTTONDOWN和WM_LBUTTONUP处理程序中,插入符位置始终是当前的.
在WM_KEYDOWN和WM_LBUTTONDOWN处理程序中,GetCaretPos插入符位置返回尚未更新的当前插入符位置.
在WM_KEYUP和WM_LBUTTONUP处理程序中,GetCaretPos插入符位置还返回已更新的当前插入符位置.

这项工作与编辑控件中的工作方式完全相同.
为了解决您的问题:
请说明您要达到的目标:获得插入符号的位置或字符索引?
Maybe I am missing something but I am kind of confused by your post.
GetCaretPos and return value from Send message with EM_CHARFROMPOS are not the same.

GetCaretPos returns position of the caret as the name implies.
Send message with EM_CHARFROMPOS returns an index of the character from current caret position.

So your statement:
In OnKeyDown, I would like to get the caret position using ::GetCaretPos
or EM_CHARFROMPOS

Is unclear.

In rich edit control:
Caret position is always current in WM_KEYUP and WM_KEYDOWN as well as in WM_LBUTTONDOWN and WM_LBUTTONUP handlers.
In WM_KEYDOWN and WM_LBUTTONDOWN handlers GetCaretPos caret position returns current caret position that is not updated yet.

In WM_KEYUP and WM_LBUTTONUP handlers GetCaretPos caret position also returns current caret position that is updated.

This work exactly the same way in edit control.
In order to resolve your problem:
Please explain what are you trying to achieve: get caret position or character index?


这篇关于[CRichEditCtrl] GetCaretPos在OnKeyDown中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆