Lesson 5: Equipment Scene
First, what is a device scene
With regard to equipment scene, it is quite, and some books say that it is a device environment, showing scenes, and more common called device descriptors or device descriptors. Of course, how do you love to call with you, I still like to say as the device scene. So what is the device scene? The device scene is a Windows object, and Windows is a graphical environment, and its graphics system is difficult to confidently flexibly and powerful. In essence, all drawings under Widnows are made through the device scene, not directly to the window and the device itself. In order to illustrate the equipment scene, many books take some practical life to comparison. Among them, the most common is to make it a painter in painting. I think everyone has seen how painters draw, at least, painters painted on the movie or on the billboard. We can imagine: There is a beautiful Baiyun Mountain (I am a name), a painter holds a palette, and the other hand holds a brush, facing a picture board is writing the scenery. painting. Some books believe that the painter's palette is equivalent to the equipment scene, and some books think that the drawing board is equivalent to the equipment scene, saying that In view of this situation, I think it is still directly to explain the device scene.
As an object of Windows, the device scene is actually a data structure within Windows. Like both the PointAPI data structure has the two properties of X and Y, the device scenario has its own properties, just more attributes, as follows:
Equipment scenario attribute
BACKGROUND MODE (White) Background Mode (Opaque) Bitmap (Bitmap) None (NONE) Brush White Brush Brush Starting Point (Brush Origin) 0,0 CLIPPING Region Entire Window Or Device Surface Parameters (DEFAULT PALETTE) Pain Position 0,0 Drawing Mode (Drawing Mode) R2_COPYPEN Font (Font) System Font Fonts (MAPPING MODE) MM_TEXT Brush (PEN) Black Multilateral Pluncher Mode ALTERNATE BLACKWHITE BLACKWHITE text Color (Text) Color Black Viewport Starting Point (Viewport Extents) 1,1 Window Origin 0,0 Window Extents 1,1
Please see this table, which attributes are in the equipment scene, and there should be an impression in your mind. In fact, many attributes of the device scene correspond to the properties of forms or controls such as FORM, PICTUREBOX, TEXT in VB. For example, font, background color, drawing mode, etc. It is conceivable that many friends who learn VB do not know what is the equipment scene, but it is essentially unknowingly used equipment scenes. It can be said that the device scenario is one of the most important concepts in Windows programming. For equipment scenes, some friends may not understand it for a moment, this is natural, don't worry, no one is the same. I don't know if you can use it as a help, I am imaginating the equipment scene into a painting tool (including drawing board, palette, brush, brush, etc.). Among them, the picture board is the most important, and the other things are served for this picture board. If you have created a device scene, it is equivalent to bought this set of painting tools from the department store, which has the condition of painting. However, your room is always not so spacious. In order to continue to draw other paintings, continue to purchase new painting tools, useless tools should be cleaned up in time. Because the device scene itself is an occupied memory. Don't worry that this will reduce the running speed, create a device scene for your computer, then delete a device scene, it is all between the moment, it is not a waste of time, it is absolutely not like running a department store. Trouble, time. For the drawing, you should know that the drawing is not simply painting, and the output text is also a drawing process. Despite this, the graphics function and text function in the API function are each. Painting and writing text are in the same device scenario, this is important. I think, you probably not understand well, but there is no relationship, continue to be optimistic about it. Remember at a point in this section: All drawings under WidNows are through the device scene.
Second, how to use the device scene from VB
How to use the device scene in VB? VB's designers have thought of this, just like the form handle HWnd is like to directly manipulate the form, providing the HDC's handle properties for the device scene. Many API functions are one of its parameters as HDC. If the control does not provide an HDC property, you can also get it with a getDC or getDCEX function, use the releasedc function to release it with the releasedc function. However, this has to be a control HWND property. If there is no HWnd property, there is no way, probably not the place where you draw. You can also create one or more device scenes according to your needs, how much is it created. This is probably the most handsome play of the CreateCompatibleDC and CreateDC functions. The device scene can be associated with a window, or may exist in an isolated manner. The so-called association is said that you draw in this device scene, the content will be immediately output to the associated window, and the so-called isolated is that the graphic is drawn in this device scene, but it only exists in this device scene, not display where is it. We can perform raster operations for a plurality of device scenarios, making processing, and finally transmitting images to device scenarios that have been associated with the form, let it display. The device scene created, if it is no longer used, should be deleted. (This description, please refer to the supplied demo process windowdc.vbp.)
The following table summarizes the API function used to get and release the device scene.
Function explains CreateCompatibleDC Create a memory device scenario with source DC compatible. Memory device scenarios can be seen as an simulation of devices in memory. By selecting a bitmap in the device, you can create a memory impact with the device compatible. CreateDC Creates a device scenario for the specified device, which is usually used to create a DC for the printer. Createic creates an information scenario (IC) for the specified device. IC is similar to the device scene, just the required system overhead, which can be used to obtain information about the device, but cannot work. DeletedC deletes a set of device scenarios. It can be used to release the device scenarios established by createdc, createc, and createcompatibledc functions. Getdc, getDCEX Gets a device scene for the specified window. If the window class uses a dedicated DC, retrieve the device scenario with this function, otherwise it gets a DC from the Windows cache, and gets a cache scene when using GetDCEX to use a dedicated DC. GetWindowDC This function is similar to getDC, just the device scene retrieved is the entire window, not the client area. ReleaseDC releases device scenarios obtained with GetDC, GetDCEX, and getWindowdc. If DC is a dedicated device scene, the function is invalid. The WindowFromDC determines the window handle associated with the specified device scene. The following demo is a very delicious program. The purpose of this program is to: we have to draw a circle in the Text control. Of course I estimate that no one will choose the Text control to draw. I always like to use the API to make some evil ways. The following is the source code of the program, you can also launch the proGram1.vbp presentation from the tutorial. How to use is that a hollow circle will appear when we click on the Text Control. Option expedition
Private Declare Function GetDC & Lib "user32" (ByVal hwnd As Long) Private Declare Function Ellipse & Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) Private Declare Function ReleaseDC & LIB "User 32" (Byval HDC As Long)
PRIVATE SUB FORM_LOAD () text1.text = "" End Sub
PRIVATE SUB TEXT1_CLICK () DIM TEXTDC AS Long Textdc = getDC (Text1.hWnd) 'Get Device Scene Handle
Ellipse Textdc, 0, 0, 100, 100 'Pavilion Empty Round ReleaseDC Text1.hWnd, Textdc' The working principle of the release device scene END SUB program is like this. First, in the Form_Load () event, we use the text1.text = "" code to clear all the texts within the Text Control. Next, when we click on the Text Control, the program draws a circle there, and the API function Ellipse (this function is the content of the next class). The Ellipse function is simple to draw in the upper left corner (x1, x2), the lower right corner (x2, y2) is drawn in a rectangle. The Ellipse function requires a DC (device scene handle), but the Text control does not provide HDC properties, there is no way to get :Textdc = getDc (Text1.hwnd) with a getDC function. Then make a drawing operation, and finally use the ReleaseDC function to release the device scene (associated device scene, can not be deleted). If we change the textdc = getdc (text1.hwnd) Ellipse Textdc, 0, 0, 100, 100 in the above code to Textdc = Getdc (0)
What happens to Ellipse Textdc, 0, 0, 500, 500? Maybe you try it. What did you learn? Or directly tell you: The device scene handle of the entire screen is always equal to 0.
Winner? You can also make the Text control output a picture (photo). The method is simple. You can use the Picture control to load a picture first, then use the API function bitblt to transfer the picture to the Text control, you will study it yourself.
Ha, now I feel that the equipment scene is really existing? Oh, then come!
Third, in-depth equipment scene (logical coordinates, logic windows, equipment coordinates, viewports)
Device Coordinate, also known as physical coordinate, is a coordinate of the output device (display, printer, etc.). Typically call the device coordinates on the screen as screen coordinates. The screen coordinates are indicated by the horizontal distance and the vertical distance of the upper left corner of the screen, and is expressed in pixels. The X-axis direction is downward, the coordinate origin is at the upper left corner of the window. Logical Coordinate is the coordinate of the system as records. Under the default coordinate mode (mm_text coordinate mode), the direction of logical coordinates is the same as the direction and unit of the device coordinates. However, this has to have a prerequisite: 1, the window is a non-rolling window; 2, the window is the scroll window, but the vertical scroll bar is at the top of the border, and the horizontal scroll bar is at the leftmost side of the border.
In order to use logic coordinates in different fields, Windows provides the following eight coordinate modes (also known as image modes).
Coordinate mode illustrates the default coordinate mode of the MM_Text device scene, and the coordinate mode of the VB form or the picture control is equivalent to scalemode = vbpixels. With pixels as logical units, X axial right is positive, and the Y axial is positive. It is also the default mode of VC. MM_HIENGLISH is a logical unit in 0.001 inches, and the X axial right is positive, and the Y axial direction is positive. MM_LOENGLISH is a logical unit in 0.001 inches, X axis right is positive, and the Y axial is positive. MM_HIMETRIC is a logical unit in 0.01 mm, X axial right is positive, and the Y axial is positive. MM_LOMETRIC is a logical unit in 0.1 mm, and the X axis right is positive, and the Y axial is positive. MM_TWIPS is a logical unit in a flag (1/1440 inches), and the X axis right is positive, and the Y axial is positive. MM_ANISOTROPIC can set the length of the logical unit, X axis right is positive, the Y axial direction is positive. The length unit of the x-axis and Y axes may be different. MM_ isotropic can set the length of the logical unit, the X axis right is positive, the Y axial direction is positive. The length unit of the x-axis and Y axes must be the same. Although the coordinate conversion of logical coordinates to the device coordinates is automatically performed during the shooting process, you can also use the relevant API function to convert coordinate conversions between physical coordinates. There are two functions:
Function, DPTOLP converts points in the device coordinate system to the corresponding device scene, LPTODP converts points in the device scene logic coordinate system to physical or device coordinate system
Logical coordinates are coordinates in the device scene. There is a logical window in this coordinate system, we are drawing on this logical window. The logical window itself is sized. This size is determined by two attributes of the Window Origin and Window Extents of the Device Scene. The two attributes represent the point of the logical window on the upper left corner of the logical coordinates and the point from the lower corner. Since the coordinate mode of logical coordinates is not necessarily default mm_text, the x and y values in both attributes may be negative (of course, this is not a common situation). In most cases, we need to draw images in the device window to actually output into the device window. For the display, the device window is the screen. Then there is a problem here: that is, the image in the logical window should appear in which position is on the screen, but it is said that the device window is located. Windows has long thought of this: the area corresponding to the logical window is called viewport on the device. VIEWPORT. Since it is corresponding to the logical window, then the viewport is naturally small. This size is determined by two properties of the Viewport Origin and Window Extents, which are the viewport origin of the device scenario.
In order to deepen the contents of the above understanding, I made a leapfrutive process (suitable for MM_Text coordinate mode), which is recommended to optimize from new reading.
The size of the logic window and the viewport is not the same, and the high and wide ratios of the two are not necessarily the same. It can be seen that there will be coordinate conversion when the logical window to the image of the viewport. But don't worry, we don't need to manage those things, and Windows will automatically do this, you need to grasp the size of the logical window and the size of the viewport. In MM_Text mode, as long as the size of both is certain, regardless of the size of the size, the image of the logical window is extended to the viewport size and displayed in the viewport. Well, you probably think of the Scale method in the VB, which is used to change the coordinate system of Form, PictureBox, or Printer. If you have, you have guess it, the actual activity it is actually the logic window of the device scene. Equipment scene shooting function
The function explains the startDCORGEX to define the starting point position of the Window Customer Area. GETGRAPHICSMODE defines whether to allow enhanced image. GetMapMode defined current device context acquiring mapping modes GetViewportExtEx GetViewportOrgEx obtain the viewport origin GetWindowExtEx viewport window range GetWindowOrgEx acquisition logic acquires the start point logical window starting offset OffsetWindowOrgEx OffsetViewportOrgEx viewport window starting offset ScaleViewportExtEx the viewport window range zoom scaling logic ScaleWindowExtEx
Fourth, regional and tailoring area
Please note that attention, pay attention, pay attention! Now, it is estimated that some friends think that the viewport is the actual location and size of the window in the screen. If you think so, then you have a big mistake. Because you will find that the actual image is smaller than the window or greater than the window. There is still another problem. That is, there are many windows in the screen, and the video ports are as much as the window of each window. These viewports will overlap. So, which one should be displayed in the overlapping section? In order to solve such problems, Windows provides a tailoring function. The so-called tailoring function means that if the screen exceeds the specified area (tailoring area), the range will be automatically cut off without displaying. This feature is automatically completed by Windows, what you want to do is whether you maintain the default crop area or reset it. As for the area, you understand it as a polygon. The standard concept is that the area refers to the GDI object that describes a certain planar range in the device scene. Each zone has an object handle, and like any GDI object, you can call the API function deleteObject (next class) to delete when not in use. The area-related area function is listed in the table below.
Area has a handle
Functional Description CombineRgn a selected number of the combined method of creating two regions CreateEllipticRgnCreateEllipticRgnIndirect circular and elliptical area CreateRectRgnCreateRectRgnIndirect CreateRoundRectRgn create a rectangular region determined to create a rounded rectangle area EquaRgn ExtCreateRegion two regions are equal to apply a transform to allow acquisition region GetRgnBox A rectangle, the rectangle is retrieved for the boundary of the selected area. The data of the descriptive area of the description area OFFSETRGN translation selecting the area PTINREGON to determine if the rectinRegion determines whether the rectang is determined in the selected area. SetRectrGn modifies the selected area Describe a rectangular area such that the cropping area is actually one area, but it has more highly negative functions. You can get a window cutting area handle, then processed it with a zone function, pay it to the window, or simply create a clipping area from the newly created a window, thereby changing the tailoring effect of a window. The entire window of the form (including the title bar and status bar, etc.), and the customer area of the window has its own tailor area. The cropping area is generally rectangular, but it may be any shape, even a plurality of regions that are separated from each other. The so-called window clipping area is actually the clipping region of the device scene (CLIPPING) attribute. A window default tailorway is the change window itself.
The window also has a clipping area, and the customer area of the window also has a tailoring area, which is undoubtedly that the window also has equipment scenarios, and the customer area also has equipment scenes. Due to habits, the frequently speaking, he is often said, and it is the equipment scene of the customer area of the window. You can set the cutting area of the entire window as any shape to implement any shape. This has to rely on the baby function setWindowsRNG! As for the device scenario of the entire window, you can use the API function getWindowDC to get (the usage of this function in the WindowsDC.vbp demo).
Regional tailoring function
Function explains ExcludeClipRect Digits a specific giant area in the tailoring area EXSETELECTCLIPREGON with the current tailoring area to get a rectangle, the rectangle to get the border of the clipping area GetClipRGN gets a window of the cutting area INTERSECTCLIPRECT to select the current area and selected The rectangular intersection is set to the cutting zone OFFSETCLIPRGN Press the PTVISible Phase Phase PTVisible Determine the Selected Point (Current Trip District) Whether the RectVisible determines whether a part of the current rectangle (current tailoring area) is visible SelectClipPath to select a path to the device scene As the tailoring area (due to the concept of path, this function is discussed in the next class) SetwindowRGN sets the tailoring area for the window; this allows you to create an arbitrary shape window. The demo program included with this tutorial is designed to show the shear function of Windows. In addition, there is a content called "World Conversion" in the coordinate conversion, but it is only designed to WindowsNT users, interested friends, please check your information.
V. Equipment scene in VB
Sometimes we may make a lot of modifications to the device scene properties. After using a device scene, as a good program habit, the device scene status should be restored to the state before we control the device scene to ensure that other code correctly draws. This is not said that it is just a good habit. To restore the VB device scenario to the initial state before VB control, you can use the API function Savedc and RESTOREDC functions, the former is used to save the device scene to the device scene stack, which restores the device scenario in the stack. Still keep a unified style, draw a table:
Device scene stack function
Function Save Savedc Saves a device scenario and properties to the device scene stack. Restoredc Restores a certain device status and attribute from the Windows Device Scene Stack.
Next, let us study the problem of automatic heavy-in properties of the device scene. We know that the Form and Picturebox controls in VB have an AutoreRAW property that determines whether to use persistent graphics or redraw objects through the Paint event. When the AutoRedRAW property is set to FLASE, the Equipment Scene Handle and the control provided by the getDC function is the same, and the same device scene is referred to. However, when it is set to True, the device scenario obtained with GETDC is different from the scene handle returned by the HDC property. One thing is clear, there are two equipment scenes in this case. However, these two device scenarios cannot be associated with a form, otherwise, what should the form you should display in the image in the device scene? We can write a small program to examine this problem, (Program2 .vbp), the program code is as follows:
Option expedition
Private Declare Function Getdc & Lib "User32" (Byval Hwnd As Long) Private Declare Function WindowFromDC & LIB "User32" (Byval HDC As Long) private submmand1_click () DIM MYDC AS LONG
Form1.AutoRedraw = True MyDC = GetDC (Form1.hwnd) Text1.Text = MyDC Text2.Text = Form1.hdc Text3.Text = WindowFromDC (MyDC) Text4.Text = WindowFromDC Form1.hwndEnd (Form1.hdc) Text5.Text = Sub
The program operation results are as follows (running on your computer): 1 Its associated form handle is: 033, the handle of the Form form is 3024
1. Program description, when using HDC to obtain an associated window, the function performs an error, indicating that the HDC is not associated with the form. So, when we draw a circle with the following code, we will not see anything. (Create a new project, paste the following code, you can run) Private Declare Function Ellipse & lib "gdi32" (Byval HDC As Long, Byval Y1 As Long, Byval x2 As long, Byval Y2 As long)
Private Sub Form_Click () form1.autoredraw = true Ellipse form1.hdc, 0, 0, 100, 100nd Sub Although nothing can be seen, if you cover the program form with other forms, let the program form display again You will find that the graphics appear. Why? It turned out to be a refresh method (see the table later).
The following code can display the drawing content directly. (Create a new project, paste the following code, you can run) Private Declare Function Getdc & Lib "User32" (Byval HWnd As Long) Private Declare Function Ellipse & Lib "GDI32" (Byval HDC As Long, Byval X1 As Long, BYVAL Y1 as long, byval x2 as long, byval y2 as long
Private Sub Form_Click () Dim mydc as long form1.autoredraw = true mydc = getdc (form1.hwnd) Ellipse mydc, 0, 0, 100, 100nd SUB Although the graphics appear, this time the situation is just the opposite. That is, if you cover the program form with other forms, let the program form displays again, you will find that the graphic disappears. (But if half of the graph is covered, the graph will still exist). It is because there is no image in the HDC, and the refresh method uses HDC to override GetDC.
So, if you use the API drawing function, then during Autoraw = True, the expected result should be known before design. In this case, Windows is drawing in the HDC, then uses GetDC to create a device scene associated with the form, and then copy the contents of the HDC to this temporary buffer scene to eventually display graphics. I think this process has been encapsulated in the dramatic function of VB's Line (note that this is just my guess, not necessarily accurate). As follows :Option ExplicitPrivate Declare Function Getdc & Lib "User32" (Byval HWnd As Long)
Private Declare Function Ellipse & Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) Private Declare Function BitBlt & Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) Private Declare Function ReleaseDC & Lib "user32" (ByVal hwnd As Long, Byval HDC As Long)
Private sub flow_click ()
DIM MyDC as long form1.autoredraw = true Ellipse HDC, 20, 20, 80, 80 'actual use can be used to replace the following three lines to simplify the code. Mydc = getdc (form1.hwnd) Bitblt mydc, 0, 0, form1.width, form1.height, form1.hdc, 0, 0, vbsrccopy releasedc form1.hwnd, MYDCEND SUB run this program, you can find that no matter The image is always displayed in the form. The following table describes the VB properties related to the drawing and refreshing windows.
VB attribute
AutoRedraw = false
AutoRedraw = True
HDC attribute
Window device scene handle, is associated with the window.
Pointing the handle of the memory device scene (often said to be a memory device scene), compatible with the window, this bitmap is called a fixed image bitmap, and the screen is called the screen. This device scenario is not associated with the window.
Picture properties
Window background image bitmap handle
Background image bitmap handle, changing it will cause a fixed image bitmap to refresh it immediately to reflect it.
Image attribute
The window fixes the image bitmap section, which is drawn when the image cannot be changed.
The window fixing the image bitmap section, which is referring to the handle of the device scene by the HDC property.
CLS method
Clear the window to make it only realistic background bitmap
Clear fixed position map to the background color and copy to the background bitmap (if any) to the fixed fixed bitmap.
REFRESH method
Copy the background picture (if any) to the window, then generate a PAINT event. All drawings are processed directly through the HDC device scene.
Copy the fixed image to the window, all drawings are done by the HDC device scenario.
A key application problem to learn in this section is that the memory device scene will be used. Here, Autoraw = True, the device scenario with HDC is the memory device scene, which is not associated with the form. Therefore, multi-faceted graphics processing processing can be performed in this equipment scene, and finally transmitted to the device scene acquired with GETDC, so that the processing of the image can be hidden. As an example, you can download the Play024.zip file (filter presentation) "" VB Front "" Source Element Analysis ". 6. Equipment scene information functions
At the beginning of this class, discuss what is the equipment scene, we have given various properties of the device scene. This section mainly discusses functions related to acquiring these attribute information and its application examples. The information of the device scene is the getDeviceCaps function provided by the Windows General Device Interface (GDI) to obtain device scene information. The function of this function is to return information based on the function of the device represented by the specified device scene. This function has two properties, one of which is the device scene handle, and its second is the type parameters of returning information. For the use of this function, the main thing is to recognize the meaning of these constants. Because of this, I can't provide Chinese documents here, please refer to other relevant information such as MSDN.
Finally, the equipment scene is finished, and the next class we will discuss how to draw in this equipment scene. I think it is a very easy thing. Of course, as long as you understand the equipment scene.