Get the App
SLTechnology News&Howtos  ›  Servers  › 

WxWidgets lesson 9 wx drawing tool

Shulou Source: shulou.com Published: 2022-06-02 23:59:25 10月03日 Update

Brush brush instructions

WxPen is used as a brush to render contour lines, and wxBrush as a brush to render the fill color of the shape. When brushes and brushes are used at the same time, you can render shapes with different outline colors and fill colors. The first argument to the wxPen constructor is the color, and the second argument is the thickness of the brush. Wx provides many common color macro definitions for brushes and brushes, avoiding the need to manually set RGB components and greatly reducing the burden on developers. For example, brush color wxBLACK_PEN wxGREEN_PEN wxWHITE_PEN wxBLUE_PEN, brush color wxBLACK_BRUSH wxGREEN_BRUSH wxWHITE_BRUSH wxBLUE_BRUSH, etc.

There are already examples.

WxBufferedPaintDC dc (this)

Dc.SetBrush (wxBrush (wxColour (255,255,255)

Existing examples

WxBufferedPaintDC dc (this)

Dc.SetBrush (* wxWHITE_BRUSH)

Be careful

Dc.SetBrush (wxNullBrush)

Dc.SetPen (wxNullPen)

The above two lines of code can clear the colors of the currently set brushes and brushes to avoid affecting subsequent rendering

Code

# include "wx/wx.h"

Hollow rectangle wxPen

Void CFlightInstrumentPanel::OnPaint (wxPaintEvent& event)

{

WxClientDC dc (this)

WxPen pen (* wxRED,1)

Dc.SetPen (pen)

Dc.DrawRectangle (0,0,200,300)

Dc.SetPen (wxNullPen)

}

Solid rectangle wxBrush

Void CFlightInstrumentPanel::OnPaint (wxPaintEvent& event)

{

WxClientDC dc (this)

WxColor strokeColor (0,255,255,255)

WxColor fillColor (0,0,255,255)

WxBrush brush (fillColor)

WxPen pen (strokeColor)

Dc.SetPen (pen)

Dc.SetBrush (brush)

Dc.DrawRectangle (0,0,200,300)

Dc.SetBrush (wxNullBrush)

Dc.SetPen (wxNullPen)

}

Can be set dc.SetBrush (* wxTRANSPARENT_BRUSH); transparent brush to achieve, render hollow contour lines

Description of DrawEllipse rendering ellipse

# include "wx/dcclient.h"

WxPaintDC dc (this)

WxColor color (15,57,121)

WxBrush brush (color)

Dc.SetBrush (brush)

Dc.DrawEllipse (0,0, width, height)

Dc.SetBrush (wxNullBrush)

DrawEllipse renders an ellipse, which is inscribed in a rectangle, and renders a circle when width equals height

The first and second parameters are the X and Y axis coordinates of the upper left corner of the rectangle, and the third and fourth parameters are rectangles.

Length and width of

DrawPoint render point description

The render point uses a brush wxPen, so it is considered that the size of the render point can be modified by modifying the width of the brush. In fact, the size of the render point is always 1 pixel, so it will be created.

It becomes the following phenomenon: just rendering a point cannot be seen with the naked eye on the screen.

Example code: associate the rendering event OnPaint, render the point on the screen (100100)

Void CFlightInstrumentPanel::OnPaint (wxPaintEvent& event)

{

WxClientDC dc (this)

WxPen pen (* wxBLACK, 5)

Dc.SetPen (pen)

Dc.DrawPoint (100,100)

Dc.SetPen (wxNullPen)

}

Example code: verify that it has indeed been rendered and visible to the naked eye, and multiple adjacent points can be continuously rendered to form a straight line. Associate the mouse drag event OnMotion

Void CFlightInstrumentPanel::OnMotion (wxMouseEvent& event)

{

If (event.Dragging ())

{

WxClientDC dc (this)

WxPen pen (* wxBLACK, 5)

Dc.SetPen (pen)

Int x = event.GetPosition () .x

Int y = event.GetPosition () .y

Dc.DrawPoint (x, y)

Dc.SetPen (wxNullPen)

}

}

Conclusion:

To be able to render a point large enough, you can use the method of rendering a circle

Tags: Brushes colors rectangles codes parameters ellipses outlines events examples graphics circles sizes instances dimensions screens situations naked eyes associations positions pixels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL NVidia Shulou Tech Info Shulou Information Docker