Friday, April 15, 2022

25-Canvas Grid

 In this post, we will add the grid to the Schematic Canvas. The grid functionality is setup in MainForm.

  • Add the grid gap attribute to MainForm

        // The grid spacing.
        public const int grid_gap = 10;

  • Add the DrawBackgoundGrid() helper function to MainForm

        // Draw the background grid as a Bitmap
        private void DrawBackgroundGrid()
        {
            Bitmap bm = new Bitmap(
                schematicCanvas.ClientSize.Width,
                schematicCanvas.ClientSize.Height);
            for (int x = 0; x < schematicCanvas.ClientSize.Width; x += grid_gap)
            {
                for (int y = 0; y < schematicCanvas.ClientSize.Height; y += grid_gap)
                {
                    bm.SetPixel(x, y, Color.DarkGray);
                }
            }
 
            schematicCanvas.BackgroundImage = bm;
        }

  • Add the Resize() event handler and call the DrawBackgroudGrid() function

        private void schematicCanvas_Resize(object sender, EventArgs e)
        {
            DrawBackgroundGrid();
        }

Run the program to test that the grid is drawn on the window on startup and after the window is resized. The source code for this post is on GitHub.



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...