C# .NET - how to draw capacitor in windows forms c#

Asked By aman on 07-Jan-12 02:42 AM
How draw capacitor ...............
dipa ahuja replied to aman on 07-Jan-12 03:07 AM
If its a fixed capacitor then i advice you to draw in the pain and keep it as the image in the image control
aman replied to dipa ahuja on 07-Jan-12 03:20 AM
can we do with vertical line..................
dipa ahuja replied to aman on 07-Jan-12 03:37 AM
You can draw diff shapes this way

private void Drwings_Paint(object sender, PaintEventArgs e)
{
  //Pen selPen = new Pen(Color.White);
  Graphics graphic = this.CreateGraphics();
 
  Pen pen = new Pen(Color.SkyBlue, 5);
 
  //DRAW PIE
  e.Graphics.DrawPie(pen, 50, 40, 100, 100, 0, 6);
 
  //DRAW LINE
  e.Graphics.DrawLine(pen, 10, 10, 50, 50);
 
  //DRAW RECTANGLE
  e.Graphics.DrawRectangle(pen, 10, 10, 300, 300);
 
 
}
 
 

[)ia6l0 iii replied to aman on 07-Jan-12 03:37 AM
I can't think of a reason why would you need to draw an object like capacitor on screen. My guess is that it is for learning purposes.

Nevertheless, you can use the graphics object to draw the line. You can use the form's paint event to do all of this.
Below is a code snippet from my collection.

protected override void OnPaint(PaintEventArgs e)
{
            base.OnPaint(e);
            Graphics g = e.Graphics;
            Pen myPen = new Pen(Color.Blue);
            myPen.Width = 28;
            g.DrawLine(myPen, 10, 20, 30, 44);
}

If you want to let the user draw it, then let the user use the mouse and capture the points in the form's mouse movement events.

  Point startingPoint = new Point();
        Point endingPoint = new Point();

        public void Form1_OnMouseDown(Object sender, MouseEventArgs e)
        {
            startingPoint.X = e.X;
            startingPoint.Y = e.Y;
            endingPoint = startingPoint;
        }

        public void Form1_OnMouseMove(Object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Form myForm = (Form)sender;
                ControlPaint.DrawReversibleLine(myForm.PointToScreen(startingPoint), myForm.PointToScreen(endingPoint),  Color.Red);
                endingPoint = new Point(e.X, e.Y);
                ControlPaint.DrawReversibleLine(myForm.PointToScreen(startingPoint), myForm.PointToScreen(endingPoint),  Color.Red);
            }
        }

        public void Form1_OnMouseUp(Object sender, MouseEventArgs e)
        {
            Form thisform = (Form)sender;
            //Create the graphics object from the form.
            Graphics g = thisform.CreateGraphics();
            float width = 3.8f;
            Pen myPen = new Pen(Color.Green, width);
            ControlPaint.DrawReversibleLine(thisform.PointToScreen(startingPoint), thisform.PointToScreen(endingPoint),  Color.Red);
            g.DrawLine(myPen, startingPoint, endingPoint);
            g.Dispose();
        }