How do I draw a plus sign using the GraphicsPath object in C#?

I need to draw a plus sign that will be about 50 pixels wide (each leg of the + sign) and have been attempting to do it with the GraphicsPath object but I cannot figure out how to get each corner drawn. The code I''m using as a starting point is below. The first problem I've run into is trying to draw the corner that turns up from the crossbar on the left side. If someone knows of a good book on GDI that would really be a help.
public void DrawRectangle( Graphics objG, Pen objP, float h, float v, float width, float height, float radius )
		{
			float arcRect = radius * 2;
			GraphicsPath objGP = new GraphicsPath();
			objGP.AddLine( h + radius, v, h + width - ( arcRect ), v );												// top horizontal line
			objGP.AddArc( h + width - ( arcRect ), v, arcRect, arcRect, 270, 90 );								// TR corner
			objGP.AddLine( h + width, v + radius, h + width, v + height - ( arcRect ) );						// right vertical line
			objGP.AddArc( h + width - ( arcRect ), v + height - ( arcRect ), arcRect, arcRect, 0, 90 );	// BR corner
			objGP.AddLine( h + width - ( arcRect ), v + height, h + radius, v + height );						// bottom horizontal line
			objGP.AddArc( h, v + height - ( arcRect ), arcRect, arcRect, 90, 90 );								// BL corner
			objGP.AddLine( h, v + height - ( arcRect ), h, v + radius );											// left vertical line
			objGP.AddArc( h, v, arcRect, arcRect, 180, 90 );															// TL corner
			objGP.CloseFigure();
			objG.DrawPath( objP, objGP );
			objGP.Dispose();
		}

Open in new window

TexDotAsked:
Who is Participating?
 
TexDotAuthor Commented:
I've figured this out. I added StartFigure() calls before each arc that needs to be drawn "up" and after to start the subsequent straight lines. I've attached a small piece of my code to show this.

If there is a better way I still want to know it.

objGP.AddLine( h + radius, v, h + width - ( radius * 2 ), v );
 
objGP.StartFigure();
objGP.AddArc( h + width - ( radius * 3 ), v - ( radius * 2 ), radius * 2, radius * 2, 360, 90 ); // TI corner
 
objGP.StartFigure();
objGP.AddLine( h + width - radius, v - radius, h + width - radius, v - height - ( radius * 2 ) );
 
objGP.AddArc( h + width - radius, v - height - ( radius * 3 ), radius * 2, radius * 2, 180, 90 ); // TL corner

Open in new window

0
 
tcullerCommented:
Well, here's one way to do it, though there are many. You should be able to just copy and paste my code straight into your project, I hope.


        /// <summary>Draws a plus sign of the specified height/width
        /// on the specified Graphics object, using the specified pen,
        /// with the specified center of the plus sign.</summary>
        /// <param name="myGraphics">The Graphics object to draw on.</param>
        /// <param name="myPen">The Pen object to use for drawing.</param>
        /// <param name="myCenter">The point of intersection of the plus sign.</param>
        public static void DrawPlusSign(Graphics myGraphics, Pen myPen, PointF myCenter)
        {
            const Single WIDTH_HEIGHT = 50F;
            const Single SIDE_LENGTH = WIDTH_HEIGHT / 2;

            // Horizontal line
            myGraphics.DrawLine(myPen,
                new PointF(myCenter.X - SIDE_LENGTH, myCenter.Y),  // Starting point
                new PointF(myCenter.X + SIDE_LENGTH, myCenter.Y)); // Ending point

            // Vertical line
            myGraphics.DrawLine(myPen,
                new PointF(myCenter.X, myCenter.Y - SIDE_LENGTH),  // Starting point
                new PointF(myCenter.X, myCenter.Y + SIDE_LENGTH)); // Ending point
        }

I used PointF, though you may use Point instead. If so, just replace Single with Int32, and PointF with Point (obviously). I recommend including a height/width overload in your function argument list (I didn't include this, assuming you just wanted 50 every time), but it's obviously up to you.

Hope that helped.
0
 
TexDotAuthor Commented:
Thanks tculler for the response. I wasn't clear in my original question but I need the plus sign to be an outline (think of a border) with 50 pixels between the lines of the border. Like what is shown in the code box. The border line will actually about 1-2 pixels wide.


     +----+
     |    |
     |    |
+----+    +----+
|              |
|              |
+----+    +----+
     |    |
     |    |
     +----+

Open in new window

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
tcullerCommented:
Ah I see, sorry. So each line will be 50 pixels long? In other words, the full width/height will be 102-104 pixels each (including border)? Still do-able, just a bit more calculating.
0
 
TexDotAuthor Commented:
Yes. Each line will be 50 pixels long. The problem I've run into using the code I first posted is that when I try to draw the inside corner of the crossbar on the left (inside corner on line 4 of the sample in my first comment) using parameters of 270, -90 didn't work. I then tried it with 360, 90 but a line was drawn from the top horizontal line to the 360 position then the arc was drawn.

Is it the case that lines are drawn in a particular direction? i.e. always from left to right and top to bottom.

Do I have the points in the correct locations in the box (in the code sample area) that an arc is drawn from? Would the following code draw an arc from point 270 to point 180?

objGP.AddArc( 100, 100, 20, 20, 270, 90 );  

One last question; do the first two parameters to the AddArc method represent the upper left corner of the box that the arc is drawn from?

       0
       |
    +-----+
    |     |
270 |     | 90
    |     |
    +-----+
       |
      180

Open in new window

0
 
tcullerCommented:
Well... The first question I have for you is: why are you ising DrawArc? I thought you just needed straight lines? An arc will draw a "curved" line based in your parameters. The first parameter is the x-coordinate of the rectangle, while the 2nd is the y-coordinate, 3rd is the width, and 4th is the height. The next two params are the arc's angle, followed by the sweep-angle. From what I see, though, you need only use the DrawLine method (or DrawLines()). I'll give an example after your response.
0
 
TexDotAuthor Commented:
Once again I see that I failed to completely describe what I'm trying to accomplish. I do need round corners. Sorry about that.
0
 
tcullerCommented:
Alright, sorry for the delay, we're in the process of moving, so I'm sitting here on a card-board box lol. Do you need the inside corners rounded as well? In other words, will every plus sign in your design above be an arc?
0
 
TexDotAuthor Commented:
Wow, you're dedicated and no need to apologize.

Yes, each corner (inside and outside) will need to be rounded.
0
 
tcullerCommented:
There is always another way, a "better" way, but for a thing such as this, there really is no need to go any further, seeing how Graphics is one of C#'s weak points (for good reason--C# is NOT designed for game programming and such. It's designed for RAD (Rapid Application Development), and it does that very well).

Anyways, sorry to force you to figure it out yourself, my computer's been in shambles as of late due to painting and such.

Hope I at least somewhat helped,
Nate
0
 
tcullerCommented:
Oh, I almost forgot. I did find one thing that may help. In the GraphicsPath object, there is an instance method called "GetLastPoint()", which gives you the precise location of the END of the path, even if it was an Arc. This may prove extremely useful for cleaning up your code and such. The AddArc method is a real pain in the ass, due to how it functions. Because of that, it requires some real work-arounds; however, calling GetLastPoint after calling AddArc will get you exactly where the Arc ended in the vector.
0
 
TexDotAuthor Commented:
Thanks, I'll check it out.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.