【问题标题】:MFC: How do I save and restore the CEditView scroll position so contents are correctly scrolled?MFC:如何保存和恢复 CEditView 滚动位置以便正确滚动内容?
【发布时间】:2021-07-18 21:41:22
【问题描述】:

如果需要解决选项卡控件的问题,我想保存然后恢复 CEditView 内容。我所做的如下所示,当滚动条和位置回到正确的位置时,编辑控件的实际内容不会滚动(它在左上角,就像它从未滚动过一样)。我试过RedrawWindow(),没用。我一直主要处理水平滚动条,我只是简单地点击水平条,内容会跳转到它应该在的位置。

我错过了什么或者让它发挥作用的诀窍是什么?

TIA!!

  // hack to work around ceditview getting its window contents changed
  CView* pview=GetView(tabi);
  if (pview->IsKindOf(RUNTIME_CLASS(CEditView))) {
    CEditView* peditview=reinterpret_cast<CEditView*>(pview);
    CEdit& editctrl=peditview->GetEditCtrl();
    int startchar, endchar;
    editctrl.GetSel(startchar, endchar);
    int nhorzpos=peditview->GetScrollPos(SB_HORZ);
    int nvertpos=peditview->GetScrollPos(SB_VERT);
    CString strexistingtext;
    pview->GetWindowText(strexistingtext);
    // change label
    tabctrl.SetTabLabel(tabi, strlabel);
    // put back text
    pview->SetWindowText(strexistingtext);
    // put back where we were
    peditview->SetScrollPos(SB_VERT, nvertpos);
    peditview->SetScrollPos(SB_HORZ, nhorzpos);
    editctrl.SetSel(startchar, endchar, TRUE);
  }
  else {
    // change label
    tabctrl.SetTabLabel(tabi, strlabel);
  }

【问题讨论】:

  • 我看到有EM_LINELENGTH,我看到EM_GETFIRSTVISIBLELINE,但我看不到我可以在哪里获得水平位置(除了可能需要根据水平滚动条进行某种类型的数学运算)。

标签: winapi mfc cedit


【解决方案1】:

终于找到了有用的东西:

      // hack to work around ceditview getting its window contents changed
      CView* pview=GetView(tabi);
      if (pview->IsKindOf(RUNTIME_CLASS(CEditView))) {
        CEditView* peditview=reinterpret_cast<CEditView*>(pview);
        CEdit& editctrl=peditview->GetEditCtrl();
        int startchar, endchar;
        editctrl.GetSel(startchar, endchar);
        int topline=editctrl.GetFirstVisibleLine();
        int nhorzpos=peditview->GetScrollPos(SB_HORZ);
        CString strexistingtext;
        pview->GetWindowText(strexistingtext);
        // change label
        tabctrl.SetTabLabel(tabi, strlabel);
        // put back text
        pview->SetWindowText(strexistingtext);
        // put back where we were
        editctrl.SetSel(startchar, endchar, TRUE);
        peditview->SetScrollPos(SB_HORZ, nhorzpos); // prevents ficker
        editctrl.SendMessage(WM_HSCROLL, MAKEWPARAM(SB_THUMBPOSITION, nhorzpos), NULL);
        editctrl.SendMessage(WM_HSCROLL, MAKEWPARAM(SB_ENDSCROLL, 0), NULL);
        editctrl.LineScroll(topline,0);
      }
      else {
        // change label
        tabctrl.SetTabLabel(tabi, strlabel);
      }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-17
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多