GDI drawing of windows programming--point line drawing

GDI drawing for windows programming – point and line drawing


point drawing

  • SetPixel (draws a pixel as the specified color)
  • GetPixel (get the color of a pixel)
  • SetPixelV (faster than SetPixel)

SetPixel

  • API function prototype:

    COLORREF SetPixel(
      _In_  HDC hdc,
      _In_  int x,
      _In_  int y,
      _In_  COLORREF crColor
    );
    

    Note: In indicates that the parameter is input, and opt indicates that the parameter is optional.

Parameter analysis:

parameter meaning
hdc device context handle
x Specifies the X-axis coordinate of the pixel point to be set, expressed in logical units
y Specifies the Y-axis coordinate of the pixel point to be set, expressed in logical units
crColor [1] Specify the color of the pixel [2] Use RGBthe macro to create the color value of COLORREF

Note: COLORREF color is defined as a DWORD type (4 bytes), used to represent RGB color.

return value:

  1. If the function call is successful, the return value is the RGB color value of the pixel set by the function (this value may be different from the color specified by crColor, the reason why this sometimes happens is because no real match for the specified color is found);

  2. If the function call fails, the return value is -1.

Can be the following values

return value meaning
ERROR_INVALID_PARAMETER One or more parameters are incorrect

Remark:

  1. If the pixel coordinates are outside the current clipping area, the function fails.

  2. Not all devices support the SetPixel function. For details, refer to GetDeviceCaps .


SetPixelV

The parameters are SetPixelthe same as

The SetPixelV function is faster than the SetPixel function because SetPixelV does not need to return the actual drawn pixel value (returns a Boolean type).

return value:

  1. If the function call is successful, the return value is non-zero;

  2. If the function call fails, the return value is 0.


GetPixel

  • API function prototype:

    COLORREF GetPixel(
      _In_  HDC hdc,
      _In_  int nXPos,
      _In_  int nYPos
    );
    

    Note: In indicates that the parameter is input, and opt indicates that the parameter is optional.

Parameter analysis:

parameter meaning
hdc device context handle
nXPos Specify the X-axis coordinates of the pixels to be obtained, expressed in logical units
nYPos Specify the Y-axis coordinates of the pixels to be obtained, expressed in logical units

return value:

  1. The return value is the COLORREF value, specifying the RGB of the pixel (the color values ​​of red, green and blue can be obtained respectively through the GetRValue, GetGValue and GetBValue macros );

  2. If the specified pixel is outside the current clipping area; then the return value is CLR_INVALID.

Remark:

  1. If the pixel coordinates are outside the current clipping area, the function fails.

  2. Not all devices support the GetPixel function. For details, refer to GetDeviceCaps.

  3. The bitmap must be selected into the device context, otherwise CLR_INVALID will be returned

  4. Return value CLR_INVALID ( 0xFFFFFFFFdefined in Wingdi.h)


straight line drawing

  • MoveToEx (specify the starting point of the line)

  • LinetTo (specifies the end point of the line)


MoveToEx

The MoveToEx function moves the current drawing position to a specific point, and also obtains the coordinates of the previous position.

  • API function prototype:
BOOL MoveToEx(
  _In_   HDC hdc,
  _In_   int X,
  _In_   int Y,
  _Out_  LPPOINT lpPoint
);

Parameter analysis:

parameter meaning
hdc Specifies the device context handle
X Specifies the X-axis coordinate of the new location, expressed in logical units
Y Specifies the Y-axis coordinate of the new location, expressed in logical units
lpPoint 1. A pointer to a POINT structure , used to obtain the coordinates of the previous position 2. If this value is NULL, the coordinates of the previous position will not be obtained

return value:

  1. If the function call is successful, the return value is non-zero;

  2. If the function call fails, the return value is 0.

Remark:

  1. The MoveExTo function will affect all drawing functions.

  2. In the default device context, the point (0, 0) is initially set to the current position.


LineTo

The LineTo function uses the current pen to draw a line from the current position to a specified point (x, y).

When this function is called, the current position becomes (x, y).

  • API function prototype:
BOOL LineTo(
  _In_  HDC hdc,
  _In_  int nXEnd,
  _In_  int nYEnd
);

Note: In indicates that the parameter is input, and opt indicates that the parameter is optional.

Parameter analysis:

parameter meaning
hdc Specifies the device context handle
nXEnd [1] The X coordinate position of the end point of the line segment, expressed in logical coordinates. [2] This point will not be actually drawn, because it is not part of the line segment
nYEnd [1] The Y coordinate position of the end point of the line segment, expressed in logical coordinates. [2] This point will not be actually drawn, because it is not part of the line segment

