请叫我大星星 2018-12-21 02:58 采纳率: 50%
浏览 1082
已采纳

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

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

  • 写回答

2条回答 默认 最新

  • threenewbee 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;
        }
    }
    
    
    

    图片说明

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退