using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace StoresAndStockPricing.Controls{
internal static class BorderEffects
{
public static void FillRoundRectangle(Graphics g, Brush brush, int x, int y,
int width, int height, int radiusX, int radiusY)
{
FillRoundRectangle(g, brush, (float)x, (float)y,
(float)width, (float)height, (float)radiusX, (float)radiusY);
}
public static void FillRoundRectangle(Graphics g, Brush brush, float x, float y,
float width, float height, float radiusX, float radiusY)
{
if (g != null)
{
RectangleF rectangle = new RectangleF(x, y, width, height);
GraphicsPath path = GetRoundedRect(rectangle, radiusX, radiusY);
g.FillPath(brush, path);
}
}
public static void DrawRoundRectangle(Graphics g, Pen pen, int x, int y,
int width, int height, int radiusX, int radiusY)
{
DrawRoundRectangle(g, pen, (float)x, (float)y, (float)width,
(float)height, (float)radiusX, (float)radiusY);
}
public static void DrawRoundRectangle(Graphics g, Pen pen, float x, float y,
float width, float height, float radiusX, float radiusY)
{
if (g != null)
{
RectangleF rectangle = new RectangleF(x, y, width, height);
GraphicsPath path = GetRoundedRect(rectangle, radiusX, radiusY);
g.DrawPath(pen, path);
}
}
private static GraphicsPath GetRoundedRect(RectangleF baseRect,
float radiusX, float radiusY)
{
// if corner radius is less than or equal to zero,
// return the original rectangle
if (radiusX <= 0.0F || radiusY <= 0.0F)
{
GraphicsPath mPath = new GraphicsPath();
mPath.AddRectangle(baseRect);
mPath.CloseFigure();
return mPath;
}
// if the corner radius is greater than or equal to
// half the width, or height (whichever is shorter)
// then return a capsule instead of a lozenge
if (radiusX >= (Math.Min(baseRect.Width, baseRect.Height)) / 2.0)
return GetCapsule(baseRect);
if (radiusY >= (Math.Min(baseRect.Width, baseRect.Height)) / 2.0)
return GetCapsule(baseRect);
// create the arc for the rectangle sides and declare
// a graphics path object for the drawing
float diameterX = radiusX * 2.0F;
float diameterY = radiusY * 2.0F;
SizeF sizeF = new SizeF(diameterX, diameterY);
RectangleF arc = new RectangleF(baseRect.Location, sizeF);
GraphicsPath path = new GraphicsPath();
// top left arc
path.AddArc(arc, 180, 90);
// top right arc
arc.X = baseRect.Right - diameterX;
path.AddArc(arc, 270, 90);
// bottom right arc
arc.Y = baseRect.Bottom - diameterY;
path.AddArc(arc, 0, 90);
// bottom left arc
arc.X = baseRect.Left;
path.AddArc(arc, 90, 90);
path.CloseFigure();
return path;
}
private static GraphicsPath GetCapsule(RectangleF baseRect)
{
GraphicsPath path = new GraphicsPath();
try
{
if (baseRect.Width > baseRect.Height)
{
// return horizontal capsule
float diameter = baseRect.Height;
SizeF sizeF = new SizeF(diameter, diameter);
RectangleF arc = new RectangleF(baseRect.Location, sizeF);
path.AddArc(arc, 90, 180);
arc.X = baseRect.Right - diameter;
path.AddArc(arc, 270, 180);
}
else if (baseRect.Width < baseRect.Height)
{
// return vertical capsule
float diameter = baseRect.Width;
SizeF sizeF = new SizeF(diameter, diameter);
RectangleF arc = new RectangleF(baseRect.Location, sizeF);
path.AddArc(arc, 180, 180);
arc.Y = baseRect.Bottom - diameter;
path.AddArc(arc, 0, 180);
}
else
{
// return circle
path.AddEllipse(baseRect);
}
}
catch
{
path.AddEllipse(baseRect);
}
finally
{
path.CloseFigure();
}
return path;
}
}
}
|