MFC:如何保存和恢复 CEditView 滚动位置以便正确滚动内容?

问题描述

由于需要对选项卡控件进行变通,我想保存然后恢复 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);
  }

解决方法

终于得到了一些有用的东西:

      // 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);
      }

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...