需求是这样的:
C#winform中,创建一个文本框,然后输入画什么图形以及大小,一开始画笔是在0,0。比如说我输入:circle 20 ,就代表在0,0开始画一个直径为20的圆。
然后还有个需求就是如果输入:moveTo 100,100 circle 20, 就代表画笔移动到100,100 然后画圆。不用实时显示,只要在文本框中输入完成后显示就可以。该怎样完成这个需求。求大神指点!!
不懂该怎样将文本框获取的数据再发送至绘图!!
C#中怎样通过在获取文本框输入的坐标后绘图
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
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; } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
- ¥15 电脑蓝屏logfilessrtsrttrail问题
- ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
- ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
- ¥15 Java+vue部署版本反编译
- ¥100 对反编译和ai熟悉的开发者。
- ¥15 带序列特征的多输出预测模型
- ¥15 Python 如何安装 distutils模块
- ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
- ¥15 资源泄露软件闪退怎么解决?