Use GDI + in VC to draw color gradient effects with variable angles-. NET tutorial, VB. Net language

Source: Internet
Author: User

Use GDI + in VC to draw color gradient effects with variable angles-. NET tutorial, VB. Net language

Author: Click 4 for a netizen contribution

Gdi + is a later version of gdi (graphical device interface provided in earlier versions of windows). It is a graphic display technology for microsoft windows xp operating system or later versions. It has been integrated.. net development environment, no matter what version your OS is, you only need to install it. net Framework, with the gdi + (Note: Yes.. net Framework, rather.. net development environment, so you can also use gdi + in win98 ).

Now, let's get down to the truth.

Add the following code to the header file:

# Include <gdiplus. h>
Using namespace gdiplus;
# Pragma comment (lib, "gdiplus. lib ")

Note: when using the gdi + function, you must initialize the gdi +. After using the function, you must destroy the gdi +!
Initialization:

Gdiplusstartupinput;
Ulong_ptr gdiplustoken;
Gdiplusstartup (& gdiplustoken, & gdiplusstartupinput, null );

Destruction:

Ulong_ptr gdiplustoken = null;
Gdiplusshutdown (gdiplustoken );

The following uses a ctestdlg dialog box as an example to plot the background, and uses gdi + to achieve a color gradient effect with a variable angle. Variables used:
Irotation: integer, gradient Angle
Color1, color2, color3: rgb color value

The two colors are relatively simple. Just use the lineargradientbrush provided by gdi +:

Bool ctestdlg: onerasebkgnd (cdc * pdc)
{
Cdialog: onerasebkgnd (pdc );

// Obtain the r, g, and B values of the first color.
Int r1 = getrvalue (color1 );
Int g1 = getgvalue (color1 );
Int b1 = getbvalue (color1 );

// Obtain the r, g, and B values of the second color
Int r2 = getrvalue (color2 );
Int g2 = getgvalue (color2 );
Int b2 = getbvalue (color2 );

// Obtain the drawn Area
Crect rect;
Getclientrect (& rect );

// Gdi + Object
Gdiplus: graphics (pdc-> getsafehdc ());
// Brush
Gdiplus: lineargradientbrush lingrbrush (gdiplus: rect (0, 0, rect. width (), rect. height (), // draw area
Gdiplus: color (255, r1, g1, b1), // The first color
Gdiplus: color (255, r2, g2, b2), // second color
(Gdiplus: real) (90-irotation); // gradient Angle

Graphics. fillrectangle (& lingrbrush, gdiplus: rect (0, 0, rect. width (), rect. height ()));
Return true;
}

The three colors are complex, and the lineargradientbrush provided by gdi + is used. However, you need to calculate the diagonal length of the drawn area and divide it into three equal points based on the diagonal line.

For details, refer to the following code:

Bool ctestdlg: onerasebkgnd (cdc * pdc)
{
Cdialog: onerasebkgnd (pdc );

// Obtain the r, g, and B values of the first color.
Int r1 = getrvalue (color1 );
Int g1 = getgvalue (color1 );
Int b1 = getbvalue (color1 );

// Obtain the r, g, and B values of the second color
Int r2 = getrvalue (color2 );
Int g2 = getgvalue (color2 );
Int b2 = getbvalue (color2 );

// Obtain the r, g, and B values of the third color.
Int r3 = getrvalue (color3 );
Int g3 = getgvalue (color3 );
Int b3 = getbvalue (color3 );

// Obtain the drawn Area
Crect rect;
Getclientrect (& rect );

// Calculate the diagonal line length
Int iheight = rect. height ();
Int iwidth = rect. width ();
Double dwdiagonal = sqrt (double) (iwidth * iwidth + iheight * iheight ));

// Three plot Areas
Rect rectdraw (0, 0, (int) dwdiagonal, (int) dwdiagonal );
Rect rectdraw1 (0, 0, (int) dwdiagonal, (int) dwdiagonal)/2 );
Rect rectdraw2 (0, (int) dwdiagonal)/2, (int) dwdiagonal, (int) dwdiagonal)/2 );

// Gdi + Object
Graphics (pdc-> getsafehdc ());
Gdiplus: bitmap bmp (rectdraw. width, rectdraw. height );
Graphics grtmp (& bmp );

// Fill the area with a brush
Gdiplus: lineargradientbrush lingrbrush (rectdraw1, color (r1, g1, b1), color (r2, g2, b2), 90 );
Grtmp. fillrectangle (& lingrbrush, rectdraw1 );
Gdiplus: lineargradientbrush lingrbrush1 (rectdraw2, color (r2, g2, b2), color (r3, g3, b3), 90 );
Grtmp. fillrectangle (& lingrbrush1, rectdraw2 );

// Computing
Dwdiagonal * = 0.5;
Double dwangle = irotation * 3.1415926/180.0;
Double dwcosangle = cos (dwangle );
Double dwsinangle = sin (dwangle );
Double dwbeta = atan2 (double) iheight, (double) iwidth );
Double dwdistance = dwdiagonal * sin (fabs (dwangle) + dwbeta );
Double xc = 0.5 * iwidth-dwdistance * dwsinangle;
Double yc = 0.5 * iheight-dwdistance * dwcosangle;
Double xc1 = 0.5 * iwidth + dwdistance * dwsinangle;
Double yc1 = 0.5 * iheight + dwdistance * dwcosangle;
Double dx = dwdiagonal * dwcosangle;
Double dy =-dwdiagonal * dwsinangle;

// Draw
Point ptdestinationpoints [3];
Ptdestinationpoints [0]. x = (int) (xc-dx );
Ptdestinationpoints [0]. y = (int) (yc-dy );
Ptdestinationpoints [1]. x = (int) (xc + dx );
Ptdestinationpoints [1]. y = (int) (yc + dy );
Ptdestinationpoints [2]. x = (int) (xc1-dx );
Ptdestinationpoints [2]. y = (int) (yc1-dy );
Graphics. drawimage (& bmp, ptdestinationpoints, 3 );

Return true;
}

Http://www.west263.com/www/info/40780-1.htm

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.