Windows senior engineer: GDI+ drawing; basic introduction

Basic knowledge of Windows GDI drawing

1. Windows can draw straight lines, ellipse lines (curves on the circumference of an ellipse) and Bezier curves.

The 7 line drawing functions are:

(1) Draw a straight line
LineTo BOOL LineTo (HDC hdc, int nXEnd, int nYEnd);
use BOOL MoveToEx (HDC hdc, int X, int Y, LPPOINT lpPoint) in combination with the MoveToEx function

Point records the old coordinate point (previous current position).
///Note: GetCurrentPositionEx (hdc, &pt); Get the current position.

(2) Draw a series of connected straight lines.
Polyline connects the points of the Point array into lines. Instead of using the current point as the starting point,
PolylineTo uses the current point as the starting point and sets the current position as the end of the last line.
PolyPolyline draws multiple sets of connected lines.

(3) Fill the 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 draws a rectangle with rounded corners. And use a white brush to fill RoundRect (hdc, xL, yT, xR, yB, xC, yC)
 xC, yC are the rounded corners of the rounded rectangle.
Arc draws an ellipse. Arc (hdc, xLeft, yTop, xRight, yBottom, xStart, yStart, xEnd,yEnd) 
Chord Chord (hdc,xLeft, yTop, xRight, yBottom, xStart, yStart, xEnd, yEnd)
Pie draws a pie  chart Pie (hdc, xLeft, yTop, xRight, yBottom, xStart, yStart, xEnd, yEnd);
ArcTo and AngleArc draw ellipse lines.

//
Polygon(hdc,apt,iCount); //Draw a polygon, the last point is connected to the first point.
PolyPolygon(hdc, apt, aiCount, iPolyCount); //Draw multiple polygons.

(4) Bezier curve, draw Bezier curve
POINY apt [4] = (start point, first control point, second control point, end point)
Note that: the curve is the connecting curve from the start point to the end point, The control point (adjustment point) is not among them,
PolyBezier (hdc,apt,icount)
PolyBezierTo (hdc,apt,icount).
PolyDraw draws a series of connected lines and Bezier curves using the current point as the starting point .
For multiple Bezier curves, the starting point of the next one is the ending point of the previous one.

(5)Rectangle function
FillRect(hdc,&rect,hBrush); //Use the specified brush to fill the rectangle, (not including (Right, buttom) FrameRect(hdc,&rect,hBrush);//Use the brush to fill the rectangle Frame, do not fill the rectangle,
InvertRect(hdc,&rect); //Invert the pixels, white to black, black to white, and green to red.

///Operation on Rect
SetRect(&rect,xLeft,yTop,xRight,yButtom);//Assign
OffsetRect(&rect,x,y); //Move along x,y
InflateRect(&rect,x,y); // Increase or decrease the size of the rectangle.
SetRectEmpty(&rect); //Set 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 ); //Take the union
bEmpty=IsRectEmpty(&Rect);//Determine whether the rectangle is empty.
blnRect=PtlnRect(&Rect,point); //Judge whether the point is in the rectangle.

Second, the mapping coordinates

1). Viewport: device coordinates (pixels)
window: logical coordinates, pixels, mm, inches.
Viewport (device) coordinates are converted to window (logical) coordinates:
formula: generally don't remember, there are special functions.
xWindow=(xViewport-xViewOrg) * xWinExt/xViewExt +
xWinOrg yWindow=(yViewport-yViewOrg) * yWinExt/yViewExt +yWinOrg

(xWindow, yWindow) converted window coordinates
(xViewport, yViewport) The viewport coordinates (xWinExt, yWinExt) to be converted are
the window range of logical coordinates;
(xViewExt, yViewExt) is the window range of device coordinates.
(
xWinOrg,yWinOrg) is the window origin of the logical coordinates; (xViewOrg,yViewOrg) is the viewport origin of the device coordinates

2), equipment points are converted into logical points
DPtoLP (hdc, pPoints, iNumber); pPoints structure array index, and iNumber is the number of points to be converted
For example:
GetClientRect (hwnd, &rect);
DPtoLP (hdc, (PPOINT) &rect, 2);

Convert logical points to device points:
LPtoDP (hdc, pPoints, iNumber);

3), SetMapMode(hdc,iMapMode); //Set the mapping mode.
Default: MM_TEXT: Each logical unit is converted to a pixel, the positive X direction goes to the right, and the positive Y direction goes down.
The origin can be changed, and the range cannot be changed.
SetWindowOrgEx(); //Set the origin of the device environment
SetViewprocOrgEx(); //Set the center of the client area
//
//The origin and range can be changed,
MM_ANISOTROPIC: The logical unit is converted to an axis with any scale Any unit, use SetWindowExtExSetViewportExtEx function to specify the unit, direction and scale.

