使用鼠标在面板中绘制矩形 [英] Drawing Rectangle in a Panel using mouse

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

问题描述

所以我最近一直在尝试制作一个绘画应用程序来练习 C#.我过去 2 天的问题是创建矩形.

So I've been trying lately to make a paint application to practice C#. My problem for the past 2 days is in the creation of rectangles.

我做了一个面板,所以所有的绘图都放在那里.用户使用菜单选择他想要绘制的形状,然后他可以用鼠标开始绘制.

I made a panel so all the drawing are going there. The user selects the shape he wants to draw using a menu and he can start drawing with the mouse.

我遇到了以下两个问题:
1)即使我的起点在面板内,我移动鼠标并移到面板外,矩形被绘制在面板外,如下图所示.

I encounter 2 problems which are the following:
1) Even though my starting point was inside the panel, I moved the mouse and went outside the panel and the rectangle was drawn outside the panel as shown in the picture below.

2) 创建此矩形并尝试绘制另一个矩形后,前一个矩形被删除.所以在某种程度上我不能一次绘制 2 个矩形.

2) After I create this rectangle and I try to draw another, the previous one is deleted. So in a way I can't draw 2 rectangles at once.

这是我的源代码的一部分.

Here's a part of my source code.

Graphics mygraphics;
Pen lPen = new Pen(Color.Black); //Left Pen
Pen rPen = new Pen(Color.White); //Right pen
Point sp = new Point(0, 0);
private bool isRectangle; 
private bool isLeft, isRight; //isLeft -- Left Click, isRight -- Right Click

private void drawPanel_MouseMove(object sender, MouseEventArgs e)
{
   if (isRectangle == true)
   {
       if (e.Button == MouseButtons.Left)
       {
         isLeft = true;
         Point p = e.Location;
         int x = Math.Min(sp.X, p.X);
         int y = Math.Min(sp.Y, p.Y);
         int w = Math.Abs(p.X - sp.X);
         int h = Math.Abs(p.Y - sp.Y);
         mRect = new Rectangle(x, y, w, h);
         this.Invalidate();
       }
   }
}

private void drawPanel_MouseDown(object sender, MouseEventArgs e)
{
    sp = e.Location;           
}

private void drawPanel_MouseUp(object sender, MouseEventArgs e)
{
    isLeft = false;
}

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    e.Graphics.DrawRectangle(lPen, mRect);
}

我想要完成的是在我尝试绘制另一个矩形并将它们绘制在面板内后,我的矩形不会被删除.

What I want to accomplish is my rectangles not getting deleted after I try to draw another one and to draw them inside the panel.

你们有什么建议?

推荐答案

以下是我建议的最小更改:

Here are the minimal changes I suggest:

List<Rectangle> rectangles = new List<Rectangle>();
Rectangle mRect = Rectangle.Empty;


private void drawPanel_MouseUp(object sender, MouseEventArgs e)
{
   isLeft = false;
   rectangles.Add(mRect);
   mRect = Rectangle.Empty;
   drawPanel.Invalidate();
}

private void drawPanel_Paint(object sender, PaintEventArgs e)
{
   foreach (Rectangle rect in rectangles) e.Graphics.DrawRectangle(lPen, rect );
   e.Graphics.DrawRectangle(Pens.Orange, mRect);  // or whatever..
}

请注意,Paint 事件现在是 Panel 事件之一.请确保您挂钩Panel!!

Note that the Paint event now is the one of the Panel. Do make sure you hook it up with the Panel!!

还要注意我是如何用与其他矩形列表不同的颜色绘制当前矩形 mRect 的;这当然是可选的..

Also note how I draw the current rectangle mRect in a different color than the list of the other rectangles; this is of course optional..

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

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