Windows GDI Drawing Basics

Source: Internet
Author: User
Tags dashed line

First, Windows can draw lines, elliptical lines (curves on the ellipse circumference), and Bezier curves.
The 7 line-of-art functions are:
(1) Draw a straight line
LineTo BOOL LineTo (HDC hdc,int nxend,int nyend);
Combine the MOVETOEX function with the bool Movetoex (HDC hdc,int x,int y,lppoint lppoint); Point records the old coordinate points (the previous current position).
Note: Getcurrentpositionex (hdc, &PT); get the current position.

(2) Draw a series of connected lines.
Polyline connects the points of the point array into lines. Do not use the current point to do the start point,
PolylineTo uses the current point as the starting point and sets the current position to the end of the last line.
Polypolyline draw multiple sets of connected lines.

(3) Fill Rectangle
Rectangle Draw a rectangle and fill it with a white brush, Rectangle (Hdc,xleft,ytop,xright,ybottom)
Ellipse Draw an ellipse and fill it with a white brush. Ellipse (Hdc,xleft,ytop,xright,ybottom)
RoundRect draw rounded rectangles. Fill roundrect with white brush (HDC,XL,YT,XR,YB,XC,YC)
The XC,YC is a rounded rectangle with rounded corners.
ARC draws an oval line. ARC (hdc, Xleft, Ytop, Xright, Ybottom, Xstart, Ystart, Xend,yend)
Chord Bow Chord (Hdc,xleft, Ytop, Xright, Ybottom, Xstart, Ystart, XEnd, Yend)
Pie appeased Chart Pie (hdc, Xleft, Ytop, Xright, Ybottom, Xstart, Ystart, XEnd, yend);
ArcTo and Anglearc draw oval lines.

Polygon
Polygon (Hdc,apt,icount);//Draw a polygon, and the last point is connected to the 1th.
Polypolygon (Hdc,apt,aicount,ipolycount); Draw multiple polygons.



(4) Bezier curve, Draw Bezier curve
Poiny apt "4" = (starting point, first control point, second control point, end point)
Note that: The curve is the line curve from the start to the end, the control point (adjustment points) is not in it,
Polybezier (Hdc,apt,icount)
PolyBezierTo (Hdc,apt,icount). Start with the current point
Polydraw draws a series of connected lines and Bezier curves.
For multiple Bezier curves, the starting point of the latter is the end point of the previous bar.

(5) Rectangle function
FillRect (Hdc,&rect,hbrush); Fills the rectangle with the specified brush (does not contain (Right,buttom) framerect (Hdc,&rect,hbrush);//fills the rectangle with a brush, does not fill the rectangle,
InvertRect (Hdc,&rect); Make the pixel flip, white to black, black to white, green to red.

Operations on Rect
SetRect (&rect,xleft,ytop,xright,ybuttom);//Assignment
Offsetrect (&rect,x,y); Move along X, y
Inflaterect (&rect,x,y); Increase or decrease the size of the rectangle.
Setrectempty (&rect); Place 0
Copyrect (&a,&b); copy B to A.
Intersectrect (&A,&B1,&B2);//Copy the intersection of B1 and B2 to a.
Unionrect (&A,&B1,&B2); Fetch and set
Bempty=isrectempty (&rect);//Determines whether the rectangle is empty.
Blnrect=ptlnrect (&rect,point); Determines whether the point is in the rectangle.


Second, image coordinates

1), Viewport: device coordinates (pixels)
Windows: Logical coordinates, pixels, mm, inches
Viewport (device) coordinates are converted to window (logical) Coordinates:
Formula: Generally do not remember, there is a special function.
xwindow= (xviewport-xvieworg) * Xwinext/xviewext +xwinorg
ywindow= (yviewport-yvieworg) * Ywinext/yviewext +ywinorg