//The origin can be changed, the range cannot be changed, the positive direction of X is to the right, and the positive direction of Y is upward.
MM_HIENGLISH: Each logical unit is converted to 0.001 inches, the positive direction of X is to the right and the positive direction of Y is upward.
MM_HIMETRIC: Each logical unit is converted to 0.01 mm, the positive direction of X is to the right, and the positive direction of Y is upward.
MM_ISOTROPIC: The logical unit is converted into an arbitrary unit with an equal scale axis, that is, a unit along the X axis is equal to a unit along the Y axis, and the unit and direction of the axis can be specified by the sum function. The graphical device interface (GDI) needs to be adjusted to ensure that the units of X and Y remain the same size (when setting the window range, the viewport will be adjusted to achieve the same unit size).
MM_LOENGLISH: Each logical unit is converted to 0.01 inches, with the positive X direction to the right and the positive Y direction up.
MM_LOMETRIC: Each logical unit is converted to 0.1 mm, with the positive X direction to the right and the positive Y direction up.
MM_TWIPS; each logical unit is converted to 1/20 of the printing dot (ie 1/1400 inch), the positive X direction is to the right and the Y direction is upward.

Three, GDI objects

Brushes, brushes, bitmaps, regions, fonts, and palettes are 6 types of GDI objects that can be created.
GetStockObject(obj); Get the handle of the object.
DeleteObject(obj); Delete the object. (Note that it cannot be deleted in the effective device description table.)
Except for the palette, other objects are selected into the device description table through SelectObject(hdc,obj);.
GetObject(HOBJ,size,lpObj); lpObj can be the following structure. Acquiring object information,
the BITMAP // bitmap
DIBSECTION // bitmap 
EXTLOGPEN // brush
LOGBRUSH // brush 
LOGFONT // Font
LOGPEN // brush
(1) Default brush (Pen) windows of: BLACK_PEN
created:
Method a: hPen = CreatePen(iPenStyle,iWidth,crColor);
Parameters of iPenStyle: PS_SOLID solid pen
   PS_DASH dash pen (valid when iWidth<1, otherwise the system will replace it with a solid pen)
   PS_DOT dotted line (valid when iWidth<1, otherwise the system will replace it with a solid pen )
  PS_DASHDOT dot-dash line (valid when iWidth<1, otherwise the system will replace it with a solid pen)
  PS_DASHDOTDOT double-point dash line (valid when iWidth<1, otherwise the system will replace it with a solid pen)
  PS_NULL Blank pen
  PS_INSIDEFRAME thick solid line, dithering color can be used when iWidth>1 (not just pure color)

Method 2: LOGPEN logpen //Specify the structure of pen color, size and type. Same as above.
 hPen=CreatePenIndirect(&logpen);

(2) Font (Font) Default value: SYSTEM_FONT
Create:
One: CreateFont
only uses 2 parameters, the others are all 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, // Obliquely place the string
  int nOrientation, // The angle at which the character is inclined, 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. Specify a font character set Byte value (*don’t care)
  DWORD fdwOutputPrecision, // output precision*
  DWORD fdwClipPrecision, // clipping precision*
  DWORD fdwQuality, // expected to match font*
  DWORD fdwPitchAndFamily, // font family*
  LPCTSTR lpszFace // typeface*
);
                                  
font:
SYSTEM_FONT 
SYSTEM_FIXED_FONT 
OEM_FIXED_FONT

ANSI_FIXED_FONT、
ANSI_VAR_FONT 
DEVICE_DEFAULT_FONT

Among them: WM_INPUTLANGCHANGE: dwCharSet = wParam;
two: LOGFONT logfont;
    CreateFontIndirect (&logfont);

(3) Brush (Brush) Default value: WHITE_BRUSH 
system brush: WHITE_BRUSH  
  LTGRAY_BRUSH bright gray
  GRAY_BRUSH gray
                DKGRAY_BRUSH black gray
  BLACK_BRUSH 
creation:
one: hBrush=CreateSolidBrush(crColor);
two: hBrush=CreateHatchBrush, iHatch/Style Create a hatched brush fill part as a shadow.
Among them: iHatchStyle parameters are as follows:
 HS_BDIAGONAL 45-degree diagonal line,
 HS_CROSS cross-shaped line,
 HS_DIAGCROSS × type
 HS_FDIAGONAL 135-degree diagonal line
 HS_HORIZONTAL Horizontal
 HS_VERTICAL Vertical 
three: LOGBRUSH logbrush
    CreateBrushIndirect (&logbrush) ;
Four: hBrush=CreatePatternBrush(HBITMAP hbmp);//Brush to create bitmap,
five: hBrush=CreateDIBPatternBrushPt;//Brush to create DIB bitmap.
    CreateDIBPatternBrush,

(4) Bitmap default value: None;
 
CreateBitmap, 
CreateBitmapIndirect, 
CreateCompatibleBitmap, 
CreateDIBitmap, 
CreateDIBSection 
CreateSolidBrush The
first type::
HBITMAP CreateBitmap(
  int nWidth, // width
  int nHeight, // height
  UINT cPlanes, / / The number of color plates
  UINT cBitsPerPel, // The number of bits without pixels.
  CONST VOID *lpvBits // Pointer to the color data array. These color data are used to set the color of the pixels in the rectangular area. Each scan line in the rectangular area must It is an integer multiple of double bytes (the insufficient part is filled with 0). If this parameter is NULL, it means that no new bitmap is defined.
);

