Existing paint brush and create your own paint brush 6 zhonggdi

Source: Internet
Author: User

Windows will use the "paint brush" selected from the device environment to draw a straight line. The paint brush determines the color, width, and style of the line. The style can be solid line, dotted line, or dotted line. The default device environment of the paint brush is black_pen. The paint brush draws a solid black line with a pixel width. In Windows, three "backup brushes" are provided: black_pen, white_pen, and null_pen.

You can use the getstockobject function to obtain the handle of the alternate paint brush. You can use the SelectObject function to select the paint brush from the device environment. SelectObject returns the handle of the paint brush previously selected from the device environment.

Take a look at the following example:

Case wm_paint: HDC = beginpaint (hwnd, & PS); // getclientrect (hwnd, & rect); Hpen = (Hpen) getstockobject (white_pen ); // select an alternative white brush Hpen = (Hpen) SelectObject (HDC, Hpen); // select a white paint brush from the device environment, and keep the previous paint brush handle in the Hpen rectangle (HDC, 100,100,200,200); // draw a rectangle SelectObject (HDC, Hpen) with a white paint brush ); // select the previously reserved paint brush handle into the device environment (recovery) ellipse (HDC, 200,200,300,300); // draw an elliptical endpaint (hwnd, & PS); Return 0;

Let's see how the results are:

Because the white paint brush is the same as the background color, the rectangle is invisible;

 

Create, select, and delete a paint brush

You can use createpen or createpenindirect to create your own paint brush.

A logical paint brush is a "GDI" object. A program can create a six-in-six GDI object. It is one of the five other types: painter, bitmap, area, Font, and palette, all objects except the color palette can be selected into the device environment through the SelectObject function.

The following three rules control the use of paint brushes and other GDI objects:

1. Delete all created GDI objects.

2. Do not delete a GDI object when it is selected as a valid device environment.

3. Do not Delete backup objects.

Let's take a look at the createpen function declaration intercepted from msdn:

HPEN CreatePen(  int fnPenStyle,    // pen style  int nWidth,        // pen width  COLORREF crColor   // pen color);

Here I just want to say that as a programmer, it is very important to learn to read the English literature.

Let's take a look at this example:

Case wm_paint: HDC = beginpaint (hwnd, & PS); // getclientrect (hwnd, & rect); hpen1 = createpen (ps_solid,); // the solid state is created, paint Brush hpen2 = createpen (ps_dashdot, 3, RGB (, 0); // creates a style of ps_dashdot and a width of 3, red paint brush hpen3 = createpen (ps_dot,); // The style is created as dotted line and the width is 1 (if the parameter is 0, it is automatically set to 1 ), color: black paint brush Hpen = (Hpen) SelectObject (HDC, hpen1); // select paint brush 1 into the device environment rectangle (HDC, 50, 150,150 ); // draw a rectangle SelectObject (HDC, hpen2); // select paint brush 2 into the device environment ellipse (HDC, 200,200,300,300); // draw an elliptical SelectObject (HDC, hpen3 ); // select paint brush 3 into the device environment roundrect (HDC, 350,350,550,450, 50, 25); // draw a circle rectangle SelectObject (HDC, Hpen); // restore endpaint (hwnd, & PS); Return 0; Case wm_destroy: deleteobject (hpen1); // Delete the paint brush deleteobject (hpen2); deleteobject (hpen3); postquitmessage (0); Return 0;

The explanation is clear enough. Look at the results:

 

The above is also used (saving code ):

SelectObject (HDC, createpen (ps_dash, 0, RGB (255, 0); // create and select eobject (SelectObject (HDC, getstockobject (black_pen ))); // select the system's alternative black paint brush and delete the previous paint brush

You can also do this:

hPen=SelectObject(hdc,CreatePen(PS_DASH,0,RGB(255,0,0)));DeleteObject(SelectObject(hdc,hPen));

Turning back to the peak and pondering;

Case wm_paint: HDC = beginpaint (hwnd, & PS); // getclientrect (hwnd, & rect); setbkcolor (HDC, RGB (255,0, 0 )); // The setbkmode (HDC, transparent) color used to fill the dotted lines; // transparent to prevent the setbkmode (HDC, opaque) from being filled; // opaque Hpen = createpen (ps_dot ); selectObject (HDC, Hpen); rectangle (HDC, 100,100,300,200); endpaint (hwnd, & PS); Return 0;

Be sure to knock on the code by yourself. Don't be lazy;

 

 

 

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.