Window, view, and screen display details-basic computer plotting skills

Source: Internet
Author: User

This article from: http://anony3721.blog.163.com/blog/static/511974201132210856781/

 

Window: a small part of the logical environment. It is a rectangular box. The world coordinate system is a logical coordinate. setworg (x, y) sets the logical coordinate points (x, y) of the window) the device point mapped to the device environment (0, 0 ).
Device environment: displays, printers, and so on. The coordinate system is the device coordinate system, which is downward on the positive y axis, right on the positive X axis, and the origin is in the upper left corner. It remains unchanged and cannot be modified! The negative Half Axis of X and Y is virtual and cannot be displayed or printed.

View: A part of the device environment, with a rectangular frame. The coordinate system is the same as the device environment. Setviewportorg (X, Y) is used to set the coordinate origin of the rectangle frame of the viewport. The default value is the coordinate origin of the device.A viewport is a proportional image (projection) of a window ).

 1. The appearance of windows, sdks, and device Environments

The window and the view actually describe the same objective scenario. The difference between them is that the units of the two coordinate systems are different. The device environment is the display device. The scenario in the window and the view must be correctly displayed to the device environment in the ing mode. By default, the origin of the window coordinates, the origin of the viewport coordinates, and the device points in the Windows GDI drawing overlap at the device point (0, 0. The device environment origin of Windows GDI is located in the upper left corner of the screen. The device point is always located at (0, 0), the X axis is right, and the Y axis is up; the window coordinates used by OpenGL are different from those used by windowsgdi. OpenGL locates at the center of the screen by default. The X axis is right and the Y axis is up. The source of the view is in the lower left corner of the screen.

The view and window are characterized by the same actual geometric object, so a window must have a view, and a window must have a window. A window or a view can be understood as an object seen by the eyes, such as a ball. The display area (display screen) of the device environment can be understood as the aspect ratio of the computer screen is different, the final display result of the actual object on the computer screen. For example, the ball on the display screen looks like an elliptical shape. The coordinate values used in almost all GDI functions are logical units, that is, the actual size of an object is large. In Windows, you must convert the logical unit to "device unit"-pixels, that is, the size of an object after it is displayed in the device environment (display screen or printer. This conversion is controlled by the ing mode, the origin of the window and the view, and the range of the window and the view.
Different ing methods can be used to change the position of the window origin in the logical unit and make the object realistic in the device environment, you can also move an object to the display screen in a pixel-based viewport coordinate system. No matter what changes are made to the window and the origin of the view, the device point is (0, 0) always in the upper left corner of the customer zone. Both are used to make objects realistic on the display screen. You can adjust the source of the window and the source of the view to achieve the same effect.

The viewport is a rectangular area related to the device. The coordinate unit is related to the device. The intuitive movement of the coordinates of the source of the viewport is the movement of the DC. The coordinates of the window are logical coordinates and are irrelevant to the device. The origin of the window coordinate always corresponds to the origin of the window coordinate. For the same image, the length and width of the region expressed by the window coordinate system are different from those expressed by the coordinate system of the view, because the unit is different. The two define the proportional relationship between the two coordinate systems. During program plotting, the coordinates used are always window coordinates, while the actual display or output devices have their own coordinates. For example, some printer devices have different horizontal and vertical resolutions, and their pixels are actually rectangles. Write a program to draw a circle, if no coordinate conversion, the output on the printer is an elliptic.
 Ondraw has previously called the onpreparedc function in MFC to set the ing mode for you. By default, the ing mode is mm_text, that is. To change the default ing mode, reload onpreparedc to reset its own ing mode. To improve the drawing accuracy, you often need to change the ing mode.

2. Key Points of window and view

It has always been difficult for beginners to understand the window and the view. I used to be confused. However, I have only been able to fully understand the window and View window after I have been studying it in depth recently. By figuring out this, we can better use different coordinate laser modes and make our drawing easier:

First, you must know that the coordinate origin of the window and the view is always the same point, and the content in the window and the view is the same. The device coordinate (Display) displays different content based on the changes in the view or window origin. In mm_text ing mode, the content in the third quadrant of the world coordinate system is the content in the First quadrant of the window coordinate system, so there are setviewportorg (x, y) and set1_worg (-X,-y) with the same effect. This is hard to understand, but it is a fact. The "device environment" means that the display is always in the First quadrant of xoy in the left-hand coordinate system. At the beginning, the origin of the window coordinates, the origin of the window coordinates, and the device points overlap in the upper left corner of the customer area. In order to display the object on the real screenSetworg (x, y) moves X units to the left of the screen and Y units to the top of the screen. Setviewportorg (X, Y) is to move X units to the right of the screen and Y units to the bottom of the screen.

The key points are summarized as follows:

1. The view is related to the display device. The view is equivalent to the customer area and the device coordinates are used. A viewport is a rectangular area equivalent to a window, and its X axis is right and Y axis down.

2. The window has nothing to do with the display device. The window is in the same area as the view, but its X axis is right and Y axis is up using the logical coordinate.

3. A window uses a different coordinate system, but the origin of the two coordinate systems is always the same. However, the coordinates of the vertex (whether the origin of the viewport or the origin of the window coordinate) are not set to (0, 0 ). A window is a view. Let's just move the view while the two are in different coordinate systems, and vice versa.
4. the position of the source of the view (that is, the initial position of the brush DC) is only moved by the setviewportorgex (x, y) function. (X, Y) is relative to the device coordinate in the upper left corner of the customer zone, that is, the pixel), while setworg (x, y) sets the logical coordinate points (x, y) of the window) device point mapped to the device environment (0, 0)

