【问题标题】:Plotting a point绘制一个点
【发布时间】:2011-11-01 12:40:59
【问题描述】:

如果用户尝试调整表单大小(通过拖动表单的角),我的代码将绘制一个可缩放的图形。表单将在文本框中获得 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)));
        }



    }

}

【问题讨论】:

    标签: c# plot


    【解决方案1】:

    查看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();
    

    【讨论】:

    • 嗯...虽然我认为这将试图引用传入的参数。由于单击事件没有传入 PaintEventArgs(而不是 EventArgs),我认为它不会起作用....不过还是谢谢。
    • 你具体说的是什么参数?因为看起来您在 PaintEventArg 中唯一使用的是表单的图形对象。
    • 没错。但是,如果您查看方法“btnPlotGraph_Click”,它不会采用参数“PaintEventArgs”。 btnPlotGraph_Click 是由 WinForms 生成的方法(当您在表单上放置一个按钮时 - 它不附带传递参数 PaintEventArgs 的选项)。
    • 您只是在 PaintEventArgs 之外使用 Form 的 Graphic 对象。我建议的只是获取相同 Graphics 对象的另一种方法。应该没有区别。
    • 对不起,我想我可能是误会了。关键字“this”用于获取传递给方法的参数——对吗?我想在没有该参数的方法中使用 PaintEventArgs。在没有将 PaintEventArgs 传递给它的方法中使用关键字“this”将不起作用。还是我误会了?
    【解决方案2】:

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

    在你的Click事件中,你应该存储坐标,然后调用this.Invalidate()

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

    也可以手动创建图形对象,但更好的做法是调用Invalidate 要求表单刷新自身。

    【讨论】:

    • 谢谢。这帮助了很多
    猜你喜欢
    • 2020-05-30
    • 1970-01-01
    • 2016-01-20
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2020-07-13
    相关资源
    最近更新 更多