1

以下片段来自多行 CEdit 控件的 OnChange() 处理程序,该控件设置了“WantReturn”。

void DLG::OnChangeEditPrepareTape() 
{
    CString ss;
    std::vector<char> aTape;
    m_prepareTape.GetWindowText(ss);
    m_prepareTape.SetWindowText(ss);
}

如果 SetWindowText() 被注释掉,用户的文本会在右边建立,一切都很好。但是,随着它的进入,文本插入点移动到左边缘,并且用户的字符进入现有字符的左侧。

我想在两个调用之间添加一些修改文本,并且可以通过子类化 CEdit 来获得我想要的东西。但我很想知道 Get() 和 Set() 是否有办法做到这一点。

我正在使用带有 Service Pack 5 的 Visual C++ 6。现在已经 11 岁了,但正如他们所说的那样,“软件不会磨损”:-)。

4

2 回答 2

2

The insertion point is reset by SetWindowText() because, from the control's point of view, its whole text content has just been reset (possibly to the empty string), and both the insertion point and the current selection might not be meaningful enough to keep them around.

You can use GetSel() and SetSel() to implement this behavior yourself:

void DLG::OnChangeEditPrepareTape() 
{
    CString ss;
    std::vector<char> aTape;

    int start, end;
    m_prepareTape.GetSel(start, end);
    m_prepareTape.GetWindowText(ss);

    // Tinker with the text...

    m_prepareTape.SetWindowText(ss);
    m_prepareTape.SetSel(start, end);
}
于 2011-03-13T11:32:06.117 回答
1

You can you use GetSel to retrieve the cursor position before you replace the text, and SetSel to place it in the same location afterwards.

void DLG::OnChangeEditPrepareTape() 
{
    CString ss;
    int start, stop;
    std::vector<char> aTape;
    m_prepareTape.GetWindowText(ss);
    m_prepareTape.GetSel(&start, &stop);
    m_prepareTape.SetWindowText(ss);
    m_prepareTape.SetSel(start, stop);
}

If you modify the text before you put it back in the text box, you can increment or decrement start (and end) accordingly.

于 2011-03-13T11:28:11.580 回答