c# - 绘制一个点

标签 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)));
        }



    }

}

最佳答案

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

关于c# - 绘制一个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7961930/

相关文章:

c# - 可以在 C# 中使用没有强名称程序集的 InternalVisibleTo 吗?

javascript - 如何调用datalist内按钮的asp回发?

c# - volatile 应该与(非并发)集合一起使用吗?

r - R :'expr' 中的曲线必须是函数、调用或包含 'x' 的表达式

javascript - 从插件发送消息到表单不适用于空值

c# - 如何重新创建 PowerShell $profile 变量?它在自定义主机中为空

R 代码不保存绘图图像

r - 如何使用 gridGraphics 转换分类 TreeMap

python - 使用 matplotlib 绘制直方图或散点图

matlab - 整个循环完成后,在循环中绘制并保存绘图