【问题标题】:Clicking Drawn Shapes in C# [duplicate]在 C# 中单击绘制的形状 [重复]
【发布时间】:2020-01-27 00:05:24
【问题描述】:

在这段代码中,我画了一个红色矩形。我想知道如何使这个矩形可点击。这样,当我点击它时,我可以让它变成蓝色。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Cursor = new Cursor(Application.StartupPath + "\\Cursor1.cur");
    }

    public Rectangle rect = new Rectangle(333, 333, 95, 95);

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        System.Drawing.SolidBrush fillRed = new System.Drawing.SolidBrush(Color.Red);
        System.Drawing.SolidBrush fillBlue = new System.Drawing.SolidBrush(Color.Blue);
        e.Graphics.FillRectangle(fillRed, rect);
    }

    private void rect_Click(object sender, EventArgs e)
    {
        System.Drawing.SolidBrush fillBlue = new System.Drawing.SolidBrush(Color.Blue);
        e.Graphics.FillRectangle(fillBlue, rect);
    }
}

【问题讨论】:

标签: c# winforms


【解决方案1】:

目标:能够按照OP的代码点击一个Shape、矩形,并改变它的颜色

步骤:

  • 画一个矩形
  • 然后我们检查点击事件是否发生在矩形的范围内
  • OnClick 我们将IsRectClick 标志设置为true
  • 重绘面板

编辑:添加了另一个功能

  • 现在还可以通过拖动来移动形状
  • 点击其他区域时重绘形状

Form1.cs

using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {

        bool isRectClick = false;
        System.Drawing.SolidBrush newColor;
        public Rectangle rect = new Rectangle(200, 100, 50, 50);
        public Form1()
        {
            InitializeComponent();

        }



        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            if (isRectClick == false)
            {

                System.Drawing.SolidBrush fillRed = new System.Drawing.SolidBrush(Color.Red);

                e.Graphics.FillRectangle(fillRed, rect);
            }
            else
            {


                e.Graphics.FillRectangle(newColor, rect);
            }

        }
        private void Panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.rect.Contains(e.Location))
            {
                newColor = new System.Drawing.SolidBrush(Color.Blue); //New Color
                isRectClick = true;
                panel1.Invalidate();
            }
            else
            {
                if (e.Button == MouseButtons.Left)
                {
                    rect = new Rectangle(e.X, e.Y, 50, 50);
                    panel1.Invalidate();
                }
            }

        }
        private void Panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int initialX = 0, initialY = 0; 

                rect.X = (e.X - 200) + initialX;
                rect.Y = (e.Y - 100) + initialY;

                panel1.Invalidate();
            }
        }
    }
}

Form1.Designer.cs

using System.Drawing;

namespace WindowsFormsApp2
{
    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.panel1 = new System.Windows.Forms.Panel();

            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 

            this.panel1.Location = new System.Drawing.Point(89, 63);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(646, 342);
            this.panel1.TabIndex = 0;
            this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
            this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Panel1_MouseDown);
          this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Panel1_MouseMove);

            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel panel1;


    }
}

输出

【讨论】:

  • 您可能错过了重点。 OP 需要一个可以可选择并且在某种程度上交互式的形状。形状,而不是控件的特定区域。所以你可以点击形状,也许调整它的大小,也许在选择时画一个边框,也许移动它,也许它在交互时也会引发事件。
  • 是的,我正在阅读这个问题,Op 说他需要在点击矩形时更改它的颜色。我错了按钮点击
  • 不是矩形,而是形状。你能抓住并移动那个矩形吗?当你悬停它时它会反应吗? (ETC。)。您只是在特定表面上绘制几何形状。
  • @Jimi ,我已经更新了答案,虽然 OP 希望能够在单击时更改形状的颜色,但也添加了移动。正如你所描述的,他没有谈论对悬停或移动等做出反应
猜你喜欢
  • 1970-01-01
  • 2014-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-26
  • 1970-01-01
  • 1970-01-01
  • 2021-08-10
相关资源
最近更新 更多