如何绘制形状并使用按钮上色? [英] How to draw shapes and color them with a button?

查看:46
本文介绍了如何绘制形状并使用按钮上色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为他的所有形状填充C.在这张照片中,我有一些要在其中放置颜色(红色或绿色)的形状.当我从C单击一种形状时,我按一个按钮放红色或绿色.我尝试在PowerPoint中进行此操作,但是很难从0创建形状.要在C#或vb.net中创建此C.

I need to fill this C for every shapes from him. In this pictures i have some shapes where i want to put color, red or green. When i click one shape from C and i press one button to put red or green. I try this in PowerPoint but is hard to create shapes from 0. In want to create this C in C# or vb.net.

我的问题是,如何通过按C#或vb.net中的一个按钮来创建C形状的图形并为其着色.非常感谢.

My question is, how is possible to create this shapes in the form of C and color them by pressing one button in C# or vb.net. Thanks a lot.

推荐答案

这并不像我想的那么难,但是我通过创建'C'而不是半圈.要创建"C"形状,您需要将形状分为三个组:两个四分之一圆和一个矩形.将所有 Shape 添加到同一列表中!

It wasn't quite as hard as I thought, but I did simplyfy the task by creating not a 'C' but a half circle. To create a 'C' shape you need to break the shapes into three groups: Two quarter circles and a rectangle.. Add all Shapes to the same list!

处理将相同,只是数学部分会有所不同.实际上,矩形部分的数学运算要比它对弧的极其简单的方法要复杂得多:-)

The processing will be the same, just the math part will differ. Actually the math for the rectangle part will be even a little more involved than the extremely simple way it works for arcs :-)

这里是一个非常简单的 Shape 类的示例:

Here is an example of a very simple Shape class:

class Shape
{
    public GraphicsPath Path { get; set; }
    public Color FillColor { get; set; }

    public Shape(GraphicsPath gp) { Path = gp; }
}

您可以像这样为它创建 List< Shape> :

You can create a List<Shape> for it like this:

List<Shape> FillList(int segments, int angle1, int angle2, int inner, int outer, int rings)
{
    List<Shape> paths = new List<Shape>();
    float deltaA = 1f * (angle2 - angle1) / segments;
    float width = 1f * (outer - inner ) / rings;
    for (int s = 0; s < segments; s++)
    {
        float a = angle1 + s * deltaA;
        for (int r = 0; r < rings; r++)
        {
            float w1 = r * width;
            float w2 = w1 + width;
            GraphicsPath gp = new GraphicsPath();

            RectangleF rect1 = new RectangleF(w1, w1, (outer - w1) * 2,  (outer - w1) * 2);
            RectangleF rect2 = new RectangleF(w2, w2, (outer - w2) * 2,  (outer - w2) * 2);
            gp.AddArc(rect1, a, deltaA);
            gp.AddArc(rect2, a + deltaA, -deltaA);
            gp.CloseFigure();
            paths.Add(new Shape(gp));
        }
    }
    return paths;
}

我添加了一些 NumericUpDowns 来演示工作中的参数:

I have added a few NumericUpDowns to demonstrate the parameters at work:

如您所见,我先选择一种颜色,然后从调色板图像中选择一种颜色,然后为 Shapes 着色.

As you can see I color the Shapes by selecting one and then picking a color from a palette image..

这是 Form Paint 事件:

private void Form1_Paint(object sender, PaintEventArgs e)
{
    foreach (Shape gp in paths)
    {
        using (SolidBrush br = new SolidBrush(gp.FillColor))
            if (gp.FillColor != null) e.Graphics.FillPath(br, gp.Path);
        e.Graphics.DrawPath(Pens.Black, gp.Path);
        if (gp == selected) e.Graphics.DrawPath(Pens.OrangeRed, gp.Path);
    }

}

如果形状具有形状,并且以某种颜色绘制,则形状将用其颜色填充;我为此使用了固定的黑色 Pen ,如果有形状,则为 Selected 形状使用了红色.

The shapes are filled with their Color if they have one and also drawn at some color; I use a fixed black Pen for this and a red one for the Selected shape if there is one..

整个选择和着色很简单:

The whole selection and coloring is as simple as this:

Shape selected = null;

private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
    if (selected != null)
    {
        selected.FillColor = ((Bitmap)pictureBox1.Image).GetPixel(e.X, e.Y);
        Invalidate();
    }
}

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
    selected = null;
    foreach (Shape gp in paths)
        if (gp.Path.IsVisible(e.Location)) { selected = gp; break; }
    Invalidate();
}

这篇关于如何绘制形状并使用按钮上色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