迷你涂料应用于c#windows窗体 [英] Mini Paint application in c# windows form

查看:81
本文介绍了迷你涂料应用于c#windows窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下代码的表单:

此代码基本上可以在运行时在窗体上创建一些形状,如矩形或圆形。 Bur问题是形状不持久,意味着点击任何形式导致形状去除。请建议使用鼠标事件绘制持久形状。

I have a form with follwing code:
This code is basically able to create some shape like rectangle or circle on form at runtime. Bur problem is shape is not persistent, mean clicking any where in form cause removal of shape. Please suggest to draw persistent shape using mouse event.

 public partial class Form1 : Form
    {
        Rectangle shape;
        Point p1 = new Point();
        Point p2 = new Point();
       
       
       
        public Form1()
        {
            InitializeComponent();
            //this.DoubleBuffered = true;

        }


        protected override void OnMouseDown(MouseEventArgs e)
        {

            shape = new Rectangle(e.X, e.Y, 0, 0);
            p1 = e.Location;
            this.Invalidate();
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {

            if (e.Button == MouseButtons.Left)
            {
                shape = new Rectangle(shape.Left, shape.Top, e.X - shape.Left, e.Y - shape.Top);

                p2 = e.Location;


            }
          this.Invalidate();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Pen pen = new Pen(colorDialog1.Color, 1);
            
            Graphics g = this.CreateGraphics();
          
                g.DrawEllipse(pen, shape);
               
        }       
}



这就是我所做的。此代码能够在运行时在窗体上绘制圆形,但是当我单击形状移除的任何位置时。我需要一个持久的形状。


This is what i did. This code is capable of drawing circle on form at run time, but as i click anywhere the shape removes. I need a persistent shape.

推荐答案

你需要仔细考虑你的目标是创建一个Paint程序......一个创建光栅(像素,位图)的程序。 ..或者如果你的目标是创建具有持久性的图形对象,可以选择,可以移动。



因为绘画是持久的WinForms,你需要在OwnerDrawn上下文(容器)的Paint事件中完成所有绘图。



对于图形对象绘图,你可以考虑使用Visual Basic C#中的PowerPack Shape和Line功能,或者考虑通过定义区域并将控件的可见区域(如面板)剪切到区域来创建自定义形状控件。



当然这两种技术可以混合:你可以采用一个面板并使其成六角形,然后使用Paint Event用渐变填充填充它。



创建一个油漆WinForms中的(raster)程序具有任何实际功能i是一项重要的工作,我建议你考虑使用WPF。



如果你搜索CP,你会发现一些令人印象深刻的绘图程序。
You need to think carefully if your goal is to create a Paint program ... one that creates raster (pixels, bitmaps) ... or if your goal is to create "graphic objects" that have persistence, can be selected, can be moved about.

For "painting" to be persistent in WinForms, you need to do all your drawing in the Paint Event of an OwnerDrawn context (container).

For "graphic object" drawing you can consider using the Visual Basic PowerPack Shape and Line features in C#, or consider creating custom shaped controls by defining Regions and clipping the visible areas of Controls, like Panels, to the Region.

Of course the two techniques can mix: you could take a Panel and make it hexagon shaped, then use the Paint Event to fill it with a gradient-fill.

Creating a Paint (raster) program in WinForms with any real functionality is a major piece of work, and I'd suggest you consider using WPF instead.

If you search CP, you will find some impressive drawing programs.


基本上,您需要在自己的数据中保留所有图形。图形不存储在任何地方。要呈现数据,您需要覆盖虚拟方法 System.Windows.Forms.Control.OnPaint 或处理事件 System.Windows.Forms.Control .Paint 。您将获得一个类 System.Drawing.Graphics 的实例,您可以使用它来渲染图形。当您需要修改图片时,在数据中添加更改并调用 System.Windows.Forms.Control.Invalidate 。为了防止闪烁,请使用双缓冲。



请参阅我过去的答案:

在面板上捕获图纸 [ ^ ],

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

mdi子表单之间的绘制线 [ ^ ],

如何加速我的vb.net应用程序? [ ^ ],

用C#.net鼠标滚轮缩放图像 [ ^ ]。



-SA
Basically, you need to persist all graphics in your own data. Graphics is not stored anywhere. To render data, you need to override the virtual method System.Windows.Forms.Control.OnPaint or handle the event System.Windows.Forms.Control.Paint. You will get an instance of the class System.Drawing.Graphics which you can use to render your graphics. When you need modify the picture, add the changes in your data and call System.Windows.Forms.Control.Invalidate. To prevent flicker, use double buffering.

Please see also my past answers:
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
How to speed up my vb.net application?[^],
Zoom image in C# .net mouse wheel[^].

—SA


请注意,您必须刷新用户使用鼠标绘制的内容。检查无效呼叫。
Beware you must be refreshing what the user paints with the mouse. Check the Invalidate call.


这篇关于迷你涂料应用于c#windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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