小弟我就想知道如何实现。(事件和方法)

我就想知道怎么实现。。。(事件和方法)
本帖最后由 u011311854 于 2013-10-04 20:52:16 编辑
public partial class Form1 : Form
    {
       Point pt;
       public bool a ;
 
        public Form1()
        {
            InitializeComponent();
             
        }
 
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
          if (a==true)
          {
           Graphics dc = e.Graphics;
           Pen blue = new Pen(Color.Blue, 3);
           dc.DrawRectangle(blue, pt.X, pt.Y, 50, 50);
            
          }
 
          a = false;
            
        }
 
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            a = false;
            pt.X = e.X;
            pt.Y = e.Y;
            this.Text = string.Format("鼠标位置:({0},{1})", pt.X, pt.Y);
 
        }
 
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            a = true;
            
        }
 
        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            a = true;
        }


我想,鼠标抬起时,在panel上画,我想知道事件和画图的方法怎么调用。。。
这段代码,想通过a作为一个开关。但是实现不了。。。
谢谢!

------解决方案--------------------
private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            a = (e.Button == == MouseButtons.Left);  
        }

这些可以删除:
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            a = true;
             
        }
  
        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            a = true;
        }
------解决方案--------------------
鼠标按下,记录起始点坐标,将标记设为true
鼠标移动,如果标记为true,则设置结束点坐标,先清空,再画上矩形
鼠标抬起,记录结束点坐标,将标记设为false

------解决方案--------------------
a = true;
这句话下面加一句话:
Invalidate();
------解决方案--------------------
你的鼠标点击事件本身无法触发panel的paint事件,你必须再写一句调用paint事件,可以试试 panel1.Refresh();或者直接调panel1_Paint(object sender, PaintEventArgs e)
------解决方案--------------------

  private void panel1_Paint(object sender, PaintEventArgs e)
        {
            if (a==true)
            {
                Graphics dc = e.Graphics;
                Pen blue = new Pen(Color.Blue, 3);
                dc.DrawRectangle(blue, pt.X, pt.Y, 50, 50);
            }