"Windows Programming" series Fifth: GDI graphics drawing

Source: Internet
Author: User

On two we learned the text character output as well as the Unicode programming program, and know how to output a text string with common Win32, this one we learn another very important part of Windows programming GDI graphics drawing. The GDI function of Windows contains hundreds of APIs that we can use, and this article explains the most common GDI drawings. GDI can draw points, line curves, fill enclosed areas, bitmaps, and text, where the text section is already in the previous article, please refer to the third article in the Windows Programming Series: Text character output.

As with the previous GDI objects, the drawing functions in this article must also be a device context handle (HDC) as a function parameter, as we know from the previous article that HDC can be obtained with the BeginPaint function when processing wm_paint, or from GetDC, GETWINDOWDC.

Since it is a painting, there is no color description, there are several representations of colors in Windows, where COLORREF is the most used in GDI, and it is actually an unsigned 32 integer. Where red, green, and blue each account for one byte, the highest byte is not used, as shown in:

This value can be generated by using the RGB macros provided by Windows, which is defined as RGB in Windows:

#define RGB (R,g,b) ((COLORREF) (((BYTE) (R) | ( (WORD) ((BYTE) (g)) <<8)) | (((DWORD) (BYTE) (b)) <<16))

In addition to this, Windows also has a struct rgbquad that also represents color, which is commonly used in bitmap structure information.

    • Portrait point

Windows provides the SetPixel and GetPixel functions to set and get the color of pixel points. The function prototypes are:

COLORREF SetPixel (hdc hdc, int X, int Y, COLORREF crcolor); COLORREF GetPixel (hdc hdc, int nxpos, int nypos);

This function is not used frequently.

    • Brush painting Brush

Before drawing, you can create brushes for subsequent paint use, and the API functions for creating brushes are:

Hpen CreatePen (int fnpenstyle, int nwidth, COLORREF crcolor); Hbrush CreateSolidBrush (Colorref crcolor); Hbrush CreatePatternBrush (Hbitmap hbmp); Hbrush createhatchbrush (int fnstyle, COLORREF clrref);

It can specify brush style, width, and color. Styles can be solid lines, dashed lines, dotted lines, and more, depending on the various types of MSDN instructions.

    • Draw lines

There are more than 10 line functions provided by Windows, the usual lines are drawn as LineTo, and many segments are generally used polyline, PolylineTo, Polypolyine, etc., the curve can draw ellipse, elliptical arc, Bezier spline curve. For a prototype of these functions, refer to MSDN, which we will use to illustrate the use of these functions in the following example.

    • Closed Area padding

If the drawing of Windows is a closed area, the interior can be filled, of course, if you do not display the fill, the system will be filled with the default color, such as the window background color. We can also create a brush before drawing a closed drawing, and if the created brush is selected into the device environment, the system will fill the inner area with a brush. Common enclosing drawing API functions are rectangular rectangle, rounded rectangle roundrect, ellipse ellipse, pie chart pie, and chord cut chart chord.

    • Bitmap output

Windows has a lot of output on bitmaps, including device-related and device-independent bitmaps, bit-block transfer, transparency, scaling, and so on, this article only for the bit drawing brush for instance demonstration, and other content in the future can be written in a separate article. Use the LoadImage function to load a bitmap file with a bitmap, and then create a pattern brush with CreatePatternBrush.

    • Text output

This has been discussed earlier, please refer to the third article in the Windows Programming Series: Text character output article.

    • Drawing properties

When drawing a drawing, the environment device has 5 properties that affect most drawings:

Brush position: When you draw a line, it starts at the position where the brush is located, and the brush position can be set using the Movetoex function.

Brushes: Drawings are drawn with brushes in the current environment, and if they are not created, the system default brushes are used.

Background: Some GDI will have transparent and opaque settings.

Background color: such as the gap color of the text output.

Drawing mode: such as the dash can be set solid line, dashed, etc., fill may have different fill drawing mode.

The following is a complete example of how these common functions are applied and how they are actually used.