It's easy to get setviewportorg and setworg confused, but the distinction between them is actually quite clear. changing the viewport origin to (x, y) with setviewportorg tells windows to map the logical point (0, 0) to the device point (x, y ). changing
The window origin to (x, y) with setworg does essential tially the reverse, telling windows to map the logical point (x, y) to the device point (0, 0) -the upper left corner of the display surface. in the mm_text Mapping mode, the only real difference
The two functions is the signs of X and Y. In other mapping modes, there's more to it than that because setviewportorg deals in device coordinates and setworg deals in logical coordinates.
<Programing windows with MFC>

DC always starts to draw from the origin of the world coordinate system. The origin of the paint brush is the origin of the canvas and the origin of the world coordinate system. UseSetviewportorg (x, y) and setviewworg (-X,-y)Move the origin of the world coordinate system to the (x, y) Point in the environment coordinate system of the device.Setworg (-X,-y) indicates the logical point (-X,-y) of the DC drawing at the device point (), and the logical point) (x, y) points in the device coordinate system in mm_text ing mode.

1) to move the source of a view is like moving the brush DC. If you set the source of the view to (xvieworg, yvieworg), which is equivalent to moving the DC brush, the logical point is (0, 0) it will be mapped to the device point (xvieworg, yvieworg). The initial DC drawing is written under the logical point (0, 0), and the drawing will be written in the customer area (device point) (xvieworg, yvieworg) draw for the center. In mm_text ing mode, setviewportorg (150,100); the DC plot coordinate origin is moved from (150,100) to the lower right ). You can see that the setviewportorg () function can change the coordinate origin of the device context DC.

Void cdemoview: onpaint ()
{
Cpaintdc DC (this); // device context for painting
DC. setviewportorg (150,100 );
Cpen penblue;
Penblue. createpen (ps_solid, 1, RGB (0, 12, 255 ));
DC. SelectObject (& penblue );
DC. ellipse (-100,-100,100,100 );
}

2) moving the window origin is like moving the canvas. If you change the window origin to (xwinorg, ywinorg), the logical point (xwinorg, ywinorg) it will be mapped to the device points (150,100), set0000worg (150,100); the logical point () corresponds to the device point (); no matter what changes are made to the window and the viewport origin, it should be noted that the first device point () is always the upper left corner of the customer zone, and the second window is the same point as the source of the viewport.

Void cdemoview: onpaint ()
{
Cpaintdc DC (this); // device context for painting
DC. set0000worg (-150,-100 );
Cpen penblue;
Penblue. createpen (ps_solid, 1, RGB (0, 12, 255 ));
DC. SelectObject (& penblue );
DC. ellipse (-100,-100,100,100 );
// Ondraw (& DC );
}
5. Understand the coordinate conversion formula between the window and the window:
Xviewport = (XWindow-xwinorg) * xviewext/xwinext + xvieworg;
Yviewport = (ywindow-xwinorg) * yviewext/ywinext + yvieworg;
This formula does not seem easy to understand at first, and the deformation is as follows:
(Xviewport-xvieworg)/(XWindow-xwinorg) = xviewext/xwinext;
(Yviewport-yvieworg)/(ywindow-xwinorg) = yviewext/ywinext;
This makes it easy to understand: the ratio of the logical coordinate unit to the device coordinate unit, that is, the proportional factor.
6. When the defined customer region is greater than the window created by the program, a scroll bar is required to display the complete customer region.
7. process the tumble window: If the tumble window is not rolled, the coordinates of the source of the corresponding window and the viewport in the upper left corner of the customer area are (0, 0) by default. If the window is rolled, X pixels are rolled to the right in the horizontal direction, and y pixels are rolled down in the vertical direction. In this case, the upper left corner of the customer region is considered to be the window origin (also the origin of the view, the coordinates of the window origin (also the source of the View) remain unchanged at (), but only the position of the window origin in the environment coordinate system of the device. The current scenario is the content displayed in the device environment (Display). The Coordinate System of the device environment will never move. Use setviewportorgex
(X, y) to move the origin of the world coordinate system to the (x, y) Position of the display. The two moves have the same result:
The origin of the window coordinate always corresponds to the same point (the origin of the world coordinate system), and the origin of the New World coordinate system is exactly (x, y) in the device environment ). After debugging the csrollview function in MFC, it is found that it uses setviewportorgex
(X, y) The function moves the origin of the world coordinate system to implement "window scrolling ".
8. The coordinate origin (whether it is a viewport or a window) is not equalThe zero coordinate point (0, 0) is always the upper left corner of the customer zone.(Must understand ).
9. The coordinates of the view can be moved freely, but the zero point is always in the upper left corner of the customer area.
10. The origin of the window can be moved at will. The window is similar to a single photo and is only the current scene. The viewport is similar to the film, and records all the information from the past to the present.

