Although it is very convenient to use existing paint brushesSolid black paint brush, solid white paint brush, or no paint brushThese three cases.
If you want more colorful effects, you must create your own paint brush.
This process is usually: Using FunctionsCreatepen or createpenindirectCreate aLogical paint brushThis is just a description of the paint brush. These functions return the logic paint brush handle, and then call SelectObject to select the paint brush into the device description table. Now you can use a new paint brush to draw a line. At any time, only one paint brush can be selected into the device description table. After releasing the device description table (or selecting another paint brush to the device description table), you can call deleteobject to delete the created logical paint brush. After the paint brush is deleted, its handle is no longer valid.
The logical paint brush is a type of "GDI object" that you can createSix Types of GDI objectsOne, and the other five arePaint Brush, bitmap, area, Font, and color palette. In addition to the color palette, these objects are selected into the device description table through selectobjcet.
When using a paint brush or other GDI objects, observe the followingThree rules:
1. Finally, delete all created GDI objects;
2. When the GDI object is in a valid device description table, delete it;
3. Do not delete existing objects.
These rules are not without reason, but sometimes they are quite subtle. Here are some examples
The syntax of the createoen function is as follows:Hpen = createpen (ipenstyle, iwidth, crcolor );
The ipenstyle parameter determines whether the paint brush is solid, Dot, or dotted. This parameter can be the following identifier defined in the wingdi. h header file, showing the line type generated by each line type.
For the ps_sold, ps_null, and ps_insideframe line types, the iwidth parameter is the width of the paint brush. If the iwidth value is 0, the width of the paint brush is one pixel. The existing paint brush is a pixel width. If you specify a dotted line or dotted line paint brush and a physical width greater than 1, Windows uses a solid line paint brush instead.
The crcolor parameter of createpen is a colorref value that specifies the color of the paint brush. For paint styles other than ps_insideframe, if you select a paint brush from the device description table, Windows converts the color to the nearest solid color that the device can represent. Ps_insideframe is the only brush line that can use a jittery color, and only when the width is greater than 1.
When used together with the function that defines a fill area, the ps_insideframe linetype has another peculiar feature. For all paint linetypes except ps_insideframe, if the width of the paint brush used to draw the border box is greater than 1 pixel, the paint brush is centered and aligned on the border line, so that part of the border line will be located outside the border box; for the ps_insideframe paint brush line, the entire boundary frame is drawn within the boundary frame.
You can also create a paint brush by creating a structure of the logpen type and calling createpenindirect. If yourProgramManySource codeIn the initialization of the paint brush, so using this method will be much more effective.
To use createpenindirect, first define a logpen structure:Logpen;
This structure has three members: lopnstyle (unsigned integer or uint) is the paint brush line, lopnwidth (point structure) is the paint width measured in logical units, lopncolor (colorref) is the paint brush color. In Windows, only the X value of the lopnwidth structure is used as the paint width, while the Y value is ignored.
You can create a paint brush by passing the structure address to the createpenindirect structure:Hpen = createpenindirect (& logpen );
Note: The createpen and createpenindirect functions do not require the device description table handle as parameters. These functions create logical brushes that are not associated with the device description table. The paint brush is not associated with the device description table until selectobjcet is called. Therefore, you can use the same logic paint brush for different devices (such as screens and printers.
The following describes how to create, select, and delete a paint brush. Assume that your program uses three paint brushes: A black paint brush with a width of 1, a red paint brush with a width of 3, and a black point paint brush, we can define three variables to store the paint brush handles:
Static Hpen hpen1, hpen2, hpen3;
You can create these three brushes during wm_create:
Hpen1 = createpen (ps_sold, 1, 0 );
Hpen2 = createpen (ps_sold, 3, RGB (255, 0 ));
Hpen3 = createpen (ps_dot, 0, 0 );
During wm_paint processing, or any time that you have a valid handle to the device description table, you can select one of the three brushes into the device description table and draw lines with it:
SelectObject (HDC, hpen2); {OtherCode}
SelectObject (HDC, hpen1); {Other code}
Three paint brushes can be deleted during wm_destory processing:Deleteobject (hpen1); deleteobject (hpen2); deleteobject (hpen3 );
The above is the most direct method to create, select, and delete a paint brush. However, the logical paint brush occupies storage during the entire program running. Therefore, you may want to create a paint brush during each wm_paint message and delete it after calling endpaint, be careful not to delete the selected paint brush from the device description table.
You may also want to create a paint brush at any time and combine the createpen and SelectObject calls into the same statement:
SelectObject (HDC, createpen (ps_dash, 0, RGB (255, 0 )));
How can I delete a paint brush using the above method? Remember that SelectObject will return the selected paint brush handle in the device description table. Therefore, you can call SelectObject to select the black pen from the device description table and delete the value returned from SelectObject (or saved in the Hpen variable first ):
Delectobject (SelectObject (HDC, getstockobject (black_pen )));
If there is a paint brush handle, you can call GetObject to obtain the values of each member of the logpen structure:
GetObject (Hpen, sizeof (logpen), (lpvoid) & logpen );
If you want to enter the paint brush handle in the device description table, you can call:Hpen = getcurrentobject (HDC, obj_pen );
There is another function extcreatepen for creating a paint brush, which will be discussed later.