当前位置:编程学习 > C#/ASP.NET >>

GDI+画图问题,请高手指点

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Point P1 = new Point();
        Point P2 = new Point();
        bool isMouseDown = false;

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

     

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            
            Graphics g = e.Graphics;
            Rectangle rect1=new Rectangle(50, 50, 200, 100);
            Draw(g, rect1);
        }

        private void Draw(Graphics g, Rectangle rect)
        {
            GraphicsPath roundedRect = new GraphicsPath();
            roundedRect.AddArc(rect.X, rect.Y, rect.Width, rect.Height, 0, 360);
            roundedRect.CloseFigure();
            Region pathRegion = new Region(roundedRect);
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (isMouseDown == false)
            {
                isMouseDown = true;

                P1.X = e.X;
                P1.Y = e.Y;
            }
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown == true)
            {

                P2.X = e.X;
                P2.Y = e.Y;

                this.Invalidate();
            }
        }
    }
}


想在Form1_Paint里面调用Draw()函数来画一个椭圆,不知道为什么不能实现。

PS:
原来可以执行的代码是:
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Point P1 = new Point();
        Point P2 = new Point();
        bool isMouseDown = false;

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

      
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            
            Graphics g = e.Graphics;
       
            g.DrawEllipse(new Pen(Color.Red, 3), 1, 1, 5, 5);
            g.DrawRectangle (new Pen(Color .Blue ,3),P1.X>P2.X?P2.X:P1.X ,P1.Y>P2.Y?P2.Y:P1.Y,Math.Abs  (P2.X-P1.X),Math .Abs (P2.Y-P1.Y));
            
     
        }



        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (isMouseDown == false)
            {
                isMouseDown = true;

                P1.X = e.X;
                P1.Y = e.Y;
            }
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown == true)
            {

                P2.X = e.X;
                P2.Y = e.Y;

                this.Invalidate();
            }
        }
    }
}
--------------------编程问答-------------------- 你没有画呀,你的Draw函数最后加个g.DrawPath(Pens.Black, roundedRect); --------------------编程问答-------------------- private void Draw(Graphics g, Rectangle rect)
{
    GraphicsPath roundedRect = new GraphicsPath();
    roundedRect.AddArc(rect.X, rect.Y, rect.Width, rect.Height, 0, 360);
    roundedRect.CloseFigure();
    Region pathRegion = new Region(roundedRect);
    g.FillRegion(new SolidBrush(Color.Yellow), pathRegion);
}
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,