绘制一个点

时间:2011-11-01 02:41:19

标签: c# plot

我有一些代码,它会绘制一个图表,如果用户试图调整表单大小(通过拖动表单的角落),则可以进行缩放。表单将在文本框中获得x和y坐标。按下按钮时,我想绘制他们指示的点。但是,Click事件包含参数Object Sender和EventArgs e。 OnPaint方法(我为了绘制图形而覆盖)具有参数PaintEventArgs。

因此,当单击该按钮时,我无法执行以下代码:

g.DrawString("♫", new Font("Calibri", 12), new SolidBrush(Color.HotPink), (PlotArea.X + (7 - xMin)* PlotArea.Width/(xMax - xMin)), (PlotArea.Bottom - (6 - yMin) * PlotArea.Height / (yMax - yMin)));

这是因为“g”属于PaintEventArgs类型。我如何解决这个问题,以便在onClick方法中我可以绘制坐标?

我的代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PlotIt
{
    public partial class Form1 : Form
    {
        public static List<TheList> GraphPoints = new List<TheList>();

        //Define the drawing area
        private Rectangle PlotArea;
        //Unit defined in world coordinate system:
        private float xMin = 0f;
        private float xMax = 10f;
        private float yMin = 0f;
        private float yMax = 10f;
        //Define the offset in pixel:
        private int offset = 150;
        Graphics g;

        Boolean buttonPressed = false; 
        public Form1()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.BackColor = Color.White;

        }

        protected override void OnPaint(PaintEventArgs e)
        {
            g = e.Graphics;

            //Calculate the location and size of the drawing area
            //within which we want to draw the graphics:
            Rectangle rect = ClientRectangle;

            PlotArea = new Rectangle(rect.Location, rect.Size);
            PlotArea.Inflate(-offset, -offset);

            g.DrawRectangle(Pens.Black, PlotArea);

            Pen aPen = new Pen(Color.Green, 3);
            g.DrawLine(aPen, Point2D(new PointF(5, 0)), Point2D(new PointF(5, 10)));
            g.DrawLine(aPen, Point2D(new PointF(0, 5)), Point2D(new PointF(10, 5)));

            aPen.Dispose();
            g.Dispose();


        }



        private PointF Point2D(PointF ptf)
        {
            PointF aPoint = new PointF();

            aPoint.X = PlotArea.X + (ptf.X - xMin) * PlotArea.Width / (xMax - xMin);

            aPoint.Y = PlotArea.Bottom - (ptf.Y - yMin) * PlotArea.Height / (yMax - yMin);

            return aPoint;
        }



        private void btnPlotGraph_Click(object sender, EventArgs e)
        {



            g.DrawString("♫", new Font("Calibri", 12), new SolidBrush(Color.HotPink),    (PlotArea.X + (7 - xMin)* PlotArea.Width/(xMax - xMin)), (PlotArea.Bottom - (6 - yMin) * PlotArea.Height / (yMax - yMin)));
        }



    }

}

2 个答案:

答案 0 :(得分:2)

查看Control.CreateGraphics方法。这应该允许您获得所需的Graphic对象。

Graphics g = this.CreateGraphics();
g.DrawString("♫", new Font("Calibri", 12), new SolidBrush(Color.HotPink), (PlotArea.X + (7 - xMin)* PlotArea.Width/(xMax - xMin)), (PlotArea.Bottom - (6 - yMin) * PlotArea.Height / (yMax - yMin)));
g.Dispose();

答案 1 :(得分:2)

有一种更合适的方法可以做到这一点。

Click事件中,您应该存储坐标,然后拨打this.Invalidate()

这将导致您的表单重绘自身,触发Paint事件。

也可以手动创建图形对象,但最好通过调用Invalidate让表单刷新自己。