【问题标题】:Custom Control to use Cursor Hand使用光标手的自定义控件
【发布时间】:2014-06-28 07:04:34
【问题描述】:

我在 C# 中创建了一个自定义控件,并且只要用户的光标悬停在自定义控件上,我希望光标显示为“手”。我在哪里放置代码来做这样的事情?

????.Cursor = Cursors.Hand;

为了使它在悬停在此自定义控件上时显示手形光标?

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";
}

【问题讨论】:

    标签: c#


    【解决方案1】:

    UserControl 派生自 Control,因此应该已经具有从该类继承的 Cursor 属性。您在代码/属性中没有看到 Cursor 属性吗?

    【讨论】:

    • 它在哪里?
    • 啊,我现在看到了。我很抱歉。谢谢
    • 我在网上搜索了一下,没找到。
    猜你喜欢
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多