weixin_44198768
请叫我大星星
采纳率50%
2018-12-21 02:58 阅读 888

C#中怎样通过在获取文本框输入的坐标后绘图

需求是这样的:
C#winform中,创建一个文本框,然后输入画什么图形以及大小,一开始画笔是在0,0。比如说我输入:circle 20 ,就代表在0,0开始画一个直径为20的圆。
然后还有个需求就是如果输入:moveTo 100,100 circle 20, 就代表画笔移动到100,100 然后画圆。不用实时显示,只要在文本框中输入完成后显示就可以。该怎样完成这个需求。求大神指点!!
不懂该怎样将文本框获取的数据再发送至绘图!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    caozhy 从今以后生命中的每一秒都属于我爱的人 2018-12-20 23:00

    给你完整做了一个

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Q717883
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                currPt = new Point(0, 0);
            }
    
            private Point currPt { get; set; }
    
            private void pictureBox1_Paint(object sender, PaintEventArgs e)
            {
                e.Graphics.FillRectangle(Brushes.White, pictureBox1.DisplayRectangle);
                foreach (var line in textBox1.Lines.Select(x => x.ToLower().Trim()).Where(x => x != ""))
                {
                    if (line.StartsWith("moveto"))
                    {
                        int x = int.Parse(line.Replace("moveto", "").Replace(" ", "").Trim().Split(',')[0]);
                        int y = int.Parse(line.Replace("moveto", "").Replace(" ", "").Trim().Split(',')[1]);
                        currPt = new Point(x, y);
                    }
                    else if (line.StartsWith("circle"))
                    {
                        int r = int.Parse(line.Replace("circle", "").Trim().Split(',')[0]);
                        e.Graphics.DrawEllipse(Pens.Black, currPt.X - r, currPt.Y - r, r + r, r + r);
                    }
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                pictureBox1.Refresh();
            }
        }
    }
    
    
    namespace Q717883
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.button1 = new System.Windows.Forms.Button();
                this.pictureBox1 = new System.Windows.Forms.PictureBox();
                ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
                this.SuspendLayout();
                // 
                // textBox1
                // 
                this.textBox1.Location = new System.Drawing.Point(11, 14);
                this.textBox1.Multiline = true;
                this.textBox1.Name = "textBox1";
                this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
                this.textBox1.Size = new System.Drawing.Size(207, 386);
                this.textBox1.TabIndex = 0;
                this.textBox1.Text = "moveTo 100,100\r\ncircle 20";
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(130, 408);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(87, 26);
                this.button1.TabIndex = 1;
                this.button1.Text = "draw";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // pictureBox1
                // 
                this.pictureBox1.Location = new System.Drawing.Point(227, 15);
                this.pictureBox1.Name = "pictureBox1";
                this.pictureBox1.Size = new System.Drawing.Size(473, 418);
                this.pictureBox1.TabIndex = 2;
                this.pictureBox1.TabStop = false;
                this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(714, 446);
                this.Controls.Add(this.pictureBox1);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.textBox1);
                this.Name = "Form1";
                this.Text = "Form1";
                ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.PictureBox pictureBox1;
        }
    }
    
    
    

    图片说明

    点赞 评论 复制链接分享
  • qq_23126581 郭老师的小迷弟雅思莫了 2018-12-20 20:24

    你先要知道怎么去画圆,你所说的无非就是参数问题,你是不是应该先考虑怎么画一个固定的圆,然后将参数提取出来?

    点赞 1 评论 复制链接分享

相关推荐