CEdit的派生类的OnLButtonDbClk()中,用SetSel()想高亮显示选中行,无效???? 哪里有问题呢? 谢谢

shadowWind 2005-01-24 08:36:51
1. 动态创建CMyEdit控件的代码:
pEdit = new CMyEdit;
pEdit->Create(WS_CHILD|WS_VISIBLE|WS_TABSTOP|ES_MULTILINE
|WS_VSCROLL,
CRect(10,10,100,100), this, 1);
pEdit->ModifyStyleEx(0, WS_EX_CLIENTEDGE, SWP_DRAWFRAME);

2. CMyEdit中被继承的函数
void CMyEdit::OnLButtonDblClk(UINT nFlags, CPoint point)
{
int iStart, iEnd;
CString m_sSelectedText, m_sEditText;
GetSel(iStart, iEnd);
GetWindowText(m_sEditText);

//get begin and end position of selected line (between two '\n')
int iLeft = m_sEditText.ReverseFind('\n');
int iRight = iLeft;
CString remainStr = m_sEditText.Left(iLeft);
while ( iLeft>iStart )
{
iRight = iLeft-1;
iLeft = remainStr.ReverseFind('\n');
remainStr = remainStr.Left(iLeft-1);
}

//set selected for current line
SetFocus();
SetSel(iLeft+1,iRight-iLeft-1); //这里无效,没有高亮显示整行。
//get text of selected line
m_sSelectedText = m_sEditText.Mid(iLeft+1,iRight-iLeft-1);//这里得到了整行的内容

CEdit::OnLButtonDblClk(nFlags, point);
}

???
谢谢
...全文
443 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
chuanke 2005-01-25
  • 打赏
  • 举报
回复
==========================================================================
把CEdit::OnLButtonDblClk(nFlags, point);//注释起来,另:
CEdit::SetSel
void SetSel( DWORD dwSelection, BOOL bNoScroll = FALSE );

void SetSel( int nStartChar, int nEndChar, BOOL bNoScroll = FALSE );

Parameters

dwSelection

Specifies the starting position in the low-order word and the ending position in the high-order word. If the low-order word is 0 and the high-order word is –1, all the text in the edit control is selected. If the low-order word is –1, any current selection is removed.

低位字指定起始位置,高位字为结束位置。如果低位为0,高位为-1,则编辑控件中的全部文本被选中;如果低位字为-1,则任何当前选定内容被去掉选定状态。

bNoScroll

Indicates whether the caret should be scrolled into view. If FALSE, the caret is scrolled into view. If TRUE, the caret is not scrolled into view.

指示是否显示脱字符是滚动可见的。如果值为FALSE,则显示,TRUE不显示。

nStartChar

Specifies the starting position. If nStartChar is 0 and nEndChar is –1, all the text in the edit control is selected. If nStartChar is –1, any current selection is removed.

指出当前选中部分的开始位置。如果nStartChar=0且nEndChar=-1,则编辑控件的文本被全选;如果nStartChar=-1,则任何当前选定内容被去掉选定状态。

nEndChar

Specifies the ending position.

指出结束位置。

Remarks

Call this function to select a range of characters in an edit control.

调用此成员函数在一个编辑控件中选择一定范围的字符。
shadowWind 2005-01-25
  • 打赏
  • 举报
回复
谢谢 tonyswe(tong) ( )

我太马虎了,Setsel那里参数写错了。

最后一行注释掉就好了。

解决了,结贴。
hongtog 2005-01-25
  • 打赏
  • 举报
回复
发现是最后一行在作怪,, 注释调就可以了..
CEdit::OnLButtonDblClk(nFlags, point);
hongtog 2005-01-25
  • 打赏
  • 举报
回复
哦,,错了,,是选了单词了..
hongtog 2005-01-25
  • 打赏
  • 举报
回复
楼主是不是想双击选中CEdit的某行呀,,
代码是否有误,,
改成
void SetSel( int nStartChar, int nEndChar, BOOL bNoScroll = FALSE );


int iLeft = m_sEditText.ReverseFind('\n');
int iRight = iLeft;
CString remainStr = m_sEditText.Left(iLeft);
if(iLeft<iStart)
iRight = m_sEditText.GetLength(); //

while ( iLeft>iStart )
{
iRight = iLeft-1;
iLeft = remainStr.ReverseFind('\n');
remainStr = remainStr.Left(iLeft-1);
}
//set selected for current line
SetFocus();

SetSel(iLeft+1,iRight); //这里无效,没有高亮显示整行。
shadowWind 2005-01-25
  • 打赏
  • 举报
回复
I_Love_CPP(我愛C++) ( ) :

不是的,CEdit自己默认的双击高亮显示的是当前选中的一个单词,而不是一整行
老夏Max 2005-01-25
  • 打赏
  • 举报
回复
不会是你在主界面中已经处理过双击消息的关系吧?呵呵,给个Sample我测试一下,xia_qingqi@broadxent.com.cn
fanqing 2005-01-25
  • 打赏
  • 举报
回复
既然楼上说没有就使用RichEdit
I_Love_CPP 2005-01-24
  • 打赏
  • 举报
回复
CEdit自己就有设置整行高亮的效果(北京为蓝色,字体为白色)。

16,470

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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