To better describe the real world in a computer, a logical coordinate system must be set up in line with the real world. Of course, Windows provides usDevice coordinatesAndLogical coordinatesIng interface.

The coordinate system ing interfaces provided by Windows include:
Setmapmode (INT nmapmode) sets the ing mode. Windows provides eight ing modes.
Setdomainworg (int x, int y)/setdomainworg (point) is used to set the origin of the window coordinate corresponding to the origin of the viewport coordinate, windows requires that the coordinates of the view must correspond to the coordinates of the window. The units of X, Y, and point are logical units.
Setviewportorg (int x, int y)/setviewportorg, windows requires that the coordinates of the view must correspond to the coordinates of the window. The units of X, Y, and point are device units, that is, pixels.
Setw.wext (int cx, int CY)/setw.wext (size) set the window size range in the logical coordinate system. The unit of Cx, Cy, and size is the logical unit.
Setviewportext (int cx, int CY)/setviewportext (size) sets the size range of the video ports in the device coordinate system. The unit of Cx, Cy, and size is the device unit, that is, pixel.

3. instructor Sun Xin's explanation of misplacement of images

When we click the left mouse button in the window, we get the device coordinates (680,390). In mm_text ing mode, the logical coordinates are equal to the device coordinates, therefore, the coordinates of the Point saved by the collection class are in pixels and the coordinate value is (680,390 ). Before calling the ondraw function, the onpaint function calls the onpreparedc function. The onpreparedc function calls setviewportorgex () to adjust the attributes of the display context and set the origin of the view to (0, -150). In this way, the origin of the window, that is, the logical coordinate (150), will be mapped to the device coordinate (0, because the GDI function uses logical coordinates, and when the image is displayed, Windows needs to convert the logical coordinates to the device coordinates. Therefore, the originally stored coordinate point (680,390) (used as the logical coordinate in the GDI function), according to the conversion formula

Xviewport = XWindow-xwinorg + xvieworg

And yviewport = ywindow-ywinorg + yvieworg,

The X coordinate of the device point is 680-0 + 0 = 680, And the Y coordinate of the device point is 390-0 + (-150) = 240, so we can see that the image appears above the original display area.

Solution description

First, after drawing the image, call the onpreparedc function before saving the coordinate point to adjust the attributes of the display context and set the origin of the view to (0,-150, the origin of the window, that is, the logical coordinate (150), will be mapped to the device coordinate (0,-680,390). Then we call the dptolp function to convert the device coordinate () to the logical coordinate, the formula for converting the device coordinate to the logical coordinate is as follows:
XWindow = xviewport-xvieworg + xwinorg,
Ywindow = yviewport-yvieworg + ywinorg. The X coordinate of the logical point is 680-0 + 0 = 680, And the Y coordinate is 390-(-150) + 0 = 540, save the logical coordinates (680,540). When the window is re-painted, the onpreparedc function is called to adjust the display context attributes and set the origin of the view to (0,-150 ), then, the GDI function uses the logical coordinate point (680,540) to draw the image, which is converted to the device coordinate point (680,390) by windows, which is the same as the device point when the image was originally displayed, of course, the image is still displayed in the original place.

 

From the windows mouse message, you can obtain the current coordinate value of the mouse pointer (point. X and point. Y). The coordinate value is the device coordinate.
Many MFC library functions, especially the crect member functions, can only work under the device coordinate.
In addition, we sometimes need to use physical coordinates. The concept of physical coordinates is the actual size of the real world.
How to convert the device coordinates-logical coordinates-physical coordinates becomes a problem we need to consider. Physical coordinates and logical coordinates must be done by ourselves, however, Windows provides functions to help us convert logical coordinates and device coordinates.

The CDC lptodp function can convert the logical coordinate to the device coordinate.
The dptolp function of CDC can convert the device coordinates into logical coordinates.

The following lists the coordinates we should remember:

◎ All member functions of cdc take logical coordinates as parameters
◎ All the member functions of cwnd take the device coordinates as parameters.
◎ The region definition uses device coordinates
Device coordinates should be considered for all selected test operations.
◎ Values that need to be used for a long time are saved using logical or physical coordinates. The device coordinates change due to the rolling change of the window.

Related Article

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.