Windows mapping mode

xiaoxiao2021-03-06  13

Windows mapping mode

I_love_cpp

First, mapping mode

This is a primary topic, but it seems that many people have not figured it, because almost a few days, they will send this. Someone asked me this question half a year, I wanted to write this article, but I haven't think it didn't have time (because in addition to learning, work and play, there is almost not much empty time). This article is now actually only the processing of "mapping mode" in "Programming Windows", writing to our Chinese readers more easy to understand; I will gradually add some related content.

First, from a very simple but very common function: BOOL TEXTOUT (HDC HDC, // Handle to DCINT NXSTART, // X-Coordinate Of Starting Position Int Nystart, // Y-Coordinate Of Starting Position LPCTSTR LPSTRING, / / CHARACTER STRING INT CBSTRING / / NUMBER OF CHARACTERS; here (NxStart, NyStart) is the starting coordinate point displayed by the string, where the coordinates are "logical coordinates". In order to eventually display the text on the device (screen), Windows must "translate these logical coordinates" into "device coordinates" - actually "Pixels". Windows defines eight mapping patterns, as shown in the following table:

-------------------------------------------------- --------------------------------- Increasing Value Mapping Mode Logical Unit X-AXIS Y-AXIS

MM_TEXT Pixel Right Down MM_LOMETRIC 0.1 mm Right Up MM_HIMETRIC 0.01 mm Right Up MM_LOENGLISH 0.01 in. Right Up MM_HIENGLISH 0.001 in. Right Up MM_TWIPS 1/1440 in. Right Up MM_ISOTROPIC Arbitrary (x = y) Selectable Selectable MM_ANISOTROPIC Arbitrary (x! = Y ), SELECTABLE SELECTABLE ----------------------------------- ---------------------------------------Table 1

Note: In. Table inch; Windows default mapping mode is mm_text, actually the equipment coordinate! ! !

Take hot iron, look at the example:

Example: If we don't use SetMapMode to set a Windows mapping mode, Windows will use the default mm_text mapping mode, Textout (HDC, 8, 16, Text ("Hello"), 5);

The result is that the text will output text from left to right from left to right from left to the left boundary from the client zone.

and

Setmapmode (HDC, MM_LOENGLISH);

Textout (HDC, 50, -100, Text ("Hello"), 5);

The result will be from left to right from left to right from left to right from left to the left boundary 50 * 0.01 in. = 0.5 in. = 0.5 in.

But why is TEXTOUT's third parameter? Because the default value of the coordinates in the upper left corner of the client area is always (0, 0) (later, it will be said in detail), and the above list is not written? Under mm_loenglish, the direction in which the Y-axis value is incremented is up, that is Said down, of course, the third parameter at this time is negative.

Windows setting these mapping mode is more convenient to allow users to use it, so you have selected the right mapping mode to simplify your work; or choose MM_ISOTROPIC, mm_anisotropic to set the logical coordinates unit, the value is incremented; or simply use MM_Text (equivalent to using device coordinates), you need to get the necessary information with getDevicecap when you need to zoom it.

Second, coordinate original

Often asked users to ask CDC :: SetWindoworg and CDC :: SetViewPortorg, they are essentially encapsulated GDI functions setWindoworgex and SetViewportorgex:

The 9CBS explanation is:

The SetWindoworgex Function Specifies Which Window Point Maps to The Viewport Origin (0). The SetViewportorgex Function Specifies Which Device Point Maps to the window ORIGIN (0).

Beginners tend to fog water.

In fact, "mapping" in "Mapping Mode" is "mapping" in mathematics, a correspondence or a mathematical expression. This expression is:

Formula 1: XViewPort = (XWindow - Xwinorg) × KX XVIEWORG YVIEWPORT = (YWINDOW - YWINORG) × KY YVIEWORG

Equity Formula 2:

XWindow = (XViewPort - XVieworg) / KX Xwinorg Ywindow = (YViewPort - YVIEWORG) / KY YWINORG

Among them, Xwinorg, Ywinorg, (XVieworg, YVieworg) represent the origin of logical coordinates and device coordinates, and the default is (0,0);

(XWindow, ywindow) represents the logical coordinate point, (XViewPort, YViewPort) means that the corresponding device coordinate point (that is, in the current mapping mode, Windows will be imaged (XViewPort, YviewPort). ); Constant KX, KY is the scaling coefficient (described later), and the zoom factor in different mapping mode is different.

Therefore, in the default, (xwinorg, yvious) = (xVieworg, yvieworg) = (0, 0), formula is actually:

XViewPort = xwindow / kx yviewport = ywindow / ky.

Therefore, it can be seen that the mapping pattern is actually set a ratio, there is nothing amazing!

example:

Setmapmode (HDC, MM_Text); // Under the scales of scale kx = ky = 1 under mm_text;

Setwindoworgex (HDC, 100, 100, NULL); // This time (xwinorg, ywinorg) is assigned (100, 100);

// and (XVieworg, YVieworg) remains the same as (0, 0).

Then the formula is:

XViewPort = xwindow -100 yviewport = ywindow -100

therefore

Textout (HDC, 520, 530, Text ("IloveYou, Mygirl"), 15); // (xWindow, Ywindow) = (520, 530)

The device will be displayed on the device from the (520, 530) - (100, 100) = (220, 230) points.

The SETVIEWPORTORGEX is the value used to set (XVieworg, YVIEWORG), and the usage method is similar to SETWINDOWORGEX. As can be seen from the formula, in mm_text,

Setwindoworgex (HDC, X, Y, NULL);

versus

SetViewPortorgex (HDC, - X, - Y, NULL);

equivalent.

Third, zoom factor

Scaling coefficient kx = xViewext / xwinext ky = yviewext / ywinext

XViewExt, YViewExt), and (xwinext, ywinext) can be obtained from GetViewPortext and getWindowExtex, respectively.

Under mm_text, kx = ky = 1; other mapping mode KX, KY is constant, which can be calculated by the following table (Window NT, Display device: 1024x768 Pixels): ----------- -------------------------------------------------- ------------------------- Mapping Mode ViewPort Extens (x, y) Window Extents (x, y) mm_lometric (1024, -768) (3200 , 2400) mm_himetric (1024, -768) (32000, 24000) mm_loenglish (1024, -768) (1260, 945) mm_hienglish (1024, -768) (12598, 9449) mm_twips (1024, -768) (18142, 13606) ) ------------------------------------- -----------------------------------

Table 2 Windows NT Extens (1024 x 768)

references:

MSDN by Microsoft

"Visual C technology insider" by David J. Kruglinski et al

"Programming Windows" 5 TH Edition, by Petzoldi

~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~ The end

This person is scattered, and if there is any improper in this article, please let me know. Thank you.

转载请注明原文地址:https://www.9cbs.com/read-47360.html

New Post(0)