//Simplified
HBITMAP CreateCompatibleBitmap( HDC hdc, int nWidth, int nHeight );

//The second
HBITMAP CreateBitmapIndirect(&bitmap); bitmap is the BITMAP structure

(5) Region The region is a description of a range on the display. This range is a combination of rectangle, polygon, and ellipse. Select the region into the device description table, and the region can be used for drawing and cutting.
Function: iRgnType=CombineRgn(hRgn,hRgn1,hRgn2,iCombine);
The parameters of
iCombine : RGN_AND The common part of the two source areas
RGN_OR All
RGN_XOR Remove the common part.
RGN_DIFF hRgn1 is not part of
hRgn2 and RGN_COPY hRgn1 is all. (Ignore hRgn2);
The parameters of
iRgnType : NULLREGION get empty area
SIMPLEREGION get simple rectangle, ellipse or polygon
COMPLEXREGION multiple polygon, rectangle, ellipse combination
ERROR Error
creation:
1: Create rectangular area:
    hRgn = CreateRectRgn (xLeft, yTop, xRight, yBottom);
    hRgn = CreateRectRgnIndirect (&rect);
Two: Create an elliptical clipping area;
   hRgn = CreateEllipticRgn (xLeft, yTop, xRight, yBottom); //
   hRgn = CreateEllipticRgnIndirect (&rect);
Three; create a polygon clipping area
 hRgn = CreatePolygonRgn (&point, iCount, iPolyFillMode);

///
Rectangle and area clipping
For rectangle;
InvalidateRect (hwnd, NULL, TRUE);//Invalidate the rectangle
GetUpdateRect to obtain the coordinates of the invalid rectangle or WM_PAINT to get the
ValidateRect from the PAINTSTRUCT structure to make it valid
For the area:
InvalidateRgn (hwnd , hRgn, bErase);
ValidateRgn (hwnd, hRgn);

SelectObject(hdc,hRgn); or SelectClipRgn(hdc,hRgn); Selected into 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 previous clipping area and The intersection of a rectangle, 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 current attributes that determine how GDI functions work on the device. These attributes allow the parameters passed to the GDI function to contain only the actual coordinates or size information, but not 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 monitor or printer.

Understanding 3: The device description table (DC for short) is actually a data structure stored inside GDI. Some values ​​are graphical "attributes". These attributes define the work of some GDI drawing functions, such as: text color, text background, X coordinate of TextOut function, y coordinate mapping to the window client area, and windows display Fonts,

//Get handle hdc

(1) Note that this cannot make the invalid area valid
hdc=GetDC(hwnd);
//
ReleaseDC(hwnd, hdc);

(2) Used when processing WM_PAINT message. It can make the invalid area effective and redraw.
hdc=BeginPaint(hwnd,&ps);
/
EndPaint(hwnd,&ps);

(3) Can get the drawing information of the non-client area. Including window title bar, menu bar, scroll bar, frame, etc.
Processed in WM_NCPAINT,
hdc=GetWindowDC(hwnd);
//
ReleaseDC(hwnd, hdc);

(4) General method of obtaining DC. DC on the video display or printer.
hdc=CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL); //Get the dc of the entire screen
//
DeleteDC(hdc);

(5) Obtain a dc whose parameters are the same as those of CreateDC. But you cannot use this information environment handle to write to the device description table. pay attention.
hdc=CreateIC(TEXT("DISPLAY"),NULL,NULL,NULL);
/
DeleteDC (hdc);

(6) The bitmap can be selected into the internal device description table. Then use GDI functions to draw on the bitmap.
hdcMem=CreateCompatibleDC(hdc); //The bitmap is selected into the internal device description table
///
DeleteDC (hdcMem);

(7) Obtain the device description table of the meta file 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 save multiple times, RestoreDC (hdc, -1); restore DC
5. Device description table attributes

1) Fill in some gaps. For example, the space between dots and dotted lines is filled.
Background mode: Use SetBkMode (hdc, iBkMode) to modify
(1) OPAQUE (system default), and the interval is filled with background color.
       Modification: SetBkColor(hdc,crColor); //Modify the background color
  GetBkColor(hdc); Get the background color
(2) TRANSPARENT does not fill the interval, does not erase the background, transparent mode

2) Drawing mode: Drawing
with a brush is actually a certain Boolean operation between the brush pixel and the original pixel at the target position.
Default value: R2_COPYPEN;
Modification:
SetROP2(hdc,iDrawMode);
Get:
iDrawMode=GetROP2(hdc);

Welcome friends who want to change careers and learn programming, use more information to learn and grow faster than yourself, welcome everyone to join the group~

 

Guess you like

Origin blog.csdn.net/Python6886/article/details/111411401