与分离器样式的Texturebrush

时间:2011-09-05 11:45:19

标签: c# gdi+

需要一个Method来返回一个变量Texturebrush。该方法需要重载:

Size AreaSize,
int HorizontalSeperatorCount,
int VerticalSeperatorCount,
int SeperatorWidth,
Brush Seperatorbackground,
Brush Rectanglebackground,

矩形的大小将自动计算


TextturebrushExample1

这些示例具有以下值

Size AreaSize = new Size(100, 100),
int HorizontalSeperatorCount = 1,
int VerticalSeperatorCount = 1,
int SeperatorWidth = 10,
Brush Seperatorbackground = Brushes.Grey,
Brush Rectanglebackground = Brushes.Red

TextturebrushExample2

第二个示例具有不同的VerticalSeperatorCount

Size AreaSize = new Size(100, 100),
int HorizontalSeperatorCount = 1,
int VerticalSeperatorCount = 3,
int SeperatorWidth = 10,
Brush Seperatorbackground = Brushes.Grey,
Brush Rectanglebackground = Brushes.Red

方法的签名

public static TextureBrush GetTextureBrush(Size areaSize, int horizontalSeperator, int verticalSeperatorCount, int seperatorWidth, Brush seperatorBackground, Brush rectangleBackground)

Texturebrush将用于填充窗口

TextturebrushExample3

我不是最好的画画。 我对解决方案非常满意。

1 个答案:

答案 0 :(得分:0)

public static TextureBrush GetSeperatorBrush(Size areaSize,
    int horizontalSeperatorCount,
    int verticalSeperatorCount,
    int seperatorWidth,
    Brush rectangleBackground)
{
    var horizontalRectangleCount = horizontalSeperatorCount + 1.0f;
    var verticalRectangleCount = verticalSeperatorCount + 1.0f;

    var horizontalSeperatorBreadths = (horizontalSeperatorCount + 2.0f) * seperatorWidth;
    var verticalSeperatorBreadths = (verticalSeperatorCount + 2.0f) * seperatorWidth;

    var rectangleWidth = (areaSize.Width - verticalSeperatorBreadths) / verticalRectangleCount;
    var rectangleHeight = (areaSize.Height - horizontalSeperatorBreadths) / horizontalRectangleCount;

    var bitmap = new Bitmap((int)Math.Ceiling(rectangleWidth + seperatorWidth), (int)Math.Ceiling(rectangleHeight + seperatorWidth));
    var graphics = Graphics.FromImage(bitmap);

    var rectanglePoints = new[] { new PointF(seperatorWidth, seperatorWidth), 
        new PointF(seperatorWidth + rectangleWidth, seperatorWidth), 
        new PointF(seperatorWidth + rectangleWidth, seperatorWidth + rectangleHeight), 
        new PointF(seperatorWidth, seperatorWidth + rectangleHeight),
        new PointF(seperatorWidth, seperatorWidth)};

    graphics.FillPolygon(rectangleBackground, rectanglePoints);
    graphics.Dispose();

    var textureBrush = new TextureBrush(bitmap, System.Drawing.Drawing2D.WrapMode.Tile);
    return textureBrush;
}

因为四舍五入而对多个分隔符造成一些麻烦。对我来说没关系。