|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionThis is a custom control which gives ability to easy draw circles and edges between them.This control is very useful for drawing binary trees. Using the code The control is based on The methods are divided as follows: CircleEntity, where are stored: coordinates, caption and List of all vertices which are direct linked to a given circle. All circles are stored in Generic List of type CircleEntity.The most interesting method is a derived and rewritten method OnPaint: /// <summary>
/// Override OnPaint and implement custom draw functionality
/// First draw linked lines. Next add text in link lines.
/// Second draw Circles.
/// </summary>
/// <param name="e">Paint arguments</param>
protected override void OnPaint(PaintEventArgs e)
{
int x;
int y;
string s;
float textX;
float textY;
SizeF sizeText;
Graphics g = e.Graphics;
Point pStart;
Point pEnd;
// First draw linked lines and draw text if exist
for (int i = 0; i < _circles.Count; i++)
{
List<CircleLinkEntity> cl = _circles[i].Links;
for (int j = 0; j < cl.Count; j++)
{
// Calculate center grid coordinates position
pStart = CalculateGrigPosCenter(_circles[i].PosX, _circles[i].PosY);
pEnd = CalculateGrigPosCenter(cl[j].Link.PosX, cl[j].Link.PosY);
g.DrawLine(_linkedLinePen, pStart, pEnd);
s = cl[j].Text;
// if text empty not draw
if (String.IsNullOrEmpty(s))
continue;
// Calculate text position
sizeText = g.MeasureString(s, _circleFont);
textX = Math.Min(pStart.X, pEnd.X) + (Math.Abs(pStart.X - pEnd.X) / 2) - (sizeText.Width / 2);
textY = Math.Min(pStart.Y, pEnd.Y) + (Math.Abs(pStart.Y - pEnd.Y) / 2) - (sizeText.Height / 2);
// Draw background rectangle
g.FillRectangle(_circleFillSolidBrush, textX, textY, sizeText.Width, sizeText.Height);
// Draw outside rectangle
g.DrawRectangle(new Pen(_circleOutlineColor, 1), textX, textY, sizeText.Width, sizeText.Height);
// Draw text
e.Graphics.DrawString(s, _circleFont, _circleFontColorSolidBrush, textX, textY);
}
}
Point p;
// Draw Circles and add inside text
for (int i = 0; i < _circles.Count; i++)
{
p = CalculateGrigPos(_circles[i].PosX, _circles[i].PosY);
// Centred to grid coordinates
x = p.X + _gridCoordinateCorrection;
y = p.Y + _gridCoordinateCorrection;
// Draw first fill circle for background
g.FillEllipse(_circleFillSolidBrush, x, y, _circleSize, _circleSize);
// Draw outside circle
g.DrawEllipse(_circlePen, x, y, _circleSize, _circleSize);
s = _circles[i].Text;
// Calculate text Height and Width
sizeText = g.MeasureString(s, _circleFont);
// Calculate text position. Justify
textX = p.X + _cellHalfSize - (sizeText.Width / 2);
textY = p.Y + _cellHalfSize - (sizeText.Height / 2);
g.DrawString(s, _circleFont, _circleFontColorSolidBrush, textX, textY);
// Rectangle over text
//g.DrawRectangle(new Pen(Color.Red, 1.0F), textX, textY, sizeText.Width, sizeText.Height);
}
base.OnPaint(e);
}
In first step are drawn all linking edges and their captions, and meanwhile all coordinates of grid are translated to real view coordinates. Points of InterestThis control can be used in studding of binary and other types of trees. We used it to display results of Huffman algorithm. HistoryIn first version we have made the base functionality, which contains only adding of circles and linking edges, and later we added editing, searching and deleting of elements. Also we have made ability to show captions on edges.
|
||||||||||||||||||||||