Wednesday, April 6, 2022

5-How to draw on the schematicCanvas

The schematicCanvas (pictureBox) supports drawing graphics such as lines, rectangles, ellipses, curves, and paths (see Microsoft Documentation). It will draw any graphics listed in the "paint" event handler.

In the Test Diagram designer window, display the events and double click on the blank area next to the Paint event. Add the following code to the event handler to draw a simple rectangle.

private void schematicCanvas_Paint(object sender, PaintEventArgs e)
{
    // Draw a simple rectangle with black border and no fill color
    Pen pen = new Pen(Color.Black);
    e.Graphics.DrawRectangle(pen, 100, 100, 100, 100);
}

The DrawRectangle method signature is DrawRectangle(pen, x, y, width, length) where x,y are the location of the upper left corner of the rectangle in pixels. We first create a new pen then draw the rectangle.


We have succeeded in drawing on the schematicCanvas, however, there are two problems with this approach.
  1. The pen is created everytime the Paint event occurs which can be many times per second.
  2. We can only draw a rectangle at a fixed location.
We need a way to draw a shape (component) selected by the user from a menu button.


No comments:

Post a Comment

34-Microwave Tools with Analysis (Series Final Post)

In this final blog post, I have integrated Y-Matrix analysis into the Microwave Tools project. This version has addition Lumped, Ideal, Micr...