如何在WinForms中绘制形状 [英] How to draw shapes in WinForms

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

问题描述

我正在尝试编写类似绘画的程序。您可以通过选择所需的形状来绘制填充的形状,单击图片框并拖动鼠标以获取所需的大小。但是可能在我拖动时发生。当我使用 refresh(); 时,先前绘制的形状会自行删除。我应该怎么做才能绘制填充的形状?

I'm trying to code paint-like programme. You can draw filled shapes by selecting which shape you want, click picturebox and drag mouse to get which size you want. But THIS can happens when I drag. When I use refresh();, shapes -which previously drawn- deletes itself. What should I do to draw filled shapes?

private void CizimPicture_MouseDown(object sender, MouseEventArgs e)
    {
        Cursor = Cursors.Cross;
        if (e.Button == MouseButtons.Left)
        {
            cizim = true;
        }
        X1 = e.X;
        Y1 = e.Y;
    }

    private void CizimPicture_MouseUp(object sender, MouseEventArgs e)
    {
        Cursor = Cursors.Default;
        cizim = false;
    }

    private void CizimPicture_MouseMove(object sender, MouseEventArgs e)
    {
        if (!cizim) return;

        if (cizim == true)
        {
            X = e.X;
            Y = e.Y;
            X2 = (e.X - X1);
            Y2 = (Y1 - e.Y);

            if (dikdörtgen == true)
            {
                resmim.FillRectangle(renk.Brush, X1, Y1, X2, -Y2);
            }
            if (elips == true)
            {
                resmim.FillEllipse(renk.Brush, X1, Y1, X2, -Y2);
            }
        }

    }


推荐答案

我寻找的示例代码既简单又有效,却找不到任何东西。为此,您不需要屏幕外的位图或 CreateGraphics ,但是您将需要跟踪鼠标的位置,绘制到屏幕上以及将绘制的形状添加到形状列表中,如下所示:埃里克建议。要处理交互式绘图,您需要在表单处理程序中存储鼠标状态,初始单击位置和当前矩形:

I looked for sample code that was both simple and worked and did not find anything. You do not need offscreen bitmaps or CreateGraphics for this, but you will need to handle tracking the mouse position, drawing to the screen, and adding drawn shapes to a list of shapes as Eric suggests. To handle interactive drawing you need to store the mouse state, initial click position, and current rectangle in your form handler:

bool mouseDown;
Point clickPos;
Rectangle rect;

然后,当用户单击时,请记住初始位置:

Then when the user clicks, remember the initial position:

private void MouseDown(object sender, MouseEventArgs e)
{
    mouseDown = true;
    clickPos = e.Location;
    rect = new Rectangle(clickPos, new Size(0, 0));
}

当用户向下拖动鼠标时,创建一个包含开始和结束的矩形当前位置:

While the user drags with the mouse down, create a rectangle encompassing the start and current location:

private void MouseMove(object sender, MouseEventArgs e)
{
    if (mouseDown)
    {
        this.Invalidate(rect);
        if (e.Location.X > clickPos.X && e.Location.Y > clickPos.Y)
        {
            rect = new Rectangle(clickPos.X, clickPos.Y, e.Location.X - clickPos.X, e.Location.Y - clickPos.Y);
        }
        else if (e.Location.X > clickPos.X && e.Location.Y < clickPos.Y)
        {
            rect = new Rectangle(clickPos.X, e.Location.Y, e.Location.X - clickPos.X, clickPos.Y - e.Location.Y);
        }
        else if (e.Location.X < clickPos.X && e.Location.Y < clickPos.Y)
        {
            rect = new Rectangle(e.Location.X, e.Location.Y, clickPos.X - e.Location.X, clickPos.Y - e.Location.Y);
        }
        else if (e.Location.X < clickPos.X && e.Location.Y > clickPos.Y)
        {
            rect = new Rectangle(e.Location.X, clickPos.Y, clickPos.X - e.Location.X, e.Location.Y - clickPos.Y);
        }

        this.Invalidate(rect);
    }
}

当用户释放鼠标时,停止绘制:

When the user releases the mouse, stop drawing:

private void MouseUp(object sender, MouseEventArgs e)
{
    mouseDown = false;
}

Windows窗体中排名第一的最重要规则是:仅绘制到绘画事件中的屏幕上。永远不要绘制 MouseMoved 事件:

The #1 most important rule in Windows Forms is: only draw to the screen in the Paint event. Never never draw in the MouseMoved event:

private void Paint(object sender, PaintEventArgs e)
{
    e.Graphics.FillRectangle(Brushes.DarkGray, rect);
}

完成此操作后,创建一个表单 List< ; Rectangle> 并在 MouseUp 事件中添加当前矩形,并在 Paint 事件。您可能还希望将图形裁剪到要在其中进行绘制的面板或窗口。您还可以在 MouseMoved 中进行一些优化,以仅使更改后的屏幕区域无效,旧矩形和新矩形。

Once you get this working, create a form List<Rectangle> and add the current rectangle in the MouseUp event and draw all rectangles in the Paint event. You might also want to clip your drawing to the panel or window you are drawing in. You can also do some optimizations in MouseMoved to only invalidate the changed screen region, not both the old and new rectangles.

这篇关于如何在WinForms中绘制形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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