Use the MFC image brush class cbrush instance.

Source: Internet
Author: User

Image brush type cbrush
You can use a paint brush to draw the border of the image, and use a paint brush to color the image. Most GDI plotting functions use both a paint brush and a paint brush. They use a paint brush to draw the perimeter of a variety of graphics, and fill the image with a paint brush, so you can set a paint brush with a color and style, you can use another color and style to set the paint brush, and use the complex function to draw a complex shape.
A paint brush is managed by the cbrush class. There are two ways to create a paint brush: one is to call the constructor, and the other is to call the relevant member functions.
1. cbrush Constructor
The cbrush class structure function is defined as follows:
① Cbrush ();
② Cbrush (colorref crcolor );
Throw (cresourceexception );
③ Cbrush (INT nindex, colorref crcolor );
Throw (cresourceexception );
④ Cbrush (cbitmap * pbitmap );
Throw (cresourceexception );
Parameter description:
Crcolor: Set the paint brush color.
Nindex: If a paint brush has a pattern, this parameter sets the pattern type of the paint brush. The value is as follows:
L hs_bdiagonal: A 45-degree oblique pattern with a left high and a right low.
L hs_cross: square pattern formed by a horizontal line and a vertical line.
L hs_diagcross: it is a 45-degree Diagonal Square pattern.
L hs_fidagonal: A 45-degree oblique pattern with a right height and a left low value.
L hs_horizontal: horizontal line pattern.
L hs_vertical: vertical line pattern.
Pbitmap: a pointer to the cbitmap class. It is used to specify the bitmap used for filling.
2. The member function provided by cbrush to create a paint brush
In addition to constructors, cbrush also provides six member functions for creating a painter:
Bool createsolidbrush (colorref crcolor)
This function creates a real-filled image brush. Crcolor indicates the fill color.
Bool createhatchbrush (INT nindex, colorref crcolor)
This function creates a paint brush with a pattern. nindex indicates the pattern type, and crcolor indicates the paint brush color.
Bool createbrushindirect (const logbrush * lplogbrush)
You can call this function to generate a paint brush using the logbrush structure. The definition of the logbrush structure is as follows:
Typedef struct tag logbrush {
Uint lbstyle;
Colorref lbcolor;
Long lbhatch;
} Logbrush;
Lbstyle specifies the paint brush type, lbcolor specifies the paint brush color, and lbhatch specifies the paint brush pattern type.
Bool createpatternbrush (cbitmap * pbitmap)
Create a paint brush filled with bitmap. The bitmap of the paint brush is specified by pbitmap, And the bitmap size must be 8 × 8.
Bool createdibpatternbrush (hglobal hpackeddib, uint nusage)
Bool createdibpatternbrush (const void * lppackeddib, uint nusage)
Create a paint brush that uses Dib (device-independent Bitmap). hpackeddib points to the address of the memory block storing the bitmap data, lppackeddib points to the DIB bitmap Data Pointer, and nusage is used to set the properties of the palette.
Bool createsyscolorbrush (INT nindex)
This function sets the system-colored image brush. nindex specifies the pattern type of the image brush.
3. cbrush demo instance
(1) create a dialog box-based application Program Brushdemo.
(2) Add a member function runbrush () to the cbrushdemodlg class, which is mainly used to draw the specified rectangle in the specified area. Add the runbrush () member function Code As follows:
Void cbrushdemodlg: runbrush (cpaintdc * DC, crect rect, colorref Col, cstring text, long lbhatch)
{
Cbrush;
Cbrush * pbrush; // old brush
Cbrush. createhatchbrush (lbhatch, col); // create a pattern-based brush.
DC-> drawtext (text, & rect, dt_center | dt_wordbreak); // write the prompt text
Rect. Top = rect. Top + 40; // adjust the rectangular area
Pbrush = Dc-> SelectObject (& cbrush); // load the brush
DC-> rectangle (& rect); // draw a rectangle
DC-> SelectObject (pbrush); // restores the brush.
Cbrush. deleteobject ();
Pbrush-> deleteobject ();
}
(3) Find the cbrushdemodlg: ondrow () function and modify the content of this function:
Void cbrushdemodlg: onpaint ()
{
Cpaintdc DC (this );
// The background is white. We do not recommend this operation.
Crect rect, fillrect;
Cbrush brush;
Brush. createsolidbrush (RGB (255,255,255 ));
This-> getclientrect (& rect );
DC. fillrect (& rect, & brush); // use the fillrect member function to fill the specified area with the brush
Brush. deleteobject ();
// Draw a rectangular area
Int left = 10;
Const int max = 6;
Fillrect. Top = rect. Top + 10;
Fillrect. Bottom = rect. Bottom-10;
Long style [Max] = {hs_bdiagonal, hs_cross, hs_diagcross ,\
Hs_fdiagonal, hs_horizontal, hs_vertical}; // specifies the pattern type of the paint brush.

Cstring text [Max] = {"section line \ n from left to right", "Grid line ",\
"Oblique gridlines", "section line \ n from right to left", "water flat line", "vertical line"}; // prompt text
For (INT I = 0; I <Max; I ++)
{
Fillrect. Left = left;
Fillrect. Right = fillrect. Left + 100;
Left = fillrect. Right + 10;
// Use "RGB (I + 1) * 255/max, 0, (max-I) * 255/max)" to generate different colors
Runbrush (& DC, fillrect, RGB (I + 1) * 255/max, 0, (max-I) * 255/max), text [I], style [I]);
}
}
(4) running effect 1 is shown.

Figure 1 cbrush demo instance

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.