(xwindow,ywindow) converted window coordinates
(xviewport,yviewport) viewport coordinates to be converted
(xwinext,ywinext) is the window range of the logical coordinates;
(xviewext,yviewext) is the window range of the device's coordinates.
(xwinorg,ywinorg) is the window origin of the logical coordinates;
(xvieworg,yvieworg) is the viewport origin of the device's coordinates

2), the device point into a logical point
DPTOLP (hdc, ppoints, inumber);pP the indicator of the oints structure array, and Inumber is the number of points to be converted
For example:
GetClientRect (hwnd, &rect);
DPTOLP (hdc, (PPoint) &rect, 2);

To convert a logical point to an appliance point:
LPTODP (hdc, ppoints, Inumber);


3):
Setmapmode (Hdc,imapmode); Sets the mapping method.
Default: Mm_text: Each logical unit is converted to a single voxel, the x is in the right direction and the y direction is downward.
The origin can be changed and the range cannot be changed.
Setwindoworgex (); Set the origin of the device environment
Setviewprocorgex (); Set up customer Area Center
//
Both the origin and the range can be changed.
Mm_anisotropic: Logical units are converted to any unit with arbitrary proportional axes, with the Setwindowextexsetviewportextex function to specify units, orientations, and proportions.

The origin can be changed, the range cannot be changed, the square of X is facing right, and y is upward.
Mm_hienglish: Each logical unit is converted to 0.001 inches, the square of the X is oriented to the right, and y to the square upward.
Mm_himetric: Each logical unit is converted to 0.01 millimeters, x positive direction to the right, and y to the square upward.
Mm_isotropic: Logical units are converted to any unit with an equal proportional axis, that is, one unit along the x-axis equals one unit along the y-axis, and functions can specify the unit and direction of the axis. The Graphics Device Interface (GDI) needs to be adjusted to ensure that the units of X and y remain the same size (when the window range is set, the viewport is resized to the same size as the unit).
Mm_loenglish: Each logical unit is converted to 0.01 inches, the x is in the right direction, and Y is in the positive direction.
Mm_lometric: Each logical unit is converted to 0.1 millimeters, the x is in the right direction, and Y is in the positive direction.
Mm_twips, each logical unit is converted to 1/20 (that is, 1/1400 inches) of the print point, the X is in the right direction, and the y direction upward.


Third, GDI objects

The

Brush, brush, bitmap, region, font, and color palette are the 6 GDI objects that you can create.
Getstockobject (obj); Gets the handle to the object.
DeleteObject (obj);  Delete the object. (Note that you cannot delete it in a valid Device Description table.)
Except for the palette, the other objects are passed SelectObject (hdc,obj) and selected into the device description table.
GetObject (hobj,size,lpobj); Lpobj can be the following struct. Gets the object information,
bitmap      //Bitmap
dibsection //bitmap  
extlogpen   //Brushes
logbrush   //Brush  
logfont    //font
logpen      //Brushes
(1) Brush (pen) default value for Windows: Black_pen
Created:
Method One: Hpen=createpen (Ipenstyle,iwidth, Crcolor);
Ipenstyle Parameters:ps_solid    Solid Brush
   ps_dash     Dash brush (in iwidth <1 valid, otherwise the system will replace with a solid pen)
   ps_dot     dashed line (valid in Iwidth<1, otherwise the system will replace with a solid pen)
   PS_DASHDOT  Dot Dash (valid in iwidth<1, otherwise the system will replace with a solid pen)
  ps_dashdotdot double accent (valid in iwidth<1, Otherwise the system will replace with a solid pen)
  PS_NULL   blank pen
  PS_INSIDEFRAME  thick solid line when iwidth> 1 You can use a dithering color (not just a solid color)

Method Two: Logpen logpen//Specify the pen color, size, type of structure body. Ditto.
Hpen=createpenindirect (&logpen);

