Từ 1 tới 2 trên tổng số 2 kết quả

Đề tài: [VC] vertical scroll bar và Horizontal scroll bar

  1. #1
    No Avatar
    htqh Khách

    Mặc định [VC] vertical scroll bar và Horizontal scroll bar

    Cho em hỏi làm sao sử dụng những cái này được nhỉ?(có thể gắn nó với 1 static text không ?)

  2. #2
    No Avatar
    lychailon Khách

    Creating a Scrolling View Window
    As the lack of scroll bars in Ex05a and Ex05b indicates, the MFC library CView class, the base class of CEx05bView, doesn't directly support scrolling. Another MFC library class, CScrollView, does support scrolling. CScrollView is derived from CView. We'll create a new program, Ex05c, that uses CScrollView in place of CView. All the coordinate conversion code you added in Ex05b sets you up for scrolling.

    The CScrollView class supports scrolling from the scroll bars but not from the keyboard. It's easy enough to add keyboard scrolling, so we'll do it.

    A Window Is Larger Than What You See
    If you use the mouse to shrink the size of an ordinary window, the contents of the window will remain anchored at the top left of the window and items at the bottom and/or on the right of the window will disappear. When you expand the window, the items will reappear. You can correctly conclude that a window is larger than the viewport that you see on the screen. The viewport doesn't have to be anchored at the top left of the window area, however. Through the use of the CWnd functions ScrollWindow and SetWindowOrg, the CScrollView class allows you to move the viewport anywhere within the window, including areas above and to the left of the origin.

    Scroll Bars
    Windows makes it easy to display scroll bars at the edges of a window, but Windows by itself doesn't make any attempt to connect those scroll bars to their window. That's where the CScrollView class fits in. CScrollView member functions process the WM_HSCROLL and WM_VSCROLL messages sent by the scroll bars to the view. Those functions move the viewport within the window and do all the necessary housekeeping.

    Scrolling Alternatives
    The CScrollView class supports a particular kind of scrolling that involves one big window and a small viewport. Each item is assigned a unique position in this big window. If, for example, you have 10,000 address lines to display, instead of having a window 10,000 lines long, you probably want a smaller window with scrolling logic that selects only as many lines as the screen can display. In that case, you should write your own scrolling view class derived from CView.

    The OnInitialUpdate Function
    You'll be seeing more of the OnInitialUpdate function when you study the document-view architecture, starting in Chapter 15. The virtual OnInitialUpdate function is important here because it is the first function called by the framework after your view window is fully created. The framework calls OnInitialUpdate before it calls OnDraw for the first time, so OnInitialUpdate is the natural place for setting the logical size and mapping mode for a scrolling view. You set these parameters with a call to the CScrollView::SetScrollSizes function.

    Accepting Keyboard Input
    Keyboard input is really a two-step process. Windows sends WM_KEYDOWN and WM_KEYUP messages, with virtual key codes, to a window, but before they get to the window they are translated. If an ANSI character is typed (resulting in a WM_KEYDOWN message), the translation function checks the keyboard shift status and then sends a WM_CHAR message with the proper code, either uppercase or lowercase. Cursor keys and function keys don't have codes, so there's no translation to do. The window gets only the WM_KEYDOWN and WM_KEYUP messages.

    You can use the Class View's Properties window to map all these messages to your view. If you're expecting characters, map WM_CHAR; if you're expecting other keystrokes, map WM_KEYDOWN. The MFC library neatly supplies the character code or virtual key code as a handler function parameter.

    The Ex05c Example: Scrolling
    The goal of Ex05c is to make a logical window 20 centimeters wide by 30 centimeters high. The program draws the same ellipse that it drew in the Ex05b project. You could edit the Ex05b source files to convert the CView base class to a CScrollView base class, but it's easier to start over with the MFC Application Wizard. The wizard generates the OnInitialUpdate override function for you. Here are the steps:

    Run the MFC Application Wizard to create Ex05c. Use the wizard to generate an SDI program named Ex05c in the \vcppnet subdirectory. Set the CEx05cView base class to CScrollView, as shown here.


    Add the m_rectEllipse and m_nColor data members in Ex05cView.h. Insert the following code using the Add Member Variable Wizard available from the Class View's Properties window or by typing inside the CEx05cView class declaration:

    private:
    CRect m_rectEllipse;
    int m_nColor;

    These are the same data members that were added in the Ex05a and Ex05b projects.

    Modify the MFC Application Wizard–generated OnInitialUpdate function. Edit OnInitialUpdate in Ex05cView.cpp as shown here:

    void CEx05cView::OnInitialUpdate()
    {
    CScrollView::OnInitialUpdate();
    CSize sizeTotal(20000, 30000); // 20 by 30 cm
    CSize sizePage(sizeTotal.cx / 2, sizeTotal.cy / 2);
    CSize sizeLine(sizeTotal.cx / 50, sizeTotal.cy / 50);
    SetScrollSizes(MM_HIMETRIC, sizeTotal, sizePage, sizeLine);
    }
    Use the Class View's Properties window to add a message handler for the WM_KEYDOWN message. The code wizards available from the Properties window generate the member function OnKeyDown along with the necessary message map entries and prototypes. Edit the code as follows:

    void CEx05cView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    switch (nChar) {
    case VK_HOME:
    OnVScroll(SB_TOP, 0, NULL);
    OnHScroll(SB_LEFT, 0, NULL);
    break;
    case VK_END:
    OnVScroll(SB_BOTTOM, 0, NULL);
    OnHScroll(SB_RIGHT, 0, NULL);
    break;
    case VK_UP:
    OnVScroll(SB_LINEUP, 0, NULL);
    break;
    case VK_DOWN:
    OnVScroll(SB_LINEDOWN, 0, NULL);
    break;
    case VK_PRIOR:
    OnVScroll(SB_PAGEUP, 0, NULL);
    break;
    case VK_NEXT:
    OnVScroll(SB_PAGEDOWN, 0, NULL);
    break;
    case VK_LEFT:
    OnHScroll(SB_LINELEFT, 0, NULL);
    break;
    case VK_RIGHT:
    OnHScroll(SB_LINERIGHT, 0, NULL);
    break;
    default:
    break;
    }
    }
    Edit the constructor and the OnDraw function.Change the MFC Application Wizard–generated constructor and the OnDraw function in Ex05cView.cpp as follows:

    CEx05cView::CEx05cView() : m_rectEllipse(0, 0, 4000, -4000)
    {
    m_nColor = GRAY_BRUSH;
    }

    void CEx05cView::OnDraw(CDC* pDC)
    {
    pDC->SelectStockObject(m_nColor);
    pDC->Ellipse(m_rectEllipse);
    }
    These functions are identical to those used in the Ex05a and Ex05b projects.

    Map the WM_LBUTTONDOWN message and edit the handler. Make the following changes to the generated code:

    void CEx05cView::OnLButtonDown(UINT nFlags, CPoint point)
    {
    CClientDC dc(this);
    OnPrepareDC(&dc);
    CRect rectDevice = m_rectEllipse;
    dc.LPtoDP(rectDevice);
    if (rectDevice.PtInRect(point)) {
    if (m_nColor == GRAY_BRUSH) {
    m_nColor = WHITE_BRUSH;
    }
    else {
    m_nColor = GRAY_BRUSH;
    }
    InvalidateRect(rectDevice);
    }
    }
    This function is identical to the OnLButtonDown handler in the Ex05b project. It calls OnPrepareDC as before, but something is different. The CEx05bView class doesn't have an overridden OnPrepareDC function, so the call goes to CScrollView::OnPrepareDC. That function sets the mapping mode based on the first parameter to SetScrollSizes, and it sets the window origin based on the current scroll position. Even if your scroll view were to use the MM_TEXT mapping mode, you'd still need the coordinate conversion logic to adjust for the origin offset.

    Build and run the Ex05c program. Check to be sure that the mouse hit logic is working even if the circle is scrolled partially out of the window. Also check the keyboard logic. The output should look like this:

