Methode zum zeichnen eines Auswahlrechtecks:
/// <summary>
/// This method draws a selection box.
/// </summary>
/// <param name="grfx">The Graphics</param>
/// <param name="start">The start point</param>
/// <param name="vDeltaX">The x-value of the vector</param>
/// <param name="vDeltaY">The y-value of the vector</param>
/// <param name="pen">The pen which is used to draw</param>
/// <param name="fill">A brush or null</param>
protected virtual RectangleF DrawSelectionBox(Graphics grfx, PointF start, float vDeltaX, float vDeltaY, Pen pen, Brush fill)
{
RectangleF selectionBox = new RectangleF();
#region Determine correct position and size
if (vDeltaX > 0 && vDeltaY > 0)
{
//RightDown
selectionBox.X = start.X;
selectionBox.Y = start.Y;
selectionBox.Width = vDeltaX;
selectionBox.Height = vDeltaY;
}
else if (vDeltaX < 0 && vDeltaY < 0)
{
//LeftUp
selectionBox.X = start.X + vDeltaX;
selectionBox.Y = start.Y + vDeltaY;
selectionBox.Width = (float)Math.Abs(vDeltaX);
selectionBox.Height = (float)Math.Abs(vDeltaY);
}
else if (vDeltaX > 0 && vDeltaY < 0)
{
//RightUp
selectionBox.X = start.X;
selectionBox.Y = start.Y + vDeltaY;
selectionBox.Width = vDeltaX;
selectionBox.Height = (float)Math.Abs(vDeltaY);
}
else if (vDeltaX < 0 && vDeltaY > 0)
{
//LeftDown
selectionBox.X = start.X + vDeltaX;
selectionBox.Y = start.Y;
selectionBox.Width = (float)Math.Abs(vDeltaX);
selectionBox.Height = vDeltaY;
}
#endregion
if (fill != null)
{
grfx.FillRectangle(fill, selectionBox);
}
grfx.DrawRectangle(pen, selectionBox.X, selectionBox.Y, selectionBox.Width, selectionBox.Height);
return selectionBox;
}
Schlagwörter: Auswahlrechteck, Auswahlbox, SelectionBox, GDI
Hallo!
Passend zu meinem Vorposter hier noch zwei Links, die sich die gleiche Aufgaube gestellt haben:
How to draw a rubber band rectangle
Drawing a selection rubber band
Gruß, DaMoe