(2) Font default value: System_font
Create:
One: CreateFont
Use only 2 parameters, others are 0, for example: CreateFont (0, 0, 0, 0, 0, 0, 0, 0, dwcharset, 0, 0, 0, Fixed_pitch, NULL)
Hfont CreateFont (
int nheight,//character width, logical unit
int nwidth,//
int nescapement,//oblique-position string
int norientation,//The angle of the character skew, affecting a single character
int fnweight,//bold. 0-400 Standard, 700 bold
DWORD fdwitalic,//Italic
DWORD Fdwunderline,//Underline
DWORD fdwstrikeout,//Strikethrough
DWORD Fdwcharset,//1 is the default value. Specifies the value of one byte of the font character set (* ignore)
DWORD fdwoutputprecision,//Output accuracy *
DWORD fdwclipprecision,//cropping accuracy *
DWORD fdwquality,//expected match font *
DWORD fdwpitchandfamily,//font family *
LPCTSTR lpszface//words *
);

Font:
System_font
System_fixed_font
Oem_fixed_font

Ansi_fixed_font,
Ansi_var_font
Device_default_font

Among them: Wm_inputlangchange:dwcharset = WParam;
II: LOGFONT LOGFONT;
CreateFontIndirect (&logfont);

(3) Brush (Brush) Default value:: White_brush
System brushes: White_brush
Ltgray_brush Bright Grey
Gray_brush Ash
Dkgray_brush Black Ash
Black_brush
Create:
One: Hbrush=createsolidbrush (crcolor);
II: Hbrush=createhatchbrush (Ihatchstyle,crcolor);//Create a shaded brush fill part as a shadow
Where: The parameters of Ihatchstyle are as follows:
Hs_bdiagonal 45-degree slash,
Hs_cross cross-shaped lines,
HS_DIAGCROSSX type
Hs_fdiagonal 135-degree slash
Hs_horizontal level
Hs_vertical Vertical
Three: Logbrush Logbrush
CreateBrushIndirect (&logbrush);
Four: Hbrush=createpatternbrush (Hbitmap hbmp);//Create a bitmap brush,
V: hbrush=createdibpatternbrushpt;//creates a brush for the DIB bitmap.
CreateDIBPatternBrush,


(4) Bitmap (BITMAP) Default value: None (None);

CreateBitmap,
Createbitmapindirect,
CreateCompatibleBitmap,
CreateDIBitmap,
CreateDIBSection
CreateSolidBrush
The first kind::
Hbitmap CreateBitmap (
int nwidth,//width
int nheight,//High
UINT Cplanes,//number of color plates
UINT Cbitsperpel,//number of bits without pixels.
CONST VOID *lpvbits//Pointer to a color data array. These color data are used to set the color of pixels within a rectangular region. Each scan line in the rectangular area must be a double-byte integer multiple (the insufficient portion is filled with 0). If the parameter is NULL, then the new bitmap is not defined.
);

Simplified version
Hbitmap CreateCompatibleBitmap (hdc hdc, int nwidth, int nheight);


The second Kind
Hbitmap Createbitmapindirect (&bitmap); bitmap is a bitmap structure

The

(5) Region area is a description of the display's previous range, which is a combination of rectangles, polygons, and ellipses, which is selected into the device Description table, and the area can be used for drawing and clipping.
Function: Irgntype=combinergn (hrgn,hrgn1,hrgn2,icombine);
Icombine Parameters:
rgn_and  The public part of the two source area
rgn_or   All
rgn_xor  the common part.
Rgn_diff HRgn1 is not part of the HRGN2
Rgn_copy hRgn1 all. (Ignore hRgn2);
Irgntype parameter:
nullregion     get empty area
simpleregion   get a simple rectangle, Ellipse or polygon
complexregion   multiple polygons, rectangles, and ellipses
error   error
Create:
One: Create a rectangular area:
     HRGN = CreateRectRgn (Xleft, Ytop, Xright, Ybottom);
    hRgn = CreateRectRgnIndirect (&rect);
Two: Create an elliptical clipping region;
   hRgn = CreateEllipticRgn (Xleft, Ytop, Xright, ybottom);//
   hRgn = Createellipticrgnindi Rect (&rect);
