Device coordinates and logical coordinates

Source: Internet
Author: User

Sticktoprogram

Device coordinates and logical coordinates

 

Windows applications use a logical unit for drawing graphics. The size of each logical unit is determined by the ing mode, this logical unit can be the same as the unit of the device (a pixel on the screen or printer) or a physical unit (such as millimeters ), it can also be a user-defined unit. In Windows applications, the ing mode is used whenever it is related to the output. The purpose of this article is to help readers understand some basic knowledge about the ing mode and propose solutions to some problems that often occur during use.

I. Basic knowledge about ing Modes
When a Windows application draws a graph in its customer zone, it must provide the location in the customer zone. The location is represented by the X and Y coordinates, the X represents the X coordinate, and the Y represents the Y coordinate. Among all the GDI plot functions, these coordinates use a "logical unit ". When the GDI function sends the output to a physical device, Windows converts the logical coordinate to the device coordinate (such as the pixel of the screen or printer ). The conversion of logical coordinates and device coordinates is determined by the ing mode. The ing mode is stored in the device environment. The getmapmode function is used to obtain the current ing mode from the device environment. The setmapmode function is used to set the ing mode of the device environment.

1.Logical coordinates

The logical coordinate is independent of the device. It is independent of the device point size. Using logical units is the basis for achieving "WYSIWYG. When a programmer calls a line-drawn GDI function lineto to draw a line with a length of 25.4 (1 inch), he does not need to consider the device to output. If the device is a VGA display, Windows automatically converts it to 96 pixels. If the device is a 300 dpi laser printer, Windows automatically converts it to pixels.

2.Device coordinates

Windows maps the logical coordinates specified in the GDI function to the device coordinates. In all the device coordinate systems, the unit is subject to the pixel, and the horizontal value increases from left to right, the vertical value increases from top to bottom.

Windows includes the following three device coordinates to meet different needs:

(1) Customer Region coordinates, including the customer region of the application. The upper left corner of the customer region is (0, 0 ).

(2) screen coordinates, including the entire screen. The upper left corner of the screen is (0, 0 ). Screen coordinates are used in wm_move messages (for non-subwindows) and the following Windows functions: createwindow and movewindow (both for non-subwindows), getmessage, getcursorpos, getwindowrect, windowfrompoint, and setbrushorg. Using the clienttoscreen and screentoclient functions, you can convert the client region to the screen region coordinate, or vice versa.

(3) window coordinates, including the entire window of a program, including the title bar, menu, scroll bar, and window box. The upper left corner of the window is (0, 0 ). You can use the getwindowdc window device environment to convert the logical unit into window coordinates.

3.Conversion between logical coordinates and device coordinates

The ing method defines how Windows maps the logical coordinates specified in the GDI function to the device coordinates. To continue with the discussion on the ing method, we will introduce some terms related to the ing mode in Windows: we call the coordinate system where the logical coordinates are located as "window" and the coordinate system where the device coordinates are located as "viewviews ".

The "window" depends on logical coordinates. It can be a pixel, millimeter, or other scales that the programmer wants.

The "viewport" depends on the device coordinates (pixels ). Generally, the viewport is the same as the customer region. However, if a programmer uses getwindowdc or createdc to obtain a device environment, the view can also refer to the full window coordinates or screen coordinates. Point (0, 0) is the upper left corner of the customer area. The value of X increases to the right, and the value of Y increases upwards.

For all ing modes, Windows uses the following two formulas to convert the window coordinates into the visual interface coordinates:

Xviewport = (XWindow-xwinorg) * (xviewext/xwinext) + xvieworg

Yviewport = (ywindow-ywinorg) * (yviewext/ywinext) + yvieworg

(XWindow, ywindows) is the logic point to be converted, (xviewport, yviewport) is the device point after conversion. If the device coordinates are client Region coordinates or full window coordinates, windows must convert these coordinates into screen coordinates before drawing an object.

The two formulas use the points that specify the source of the window and the view respectively: (xwinorg, ywinorg) is the source of the window of the logical coordinate; (xvieworg, yvieworg) is the source of the view of the device coordinate. In the default device environment, both vertices are set to (0, 0), but they can be changed. This formula means that the logical points (xwinorg, ywinorg) are always mapped to the device points (xvieworg, yvieworg ).

Windows can also convert the coordinates of the view (device) to the window (logical) coordinates:

XWindow = (xviewport-xvieworg) * (xwinext/xviewext) + xwinorg

Ywindow = (yviewport-yvieworg) * (ywinext/yviewext) + ywinorg

You can use the dptolp and lptodp functions provided by windows to convert the device coordinates and logical coordinates.

4.Types of ing Modes

Windows defines eight ing methods listed in Table 1.

The above ing mode can be divided into the following three categories:

Unique record location

X-axis Addition

Y axis Addition

Milli meters

Mm_text

Image prime point

Right

Lower

It is related to the backup

Mm_lometric

0. 1mm

Right

Upper

0.1

Mm_himetric

0. 01mm

Right

Upper

0.01

Mm_loenglish

0. 254

Right

Upper

0.254

Mm_hienglish

0. 0254

Right

Upper

0.0254

Mm_twips

0.0176

Right

Upper

0.0176

Mm_isotropic

Ren Yi (x = y)

Optional

Optional

Configurable

Mm_anisotropic

Ren Yi (X! = Y)

Optional

Optional

Configurable


Mm_text ing mode. This ing mode is called "text" ing mode. It is not because it is most suitable for text, but because the orientation of the axis is consistent with that of the read text. Windows provides the setviewportorg and setviewworg functions to set the source point of the view and window. You can change the default window and view origins at (). The default window and view range are () and cannot be changed.

