请问如何能够实现类似Editbox,listctrl,listbox这类控件的滚动条重画!是重画该控件还是重画scorll

解决方案 »

  1.   

    自画 SCROLLBAR, 而且是自己建的SCROLLBAR,而不是CWND内建的SCROLLBAR.First you have to instantiate a couple of CScroll bars in your main frame:class CMainFrame : public CFrameWnd {
    protected:
      CScrollbar m_wndHScroll;
      CScrollBar m_wndVScroll;
      ???
    };Then, create the windows in the usual way, when your frame is created:int CMainFrame::OnCreate(...)
    {
      ???
      CRect rc(0,0,0,0);
      VERIFY(m_wndSBHorz.Create(WS_VISIBLE|WS_CHILD|SBS_HORZ,
        rc, this, AFX_IDW_HSCROLL_FIRST));
      VERIFY(m_wndSBVert.Create(WS_VISIBLE|WS_CHILD|SBS_VERT,
        rc, this, AFX_IDW_HSCROLL_FIRST+1));
      VERIFY(m_wndSBBox.Create(WS_VISIBLE|WS_CHILD|SBS_SIZEBOX,
        rc, this,-1));
      return 0;
    }Oh, I forgot to mention: you also have to create a scroll bar box, the little thingy that goes where the horizontal and vertical scroll bars meet, so you can size your window and avoid a blank spot.
    Once you've created the scroll bars, there are several things to take care of. You have to size them, process notifications, and get the view to use them. The last part is easy. CScrollView provides a virtual function, GetScrollBarCtrl, which you can override.CScrollBar* CPictureView::GetScrollBarCtrl(int nBar) const
    {
      CWnd* pParent = GetParent();
      UINT nID = AFX_IDW_HSCROLL_FIRST;
      if (nBar==SB_VERT)
        nID++;
      return (CScrollBar*)pParent->GetDlgItem(nID);
    }This gets the appropriate control from the parent using the convenient IDs AFX_IDW_HSCROLL_FIRST and AFX_IDW_HSCROLL_FIRST+1.
    What about scroll notifications? When the user scrolls, the scroll bar sends a WM_HSCROLL or WM_VSCROLL to its parent, and it's up to you to do something. Well, fortunately, you're in luck because if you put your scroll bars in the frame as I have suggested, CFrameWnd already has OnHScroll and OnVScroll handlers that route WM_HSCROLL and WM_VSCROLL to the view:// From winfrm.cpp 
    void CFrameWnd::OnHScroll(...) 
    {  
      CWnd* pActiveView = GetActiveView();
      if (pActiveView != NULL) {
        pActiveView->SendMessage(WM_HSCROLL, ...);
      }
    }If the view is a scroll view—that is, if you have derived your view from CScrollView—it does the scrolling. You don't have to do anything to handle scroll messages; MFC does it automatically!
    What about sizing? That's where you have to do a little work. To make everything fly, you must manage the positions of the scroll bars whenever your frame is resized. Remember, the scroll bars are children of the frame, not the view. Normally you'd handle sizing in OnSize, but for frame windows the place to do it is in CFrameWnd::RecalcLayout.void CMainFrame::RecalcLayout(BOOL)
    {
      CFrameWnd::RecalcLayout();
      CView* pView = GetActiveView();
      if (pView) {
        // adjust view and scrollbars...
      }
    }The details are a tedious exercise in pixel arithmetic; Figure 3 reveals all. I modified the ImgView program from last month's column to produce the ugly blue scroll bars in Figure 4. You can get the code from the link at the top of this article.
    Figure 4 Blue Scroll BarsBefore I leave the topic of scroll bars, I should point out that post Microsoft? Internet Explorer 4.0 versions of ComCtrl32.dll support something called "flat scroll bars." There's a special API you can use to initialize and create these scroll bars without the 3D-look. For more info, see "Flat Scroll Bars" in the User Interface Design and Development section of the Platform SDK ocumentation.http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/CommCtls/FlatSB/FlatSB.aspWhether you use scroll bar controls as I've shown here, or flat scroll bars, you still can only set the background color of the scroll bar, so I question what value all this has. How useful is it to change only the background color? Most apps that have custom-colored scroll bars have custom-colored thumb slider and buttons as well. For that you have to write your own scroll bar control from scratch. But really, a scroll bar is pretty simple. All it does is let the user drag a box, send messages like WM_HSCROLL and WM_VSCROLL, and handle others like SB_PAGEUP and SB_THUMBPOSITION. It's not that hard. Another possible change to a simpler option is to use a normal scroll bar and override WM_ PAINT. I personally have never tried this, but in theory it might work. If anyone has ever been brave enough to try, let me know.... Happy programming!