15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


synchronizing scrolling in two CListViews

Todd C. Englund -- tenglund@sybase.com
Thursday, September 26, 1996

To: mfc-l
Subject: synchronizing scrolling in two CListViews

Environment: VC++ 4.2, WinNT 3.51

I have an application that displays two CListViews side
by side.  Both views always contain the same number of items,
and I would like to keep the vertical scroll position of both
views synchronized.

This can (almost) be done with an OnVScroll handler something
like this:

void CTestListView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar*
pScrollBar) 
{
   // TODO: Add your message handler code here and/or call default	
   CListView::OnVScroll(nSBCode, nPos, pScrollBar);

   POINT pt;
   m_pListCtrl->GetItemPosition(0, &pt);

   CBrowserDoc* pDoc = (CBrowserDoc*) GetDocument();
   ASSERT(pDoc != NULL);

   POSITION pos = pDoc->GetFirstViewPosition();
   while (pos != NULL)
   {
      CView* pView = pDoc->GetNextView(pos);
      if (pView->IsKindOf(RUNTIME_CLASS(CCompanionListView)))
      {
         CTestListView* pListView = (CCompanionListView *) pView;

         POINT otherpt;

         pListView->GetItemPosition(0, &otherpt);

         CSize sz;

         sz.cx = 0;
         sz.cy = otherpt.y - pt.y;

         pListView->Scroll(sz);
      }
   }
}

This OnVScroll handler looks for views that should be
synchronized and calls their Scroll() member function.
The Scroll() member simply calls Scroll() on the CListCtrl.

The one problem that I have found with this approach occurs
when one list view has a horizontal scrollbar and the other
does not.  A horizontal scrollbar reduces the height of the
list control so the scroll range and position are changed
slightly.  What I would like to do is force both views to
have a horizontal scrollbar when either requires one.
I thought I could add some logic to a CListView::OnSize()
handler that would determine if the list control's horizontal
scrollbar is visible, and if so, set it to visible in the
companion view.  The problem is that GetScrollBar(SB_HORZ)
always returns a NULL pointer when called for the list control.
Even if I could get a pointer to the scroll bar in the list
control, I suspect that there is no way to prevent the list
control from hiding it again.

Anyone have any idea how to take charge of a scroll bar in
a list control?  If not, perhaps there is another way to
synchronize the views that will handle this case?

Todd



Mario Contestabile -- Mario_Contestabile.UOS__MTL@UOSMTL2.universal.com
Monday, September 30, 1996

>by side.  Both views always contain the same number of items,
>and I would like to keep the vertical scroll position of both
>views synchronized.

An easy was of achieving this might simply be to determine which item in the 
lists
should be the first, and call 
CTreeCtrl::Select() with TVGN_FIRSTVISIBLE;

That way regardless of the view sizes, their first item is the same, and it 
manages
the rest of the items.

mcontest@universal.com





| Вернуться в корень Архива |