#include <windows.h>static TCHAR szappname[] = TEXT ("GDI Test"), Static LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain (hinstance hinstance, hinstance hprevinstance, PSTR szcmdline, int icmdshow) {HWND hwnd     ;     MSG msg;     Wndclass Wndclass; Wndclass.style = Cs_hredraw |     Cs_vredraw;     Wndclass.lpfnwndproc = WndProc;     Wndclass.cbclsextra = 0;     Wndclass.cbwndextra = 0;     Wndclass.hinstance = hinstance;     Wndclass.hicon = LoadIcon (NULL, idi_application);     Wndclass.hcursor = LoadCursor (NULL, Idc_arrow);     Wndclass.hbrbackground = (hbrush) getstockobject (White_brush);     Wndclass.lpszmenuname = NULL;     Wndclass.lpszclassname = Szappname; if (! RegisterClass (&wndclass)) {MessageBox (NULL, TEXT ("This program requires Windows nt!"), Szappname, Mb_ic          ONERROR);     return 0;                  } hWnd = CreateWindow (szappname,//window class name        Szappname,//Window caption Ws_overlappedwindow,//Window style                           Cw_usedefault,//initial x position cw_usedefault,//initial y position //initial x size,//Initial                Y size null,//parent window handle NULL, Window menu handle HINSTANCE,//program instance handle N               ULL);     Creation Parameters ShowWindow (hWnd, icmdshow);          UpdateWindow (HWND);          while (GetMessage (&msg, NULL, 0, 0)) {translatemessage (&msg);     DispatchMessage (&AMP;MSG); } return Msg.wparam;}  Draws a line of the specified property static void DrawLine (HDC hdc, int x0, int y0, int x1, int y1, int style, int width, colorref color) {Hpen Hpen = creatEPen (style, width, color); Hpen hOldPen = (hpen) SelectObject (HDC, Hpen); Movetoex (HDC, x0, y0, NULL); LineTo (HDC, x1, y1); SelectObject (HDC, hOldPen);D eleteobject (hpen);} Draws a solid circle static void Drawcircle (HDC hdc, int x, int y, int len, colorref color) {Hbrush Hbrush = CreateSolidBrush (color); Hbrush hOldBrush = (hbrush) SelectObject (HDC, hbrush); Hpen Hpen = CreatePen (ps_solid, 1, color); Hpen hOldPen = (hpen) SelectObject (HDC, Hpen); Ellipse (HDC, X-LEN/2, Y-LEN/2, X+LEN/2, Y+LEN/2); SelectObject (HDC, hOldBrush);D eleteobject (Hpen); SelectObject (HDC, hOldPen);D eleteobject (hOldBrush);}  Draw fill Rectangle static void DrawRect (HDC hdc, int left, int top, int width, int height, int style, colorref color) {Hbrush Hbrush = Createhatchbrush (style, color); Hbrush hOldBrush = (hbrush) SelectObject (HDC, hbrush); Rectangle (HDC, left, top, left+width, top+height); SelectObject (HDC, hOldBrush);D eleteobject (hOldBrush);} Draw bitmap Fill rectangle static void Drawbmprect (HDC hdc, int left, int top, int width, int height, lpctstr file) {Hbitmap hbitmap = (HBITMAP) LoadImage (NULL, file, Image_bitmap, 0, 0, lr_loadfromfile| Lr_createdibsection); Hbrush hbrush = CreatePatternBrush (HBITMAP); Hbrush hOldBrush = (hbrush) SelectObject (HDC, hbrush); Rectangle (HDC, left, top, left+width, top+height); SelectObject (HDC, hOldBrush);D eleteobject (hOldBrush);D eleteobject (HBITMAP);} Static LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {HDC hdc; Paintstruct ps;switch (message) {case Wm_create:return 0;case wm_paint:{hdc = BeginPaint (hWnd, &ps); for (int i=10; I&L t;50; i+=4) {SetPixel (hdc, I, ten, RGB (0, 0, 0));//Draw pixel dots}//Draw a line of different modes DrawLine (hdc, J, Max, Max, Max, Ps_so LID, 2, RGB (0,0,0));D Rawline (hdc, Ps_dash, 1, RGB (100,0,200));D Rawline (hdc,, Ps_dashdot                        , 1, RGB (100,250,100)); Draw arc, chord secant, pie chart arc (HDC, 10, 30, 40, 50, 40, 30, 10, 40); Chord (HDC, 10, 60, 40, 80, 40, 60, 10, 70); Pie (HDC, 10, 90, 40, 110, 40, 90, 10, 100); Point Pt[4] = {{90,130},{60, 40},{140,150},{160,80}}; Draw ellipse, Rectangle ellipse (hdc,pt[0].x, Pt[0].y, pt[1].x, PT[1].Y);                        Rectangle (HDC, pt[2].x, Pt[2].y, pt[3].x, PT[3].Y);                        Draw Bézier Curve Polybezier (HDC, PT, 4); Four anchor points marked with Bezier curves Drawcircle (hdc, pt[0].x, PT[0].Y, 8, RGB (0,255,0));D rawcircle (hdc, pt[1].x, PT[1].Y, 8, RGB (0,0,255));D                        Rawcircle (hdc, pt[2].x, PT[2].Y, 8, RGB (0,0,0));D rawcircle (hdc, pt[3].x, PT[3].Y, 8, RGB (255,0,0));                        Draw round Drawcircle (HDC, +, 0, 250, 250); Draw rectangles with different fill modes (HDC, DrawRect, Max, Max, hs_bdiagonal, RGB (255,0,0));D Rawrect (hdc, Max, Max, Max, Hs_cross, RGB (0,255                        , 0));D rawrect (hdc, 290, I,, Hs_diagcross, RGB (0,0,255));D Rawrect (hdc, 290, N, a, A, hs_vertical, RGB (0,0,0));                        Draw Bitmap Drawbmprect (HDC, Chenggong.bmp, N, +, +, +, TEXT (")"); Draw Text TextOut (HDC, +, +, text ("GDI Paint Output Test program"), 11);} EndPaint (HWnd, &ps); return 0;casE wm_destroy:postquitmessage (0); return 0;} Return DefWindowProc (hWnd, message, WParam, LParam);}

As shown in this example, the figure shows that the line is not smooth, because the Win32 paint function is not anti-aliasing, the smaller the figure, the more obvious the sawtooth. You can use GDI + drawing functions provided by Microsoft and have antialiasing effects.

The GDI basic drawing of Windows is not difficult to master, so just read the detailed usage instructions for the API on MSDN and use it correctly, but when you create GDI objects and use them, you must remember to release them.

Focus on the public platform: the Programmer Interaction Alliance (coder_online), you can get the first original technical articles, and (Java/c/c++/android/windows/linux) technology Daniel Friends, online communication programming experience, get programming basics, Solve programming problems. Programmer Interactive Alliance, Developer's own home.

Reproduced please specify the source, thank you for your cooperation!

"Windows Programming" series Fifth: GDI graphics drawing

Related Article

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.