Các đề tài tương tự

  1. Xử lý sự kiện scroll của Trackbar trong C#?
    Gửi bởi tuanvi261 trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 0
    Bài viết cuối: 10-11-2012, 09:51 PM
  2. ? Làm scroll bar cho nhiều khung hiển thị ntn?
    Gửi bởi hyeb trong diễn đàn Thắc mắc lập trình ASP.NET
    Trả lời: 6
    Bài viết cuối: 02-05-2012, 08:56 PM
  3. Cách sử dụng thanh cuốn dọc - Vertical Scroll Bar
    Gửi bởi usercdcv trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 8
    Bài viết cuối: 10-06-2011, 07:51 AM
  4. Bắt sự kiện Mouse Scroll Up và Down
    Gửi bởi itkttn trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 1
    Bài viết cuối: 02-12-2010, 04:18 PM
  5. Hỏi về cách tạo dialog box động với vertical scroll bar
    Gửi bởi anhtung trong diễn đàn Thắc mắc lập trình Visual C++
    Trả lời: 0
    Bài viết cuối: 08-06-2010, 05:15 PM

Quyền hạn của bạn

  • Bạn không thể gửi đề tài mới
  • Bạn không thể gửi bài trả lời
  • Bạn không thể gửi các đính kèm
  • Bạn không thể chỉnh sửa bài viết của bạn