表单大小调整时出现问题 [英] problem when Form resizing

查看:74
本文介绍了表单大小调整时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#2005编写绘图程序.
代码:

I''m writing a drawing program with C # 2005.It has worked.

Code:

 private bool mouseDown = false ;
 private Point lastPoint = Point.Empty;
 private string color = "blue" ;
 private Graphics g;
 private Pen p;
 public Form1()
{
    InitializeComponent();
    g = CreateGraphics();
    p = new Pen(Color.FromName(color),3);
}



private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    mouseDown = true;

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



private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    if(lastPoint.Equals(Point.Empty))
        lastPoint = new Point( e.X, e.Y ) ;
    if (mouseDown){
     Point pMousePos = new Point( e.X, e.Y ) ;
     g.DrawLine( p, pMousePos, lastPoint ) ;
 }
 lastPoint = new Point ( e.X, e.Y ) ;
 //p.Dispose();
 //g.Dispose();

}



我正在用C#2005编写绘图程序.

成功了.

但是,当我调整窗体的大小时,只能绘制Form1的旧宽度和高度.

调整窗体大小时如何更改画布大小?

我尝试了p.Dispose();g.Dispose();.但是失败了.
谢谢朋友.我找到了这段代码:



I''m writing a drawing program with C # 2005.

It succeeded.

But when I resize the Form, I can only draw in the old width and height of Form1.

How to I change the canvas size when Form resize?

I tried p.Dispose(); and g.Dispose();. But failed.
Thank friends.I found this code:

List<Point> points = new List<Point>();

       private void Form1_MouseMove(object sender, MouseEventArgs e)
       {
           if (e.Button == MouseButtons.Left)
           {
               points.Add(e.Location);

               Invalidate();
           }

       }

       private void Form1_Paint(object sender, PaintEventArgs e)
       {
           if (points.Count > 2)
           {
               e.Graphics.DrawLines(Pens.Black, points.ToArray());
           }

       }


唯一的问题是,当MouseUp


only a problem is can not draw a new line when the MouseUp

推荐答案

不可以,不能画新线!都错了.

您需要在事件Paint或重写的虚拟方法OnPaint的处理程序中呈现所有图形.您的鼠标事件应该只修改某些数据并使用System.Windows.Forms.Control.Invalidate触发图像无效.

另请参阅我过去的答案以获取更多详细信息:
Paint是一种哪种好玩的方法? (DataGridViewImageCell.Paint(...)) [在mdi子窗体之间画线 [在面板上捕获图形 [如何从旧图纸中清除面板 [ ^ ].

—SA
No, no! All wrong.

You need to render all graphic in the handler of the event Paint or the overridden virtual method OnPaint; you mouse events should only modify some data and trigger invalidation of the image using System.Windows.Forms.Control.Invalidate.

See also my past answers for more detail:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
How do I clear a panel from old drawing[^].

—SA


此设计是一个非常常见的错误.从原则上讲,这甚至可能可行,但是我知道有很多尝试基于相似的想法(实际上好一点)来完成应用程序或组件,但都失败了.

有两种有效的方法.在这两种情况下,都不应将任何控件(甚至窗口或窗体)都用作图形元素.在您看来,它们似乎已经具有您需要的固有功能,但这是错误的.相反,它们具有的功能会让您迷失不必要的复杂性.

1)使用轻量级方法.在一个控件中创建所有图形基元.您需要在重写方法Control.OnPaint的Control.Paint处理程序中呈现所有图形.为了进行渲染,请使用事件参数参数中提供的System.Drawing.Graphics实例(在两种情况下).要更改图像,请使用Control.Invalidate.为了获得更好的性能,请使用带有参数(矩形或区域)的Control.Invalidate仅使场景的一部分无效.

2)使用WPF.您根本不需要进行任何渲染.相反,您应该使用Canvas并将图形基元(例如线条,矩形等,但也可以将控件放置)放在画布上.这真的很简单.您甚至可以在矢量编辑器中绘制基元,将矢量图形作为XAML导入,将保存的XAML代码放入资源字典中,然后在画布上重复使用.我建议使用出色的开源编辑器InkScape.

最后的笔记.没有可以使用MDI的情况.这是我的微软最失败的发明之一.即使Microsoft也不鼓励使用MDI.没有可靠的软件产品,甚至没有使用MDI可以接受的质量产品.如果使用它,您只会吓跑客户.最简单的替代方法是基于TabControl的选项卡式界面.

—SA
This design is a very common mistake. In principle, it may be even feasible, but I knew so many attempts to complete the application or component base on similar (actually a bit better) ideas — all failed.

There are two approaches which work. In both cases, you should not use any controls (not even windows or forms) as graphics elements. Maybe it seems to you they already have intrinsic functionality you need, but this is wrong. Just the opposite, they have functionality which will make you lost on unneeded complexity.

1) Use light-weight approach. Create all graphics primitive in one control. You need to render all graphics in the handler of Control.Paint of overridden method Control.OnPaint. For rendering, use the instance of System.Drawing.Graphics supplied in the event arguments parameter (in both cases). To change image, use Control.Invalidate. For better performance, use Control.Invalidate with a parameter (Rectangle or Region), to invalidate just a part of the scene.

2) Use WPF. You don''t need to do any rendering at all. Instead, you should use Canvas and put graphical primitives (such as lines, rectangles, etc., but you can put controls as well) on the canvas. This is really simple. You can even draw primitives in a vector editor, import vector graphics as XAML, put the saved XAML code to resource dictionary and reuse on your canvas. I recommend using wonderful Open Source editor InkScape.

Final note. There are no situations when MDI can be used. This is one of the most unsuccessful inventions my Microsoft. MDI is strongly discourages even by Microsoft. There are no solid software products or even products with acceptable quality using MDI. You would simply scare off your customers if you use it. The simplest nice alternative is tabbed interface, based on TabControl.

—SA


这篇关于表单大小调整时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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