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

查看:32
本文介绍了如何在 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,但您需要处理跟踪鼠标位置、绘制到屏幕以及将绘制的形状添加到 Eric 建议的形状列表中.要处理交互式绘图,您需要在表单处理程序中存储鼠标状态、初始单击位置和当前矩形:

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 窗体中 #1 最重要的规则是:仅在 Paint 事件中绘制到屏幕.永远不要在 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 并在 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天全站免登陆