在C#中绘制形状 [英] Drawing a shape in C#

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

问题描述

我正在使用System.Drawing命名空间,并想尝试制作一个基本的绘画程序.我在绘制形状时遇到麻烦,特别是如何显示用户正在绘制的形状而不必重新绘制整个屏幕?

当前,当用户在屏幕上绘画时,只有松开鼠标按钮,他们才能看到绘制的形状.这很烦人,很难使形状正确.

我以为使用GraphicsState和Graphics.Save()/.Restore()可以解决我的问题,但是每当我绘制一个新形状时,它都会擦除屏幕. 我希望用户在屏幕上拖动鼠标时能够看到形状.


为了存储每种形状,我使用所有形状都继承自的抽象形状类(aShapeData)和用于撤消/重做功能的形状列表(List< aShapeData>)).

抽象aShapeData类:

I''m playing around with the System.Drawing namespace and wanted to try making a basic paint program. I''m having trouble with drawing shapes, specifically, how do I show the shape the user is drawing without having to redraw the whole screen?

Currently, when the user draws on the screen, they don''t see the shape they''re drawing until they release the mouse button. This is annoying and makes it hard to get the shape just right.

I thought using GraphicsState and Graphics.Save()/.Restore() would fix my problem, but whenever I go to draw a new shape, it erases the screen. I want the user to be able to see the shape as they drag the mouse around the screen.


To store each shape, I''m using an abstract shape class (aShapeData) that all shapes inherit from, and a list of shapes (List<aShapeData>) for undo/redo functionality.

Abstract aShapeData class:

public virtual void Start(Point point, Graphics graphics)
{
    startPt = point;
    gfxState = graphics.Save(); // Not working as expected
}

public virtual void Moved(Point point, Graphics graphics)
{
    graphics.Restore(gfxState); // Not bringing back the form as it
                                // looked when it was saved.
}

public virtual void Stop(Point point, Graphics graphics)
{
    stopPt = point;
    Draw(graphics);
}



LineShape类继承自aShapeData:



LineShape class inherits from aShapeData:

public override void Moved(Point point, Graphics graphics)
{
    base.Moved(point, graphics);

    graphics.DrawLine(new Pen(Color.Black), startPt, point);
}

public override void Draw(Graphics graphics)
{
    graphics.DrawLine(new Pen(Color.Black), startPt, stopPt);
}



表单的OnPaint事件处理程序:



The form''s OnPaint event handler:

/// <summary>
/// Event raised when the form becomes invalidated. Each aShapeData object
/// in the shapeList has its "Draw(Graphics gfx)" method called, which handles
/// drawing each shape on the given graphic.
/// </summary>
private void MainForm_Paint(object sender, PaintEventArgs e)
{
    foreach (aShapeData element in currentState.shapeList)
    {
        element.Draw(currentState.graphics);
    }
}

推荐答案

请参见,动态绘制图像的主要目的是在每次鼠标移动时绘制图像.您需要设置初始位置,并且在用户拖动时,需要继续绘制和更新控件.

另外,您还需要使用背景色重新绘制之前绘制的先前形状.

所以,
在MouseDown上开始绘制.
在MouseMove上,您基于当前位置进行绘制,
使用背景色绘制现有形状.

因此,在您看来,现有的形状会被删除,而新的形状会动态创建,直到mouseup为止.


:thumbsup:
See, the main objective of drawing an image dynamically is to draw the image on every mouse move. You need to set the initial position, and while the user is dragging, you need to keep on drawing and Updating the control.

Also another thing, you need to also redraw the previous shape just drawn before this with the background color.

So,
On MouseDown start drawing.
On MouseMove, you are drawing based on current position,
You draw the existing shape with the backcolor.

So it will seem to you that the existing shape is erased and new shape is created dynamically until mouseup.


:thumbsup:


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

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