MFC allows document/View Structure programs to support scroll bars

Source: Internet
Author: User

To display a document through the scroll bar, you must also know where the document is rolled. Once you drag the scroll bar, you must tell the view to change in the document.

. All of these are done by the csrollview class of MFC. What programmers need to do is:

1. derive your own view class from the cscrollview class to support the scroll bar

2. Provide document size, determine the rolling range and set the Initial Value

3. Coordinate document location and screen coordinates

1. View class derived from cscrollview class

When using Appwizard to create an application framework, you can select the cscrollview class as the base class of the View class in step 1. In addition

You can also set it manually: in the header file and resource file of View class * view, search for cview and replace it with cscrollview.

2. Determine the scroll range and set the Initial Value

When creating a tumble window, you must set the total size of the customer window. You can use the cwnd member function setscrollsizes () to set the ruler of the rolling window.

Inch. This operation must be completed before the customer window is created. Therefore, you must reload the oninitialupdate () function of the View class. The following code:

Void cch10demo3view: oninitialupdate ()
{
Cscrollview: oninitialupdate ();
Size size = {3000,1500 };
Setscrollsizes (mm_text, size ); /// the maximum area of the scrolling window ///////////////////////////////// ////
}

3. Coordinate document location and screen coordinates

In Windows, the document coordinate system becomes the logical coordinate system, and the view coordinate system becomes the device coordinate system. After the view is rolled

Click the coordinates to obtain the device (View) coordinates of the mouse position. When using this data processing document (such as the dot line ),

It is converted to the coordinate of the document. When you call the MFC plotting function, Windows automatically converts the document to view coordinates and then draws.

CDC provides two member functions: lptodp () and dptolp () to complete the conversion. In this example

In the function, the obtained coordinates are device coordinates. Therefore, you must convert them to document coordinates using dptolp. The coordinate of the document in the ondraw () function

The system automatically converts the data to view coordinates for plotting.

In addition, the view coordinates and document coordinates also have the origin matching problem. If the view scroll bar does not scroll, the origin is overlapped.

Otherwise, we need to adjust the View coordinates. The cscrollview class provides a function cscrollview: onpreparedc ()

The member function to match the context coordinate origin of the view device.

You need to add the following code in the mouse and button message response:

Cclientdc DC (this); // get the client window DC
Onpreparedc (& DC); // match the coordinate origin
DC. dptolp (& Point); // convert the view coordinate to the document

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.