Measure ing methods: mm_lometric, mm_himetric, mm_loenglish, mm_hienglish, and mm_twips map one logical unit to a fixed actual unit, of which 1 twip is equal to 0.0176mm (1/1440 inch ). For physical units corresponding to other ing modes, see table 1. After the ing mode is set, Windows automatically sets the window and the window range. The range value is not important, but the range ratio is a fixed value. For mm_lometric, windows calculates the number of points in the horizontal pixel ratio of xviewext/xwinext = 1mm.

The custom mming modes mm_isotropic and mm_anisotropic allow programmers to set their own window and view range. The difference between mm_isotropic and mm_anisotropic is that the range of the configured X axis and Y axis must be the same, while the range of the X axis and Y axis set by mm_anisotropic can be different. Isotropi means "in all directions", and anisotropic means the opposite. In the custom ing mode, the origin and range of the window and the view can be changed, and the programmer can set the desired ing mode. The setmediawext and setviewportext functions are used to change the range of the window and view. The following code maps a logical unit to 0.396mm (1/64 inch ).

Setmapmode (HDC, mm_isotropic); (not supported by wince)

Setmediawext (64, 64 );

Setviewportext (HDC, getdevicecaps (HDC, logpixelsx), getdevicecaps (HDC, logpixelsy ));

Ii. solutions to problems related to the ing mode

In practice, programmers may encounter problems related to the display mode. For example, how to set the ing mode in oleserver and how to reduce the error of mutual conversion between logical coordinates and device coordinates. Next, I will discuss the solutions to these two problems.

1. oleserverHow to set the ing mode in

During oleserver application development, if the programmer directly calls the setmapmode function to set the ing mode to one of the measurement ing modes, the program runs normally on Windows95/98, however, the size of the object displayed on WindowsNT is smaller than that on the border. After my research, we found that oleserver on WindowsNT should use the logical inch ing method. Before discussing how to set the ing mode based on logical inches, we will first introduce the concept of logical inches.

In Windows, the unit is "logical Inch". The logical inch is larger than the actual one. If a Windows program uses an actual inch, the normal 10-pound text on the display is almost unrecognizable, so Windows uses a magnified "logical Inch" to represent the text. The logical inch only affects display, but does not affect printing.

You can use the getdevicecaps function to obtain various capabilities of the current device. The first parameter nindex indicates the type of information to be obtained. When nindex is horzsize and vertsize, the width and height of the display area can be obtained. When nindex is horzres and vertres, the number of records in each horizontal and vertical direction can be obtained, that is, the resolution; when the nindex value is logpixelsx and logpixelsy, the number of records per logical inch in the horizontal and vertical directions can be obtained.

After introducing the knowledge of logical inches, it is easy to set oleserver to a ing mode based on logical inches. If the programmer only calls setmapmode (HDC, mm_loenglish) to set the ing mode, the current ing mode is physical inches instead of logical inches. To set the logical inch, you must customize the range of the window and the viewport so that xviewext/xwinext = points of the horizontal pixel in the 0.01 logical inch. When xviewext = logpixelsx, xwinext = 100, the ratio meets the preceding requirements.

The following code sets the ing mode.

Intxlogpixperinch = getdevicecaps (HDC, logpixelsx );
Intylogpixperinch = getdevicecaps (HDC, logpixelsy );
Setmapmode (mm_anisotropic );
Setmediawext (100,100 );
Setviewportext (xlogpixperinch, ylogpixperinch );

In the above Code, the setmapmode function is called to set the ing mode to custom. The call must be prior to the call of setmediawext and setviewportext; otherwise, the setting will be invalid.
The above Code actually sets the ing mode to the logic mm_loenglish. If the programmer needs to set the logic mm_lometric, mm_himetric, mm_hienglish, or mm_twips, he only needs to modify the setwindowext parameter in the above Code, this parameter is actually the number of units per inch in various ing modes. Based on the ing mode parameters in Table 1, you can obtain the number of logical units corresponding to each inch in table 2.

For example, to set the logic mm_twips, the parameter in setmediawext is 1440.

2.Error Handling During logical coordinate and device coordinate transformation

Table 2

Ing mode

Number of unique parts per inch

Mm_loenglish

100

Mm_hienglish

1000

Mm_lometric

254

Mm_himetric

2540

Mm_twips

1440


When we set the ing mode to mm_lometric based on a logical inch, the window range is set to 256, and the window range is set to 96 (the value of logpixelsx under the VGA display ), about 2.6 logical units correspond to 1 pixel, which will obviously cause a small error, which will be manifested in all aspects of the application: A part of the customer zone is not refreshed; there is no spacing between objects, but there is a gap. Objects in different positions on the screen will be reduced or increased by a pixel.

You can take the following two steps to avoid conversion errors. (1) try to select a ing mode in which the window range and the window range ratio can be divisible. For example, the mm_twips based on the Logical inch can simplify the window range to 1440/96, there is no error when converting device coordinates to logical coordinates. From the perspective of eliminating errors, mm_twips is better than other ing modes. (2) When the window range and the window range cannot be divisible, try to simplify it. For example, when a logical unit is mapped to 1/0.3900 in 64 inch mm, the ratio of the window range to the window range is 64/96, which can be simplified to 2/3. If we set the value of the logical unit to a multiple of 2 and the value of the device unit to a multiple of 3, there will be no loss of precision after the conversion.

To sum up, if we can take the logical coordinates and the device coordinates as multiples of the simplified window and the window range values based on the characteristics of the ing mode values, there will be no error in the conversion between the logical coordinate and the device coordinate.

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.