繁体   English   中英

C#自定义控件单击事件

[英]c# Custom control click event

我已经制作了一个自定义控件,并且想添加一个click事件,以便当用户在控件上的任何位置单击时,它将返回用户在控件上单击的位置。 例如,如果用户单击栏的中间,则本质上将向我返回足够的信息供我计算50%,并且用户单击了中间。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace CustomRangeBar
{
    public partial class RangeBar : UserControl
    {
        public RangeBar()
        {
            InitializeComponent();
            label1.ForeColor = Color.Black;
            this.ForeColor = SystemColors.Highlight; // set the default color the rangeBar
            this.Click += new EventHandler(RangeBar_Click); 
        }

        protected float percent = 0.0f; // Protected because we don't want this to be accessed from the outside
        // Create a Value property for the rangeBar
        public float Value
        {
            get
            {
                return percent;
            }
            set
            {
                // Maintain the value between 0 and 100
                if (value < 0) value = 0;
                else if (value > 100) value = 100;
                percent = value;
                label1.Text = value.ToString();
                //redraw the rangeBar every time the value changes
                this.Invalidate();
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Brush b = new SolidBrush(this.ForeColor); //create brush that will draw the background of the range bar
            // create a linear gradient that will be drawn over the background. FromArgb means you can use the Alpha value which is the transparency
            LinearGradientBrush lb = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height), Color.FromArgb(255, Color.White), Color.FromArgb(50, Color.White), LinearGradientMode.Vertical);

            // calculate how much has the rangeBar to be filled for 'x' %
            int width = (int)((percent / 100) * this.Width);
            e.Graphics.FillRectangle(b, 0, 0, width, this.Height);
            e.Graphics.FillRectangle(lb, 0, 0, width, this.Height);
            b.Dispose(); lb.Dispose();
        }

        private void RangeBar_SizeChanged(object sender, EventArgs e)
        {
            // maintain the label in the center of the rangeBar
            label1.Location = new Point(this.Width / 2 - 21 / 2 - 4, this.Height / 2 - 15 / 2);
        }

    }
}

public void RangeBar_Click(object obj, EventArgs ea)
{
    // This get executed if the pictureBox gets clicked
    label1.text = "Increment 1";
}

OnClick不是覆盖或订阅的好函数,因为它不会告诉您所要查找的单击发生的位置。

您想要的是OnMouseClick ,其中包括点击点的x,y。

protected override void OnMouseClick(MouseEventArgs e)
{
    int x = e.X;
    int y = e.Y;

    //Do your calculation here.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM