NMA
NMA

Reputation: 37

Using a draw function in C#

I made a function that draws a rectangle with rounded corners using examples I got researching here, now I want to call this function with a button click, but I'm not sure how to provide the arguments to the function, can someone help in how to I call this function?

public void DrawCond(Graphics g, Pen p, float width, float height, float x, float y)
{
    // Auxiliary variables
    float radius;
    if (width>=3.55)    
    {
        radius = 1F;
    }
    else if (width>2.25)
    {
        radius = 0.8F;       
    }
    else if (width>1.61)
    {
        radius = 0.65F;      
    }
    else
    {
        radius = 0.5F;
    }

    GraphicsPath gp = new GraphicsPath();

    //Draw lines
    gp.AddLine(x + radius, y, x + width - 2 * radius, y); //bottom horizontal line
    gp.AddLine(x + radius, y + height, x + width - 2 * radius, y + height); //top horizontal line
    gp.AddLine(x + width, y + radius, x + width, y + height - 2 * radius); //inner vertical line
    gp.AddLine(x + radius, y + radius, x + radius, y + height - 2 * radius); //outer vertical line

    //Draw arcs
    gp.AddArc(x, y + radius, radius, radius, 90, 90); //bottom left corner
    gp.AddArc(x + width - radius, y + radius, radius, radius, 0, 90); //bottom right corner
    gp.AddArc(x, y + height, radius, radius, 180, 90); //top left corner
    gp.AddArc(x + width - radius, y + height, radius, radius, 270, 90); //top right corner

    g.DrawPath(p, gp);

    gp.Dispose();
}

Upvotes: 0

Views: 270

Answers (1)

Andrey Korneyev
Andrey Korneyev

Reputation: 26896

You can create Graphics from panel you want to draw to like var g = panel.CreateGraphics() and pass it to your function.

Also you can create Pen by using one of its constructors. See MSDN for reference about Pen.

Upvotes: 1

Related Questions