return value:

  1. If the function call is successful, the return value is non-zero;

  2. If the function call fails, the return value is 0.


polyline drawing

  • Polyline
  • PolylineTo
  • PolyPolyline
  • PolyBezier (Bezier curve)

Polyline

The Polyline function draws the specified array of connected points as a series of line segments (polylines).

  • API function prototype:

    BOOL Polyline(
      _In_  HDC hdc,
      _In_  const POINT *lppt,
      _In_  int cPoints
    );
    

    Note: In indicates that the parameter is input.

Parameter analysis:

parameter meaning
hdc Specifies the device context handle
lppt A pointer to an array of POINT structures (vertices of the polyline)
cPoints [1] The number of vertex POINT structures in the array pointed to by the lppt parameter [2] The value must be greater than or equal to 2

return value:

  1. If the function call is successful, the return value is non-zero;

  2. If the function call fails, the return value is 0.

Remark:

  1. The polyline is drawn by connecting the vertices in the specified array sequentially using the current brush.

  2. Unlike LineTothe or PolylineTofunctions, Polylinethe function neither uses the current position nor modifies it.


PolylineTo

The PolylineTo function draws the specified array of connected points as a series of line segments (polylines).

  • API function prototype:

    BOOL PolylineTo(
      _In_  HDC hdc,
      _In_  const POINT *lppt,
      _In_  DWORD cCount
    );
    
    

Parameter analysis:

parameter meaning
hdc Specifies the device context handle
lppt A pointer to an array of POINT structures (vertices of the polyline)
cCount [1] The number of vertex POINT structures in the array pointed to by the lppt parameter [2] The value must be greater than or equal to 2

return value:

  1. If the function call is successful, the return value is non-zero;

  2. If the function call fails, the return value is 0.

Remark:

  1. Unlike Polylinefunctions , PolylineTofunctions use and modify the current position.

  2. When drawing, the current brush will be used, first draw a line from the current position to the position of the first vertex specified by lppt, then draw to the next vertex, and so on.

  3. After the painting is completed, the current position will be moved to the position of the last vertex.

  4. If the function draws a closed shape, the shape will not be filled.


PolyPolyline

The PolyPolyline function is used to draw multiple polylines simultaneously.

  • API function prototype:

    BOOL PolyPolyline(
      _In_  HDC hdc,
      _In_  const POINT *lppt,
      _In_  const DWORD *lpdwPolyPoints,
      _In_  DWORD cCount
    );
    

Parameter analysis:

parameter meaning
hdc Specifies the device context handle
lppt A pointer to an array of POINT structures (vertices on a series of polylines)
lpdwPolyPoints A pointer to an array, each element in the array is used to specify the number of vertices of the corresponding polyline in the lppt parameter
cCount Number of elements of the array pointed to by the lpdwPolyPoints parameter (number of polylines)

return value:

  1. If the function call is successful, the return value is non-zero;

  2. If the function call fails, the return value is 0.

Remark:

  1. A series of polylines drawn by this function are all completed with the current brush.

  2. The graph drawn by this function is not filled.

  3. This function neither uses the current position nor modifies it.


PolyBezier

The PolyBezier function is used to draw one or more Bezier curves.

Bezier curve is a very beautiful curve, a Bezier spline is defined by 4 definition points: two endpoints and two control points.

  • API function prototype:
BOOL PolyBezier(
  _In_  HDC hdc,
  _In_  const POINT *lppt,
  _In_  DWORD cPoints
);

Parameter analysis:

parameter meaning
hdc Specifies the device context handle
lppt 1. A pointer to an array of POINT structures containing the coordinates of the endpoints and control points 2. The order is the start point, the first control point, the second control point, and the end point
cPoints Specifies the number of points in the array pointed to by lppt

return value:

  1. If the function call is successful, the return value is non-zero;

  2. If the function call fails, the return value is 0.

Remark:

  1. The PolyBezier function draws one or more cubic Bezier curves using the endpoints and control points specified by the lppt parameter.

  2. The four points of the first Bezier curve are: the start point, the first control point, the second control point and the end point; each subsequent Bezier curve only needs to give three points, because the previous Bezier curve The end point of the Bezier curve will be regarded as the starting point of the next Bezier curve, and so on.

  3. When drawing a series of connected Bezier curves, only when the second control point of the first Bezier curve, the end point of the first Bezier curve (that is, the start point of the second Bezier curve) and When the first control point of the second Bezier curve is a linear relationship (that is, when the three points are collinear), the curve is smooth at the connection points.


enjoy it ~

Guess you like

Origin blog.csdn.net/a924282761/article/details/127348764