图形在移动时被删除. [英] Graphic is deleted when is moved.

查看:146
本文介绍了图形在移动时被删除.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!
我尝试了几天来解决我的问题,但没有任何结果,因此请在这里询问.

我创建了一个图形对象,并在其上绘制一些形状,例如线条,矩形等.
我不在Paint事件中编写任何代码,而所有代码仅在MouseUp,Down和Move事件中.

-=问题是=-
当我移动,最小化,调整图片大小时,图形中的一部分会被删除...
当窗口位于程序顶部时,效果相同..

在这里,我将为您提供事件中的代码,以查看我对行对象所做的操作:
-------------------------------------------------- --------

Hello!
I have a tryed from several days to solve my problem but without any result, so will ask here.

I have creage a graphic object and draw on it some shapes, like lines, rectangles etc.
I do not write any code in the Paint event and all code is just in the MouseUp, Down and Move Events.

-=The problem is=-
When i move, minimize, resize the picture, a partfrom the graphic is erased...
The same efect is when a window is on top of the program..

Here i will give you the code from my events to see what i''m doing for a line object:
----------------------------------------------------------

<pre lang="xml">
private void pbx_MouseDown(object sender, MouseEventArgs e)
        {
            PenList.Color = MainForm.btnColor1.BackColor;
            PenList.Width = float.Parse(MainForm.tbxSize.Text);

            StartPoint = e.Location;
            EndPoint = StartPoint;

            MouseIsDown = true;
        } 


-------------------------------------------------- --------


----------------------------------------------------------

private void pbx_MouseMove(object sender, MouseEventArgs e)
       {
           EndPoint = e.Location;
           PenList.Color = MainForm.btnColor1.BackColor;
           PenList.Width = float.Parse(MainForm.tbxSize.Text);
           if (e.Button == MouseButtons.Left)
           {
               if (MainForm.btnLine.Checked && MouseIsDown == true)
               {
                                 ControlPaint.DrawReversibleLine(Picture.PointToScreen(StartPoint), Picture.PointToScreen(EndPoint), Color.Black);
                   EndPoint = new Point(e.X, e.Y);
                   ControlPaint.DrawReversibleLine(Picture.PointToScreen(StartPoint), Picture.PointToScreen(EndPoint), Color.Black);
               }
           }


-------------------------------------------------- --------


----------------------------------------------------------

private void pbx_MouseUp(object sender, MouseEventArgs e)
        {
            MouseIsDown = false;
            EndPoint = e.Location;
            PenList = MainForm.CustomPen;
            PenList.Color = MainForm.btnColor1.BackColor;
            PenList.Width = float.Parse(MainForm.tbxSize.Text);
       
            if (MainForm.btnLine.Checked)
            {
                Graphic.DrawLine(PenList, StartPoint, EndPoint);
            }
        }



谢谢.



Thanks.

推荐答案

这是因为您做错了.实际上,这是一个非常常见的错误.

仅当您在每条WM_PAINT Windows消息上执行图形渲染时,图形渲染才会保留.为此,您应该处理事件System.Windows.Forms.Control.Paint,或者更好的是,在派生的Form类控件中重写虚拟方法System.Windows.Forms.Control.OnPaint.每当控件的某些区域需要重新绘制时,例如,当控件的一部分隐藏在其他窗口下时,都会触发消息WM_PAINT.通常,您需要对某些数据中的图形进行所有更改,并且在更改数据时(通过鼠标事件或其他任何操作),您需要强制发送消息.这是通过System.Windows.Forms.Control.Invalidate方法之一完成的.有时,最好使用带有参数(RegionRectangle)的方法Invalidate来使场景的一部分无效,这可以帮助您提高性能.

另外,您不应创建System.Drawing.Graphics的实例(请参阅我对问题的评论).您应该使用传递给事件System.Windows.Forms.Control.Paint的处理程序或虚拟方法System.Windows.Forms.Control.OnPaint的处理程序的实例作为事件参数.

请参阅:
http://msdn.microsoft.com/en-us/library/system. windows.forms.control.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.drawing. graphics.aspx [^ ].

请查看我过去对相关问题的回答:
在mdi子表单之间画线 [在面板上捕获图形 [ Paint是一种哪种好玩的方法? (DataGridViewImageCell.Paint(...)) [
This is because you are doing it wrong. Actually, this is a pretty common mistake.

The rendering of graphics persists only if you perform it on each WM_PAINT Windows message. For this purpose, you should handle the event System.Windows.Forms.Control.Paint, or, better yet, override the virtual method System.Windows.Forms.Control.OnPaint in your derived control of form class. The message WM_PAINT will be triggered each time some area of the control needs re-painting, for example, when a part of a control was hidden under some other window. Normally, you do all the changes in graphics in some data, and when you change the data (with your mouse events or by any other action), you need to force sending the message. This is done via one of the System.Windows.Forms.Control.Invalidate methods. Sometimes, it''s good to use the method Invalidate with parameters (Region or Rectangle), to invalidate just the part of the scene, which can help you to improve performance.

Also, you should not create an instance of System.Drawing.Graphics (please see my comment to the question). You should use the instance passed to your handler of the event System.Windows.Forms.Control.Paint or the virtual method System.Windows.Forms.Control.OnPaint as the event parameter.

Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx[^].

Please see my past answers to related questions:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^].

—SA


这篇关于图形在移动时被删除.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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