Three; Create Polygon clipping area
 hrgn = CreatePolygonRgn (&point, ICount, ipolyfillmode);

///////////////////////////////////////////////////////////
Clipping of rectangles and regions
for rectangles;
InvalidateRect (hwnd, NULL, TRUE);//Make Rectangle invalid
GetUpdateRect to obtain the coordinates of the void rectangle or WM_PAINT from the PAINTSTRUCT structure.
ValidateRect make effective
For zones:
InvalidateRgn (hwnd, HRGN, bErase);
ValidateRgn (hwnd, HRGN);


SelectObject (HDC,HRGN); or SelectClipRgn (HDC,HRGN); Select the device Description table
Excludecliprect is used to exclude a rectangle from the clipping area,
IntersectClipRect is used to create a new clipping area, which is the intersection of the previous clipping area with a rectangle, and offsetcliprgn is used to move the clipping area to another part of the display area.


Four, Equipment description table

Understanding 1: The Device Description table contains many of the current properties that determine how GDI functions work on the device. These properties allow parameters passed to GDI functions to contain only the coordinates or dimension information, not the other information that Windows needs to display the object on the device,

Understanding 2: A Device description table usually refers to a physical display device. such as a video display or printer.

Understanding 3: The Device Description table (referred to as DC) is actually a data structure stored within GDI. Some values are graphical properties that define the work of some GDI drawing functions, such as the text color, the background of the text, the x-coordinate of the TextOut function, how the y-coordinate maps to the window client area, and the font that Windows displays,

Get handle HDC

(1) Note that this does not make the invalid area effective
HDC=GETDC (HWND);
//////
ReleaseDC (HWND,HDC);

(2) in processing WM_PAINT messages to make use of. Can make the invalid area become effective, redraw.
Hdc=beginpaint (HWND,&PS);
/////////
EndPaint (HWND,&PS);

(3) can obtain the drawing message of the non-client area. Including the window's title bar, menu bar, and scroll bar, frame and so on.
Processing in Wm_ncpaint,
HDC=GETWINDOWDC (HWND);
//////
ReleaseDC (HWND,HDC);

(4) Universal access to the DC method. A DC on a video display or printer.
Hdc=createdc (TEXT ("DISPLAY"), null,null,null); Get the entire screen of the DC
//////////////
DELETEDC (HDC);

(5) obtain a DC with the same parameters as the CreateDC parameter. However, you cannot use this information environment handle to write to the device description table. Special attention.
Hdc=createic (TEXT ("DISPLAY"), null,null,null);
/////////
DELETEDC (HDC);


(6) The bitmap can be selected into the Internal Device description table. Then use the GDI function to paint on the bitmap.
HDCMEM=CREATECOMPATIBLEDC (HDC); Bitmap selected into the Internal Device description table
///////////
DeleteDC (HDCMEM);

(7) Get the device description table of the metafile to create the meta-file.
Hdcmeta=createmetafile (pszFileName);
//////////
Hmf=colsemetafile (Hdcmeta)

Get information
(8) Get the size, color and other information of the video display.
int Ivalue=getdevicecaps (HDC,ILNDEX);

Save the device description table.
(9)
SAVEDC (hdc); can be saved multiple times
RestoreDC (hdc,-1); restoring DC



V. Device Description Table Properties

1\ the padding of some space compartments. Fill in intervals between dots and dashes.
Background mode: Modify with SetBkMode (Hdc,ibkmode)
(1) OPAQUE (system default), the interval is filled with the background color.
Modified: SetBkColor (Hdc,crcolor); Modify Background color
GetBkColor (HDC); Get background color
(2) TRANSPARENT does not fill the interval, does not erase the background, transparent mode

2\ Drawing method:
Drawing with a brush is actually some kind of boolean operation between the brush pixel and the original pixel at the target location.
Default value:: R2_copypen;
Modify:
SetROP2 (Hdc,idrawmode);
Get:
IDRAWMODE=GETROP2 (HDC);

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.