C#:通过鼠标位置更改面板位置

时间:2017-09-02 17:13:32

标签: c# location panel

我有一个由面板和标签组成的用户控件。 MinPanelValuePanelMaxPanel

我试图让用户像在TrackBar中那样拖动面板。

我的问题是,当我点击并尝试拖动MinPanel时,它会从0开始跳转,然后跳到200,然后转到其他随机值。它似乎试图在每次拖动时重置为默认值。

public partial class ToolboxCustomTrackBar : UserControl
{
    private int min = 0;
    private int max = 1000;
    private int selectedMin = 0;
    private int selectedMax = 1000;
    private int selectedValue = 400;
    private int selectionWidth = 0;
    private int labelHeight = 10;

    public int Min
    {
        get { return min; }
        set
        {
            min = value;
            Invalidate();
        }
    }
    public int Max
    {
        get { return max; }
        set
        {
            max = value;
            Invalidate();
        }
    }
    public int SelectedMin
    {
        get { return selectedMin; }
        set
        {
            selectedMin = value;
            Invalidate();
        }
    }
    public int SelectedMax
    {
        get { return selectedMax; }
        set
        {
            selectedMax = value;
            Invalidate();
        }
    }
    public int SelectedValue
    {
        get { return selectedValue; }
        set { selectedValue = value; Invalidate(); }
    }
    public int LabelHeight
    {
        get { return labelHeight; }
        set { labelHeight = value; Invalidate(); }
    }

    public ToolboxCustomTrackBar()
    {
        InitializeComponent();
        SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

        backdropPanel.Width = Width;
        backdropPanel.Height = Height;

        SelectedMin = Min;
        SelectedMax = Max;
        SelectedValue = (Max - Min) / 2;
        selectionWidth = Max - Min;
        Invalidate();

        backdropPanel.BackColor = Color.LightBlue;
        minPanel.BackColor = Color.DarkRed;
        maxPanel.BackColor = Color.DarkGreen;
        valuePanel.BackColor = Color.Black;

        backdropPanel.Location = new Point(Min, backdropPanel.Location.Y);
    }

    private void ToolboxCustomTrackBar_Paint(object sender, PaintEventArgs e)
    {
        backdropPanel.Location = new Point(0, LabelHeight);
        backdropPanel.Width = Width;
        backdropPanel.BackColor = Color.AliceBlue;

        minPanel.Location = new Point(SelectedMin * Width / (Max - Min), backdropPanel.Location.Y);

        maxPanel.Location = new Point(SelectedMax * Width / (Max - Min), backdropPanel.Location.Y);

        valuePanel.Location = new Point((SelectedValue) * Width / (Max - Min), backdropPanel.Location.Y);

        minLabel.Location = new Point(SelectedMin - (minLabel.Width / 2) + (minPanel.Width / 2), backdropPanel.Location.Y - LabelHeight);
        minLabel.Text = SelectedMin.ToString();

        maxLabel.Location = new Point(SelectedMax - (maxLabel.Width / 2) + (maxPanel.Width / 2), backdropPanel.Location.Y - LabelHeight);
        maxLabel.Text = SelectedMax.ToString();

        valueLabel.Location = new Point(SelectedValue - (valueLabel.Width / 2) + (valuePanel.Width / 2), backdropPanel.Location.Y - LabelHeight);
        valueLabel.Text = SelectedValue.ToString();
    }

    private void minPanel_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left) return; //ensure user left-clicked

        int pointedValue = Min + e.X * (Max - Min) / Width; //selectionWidth?
        SelectedMin = pointedValue;
    }
}

我已经删除了一些不相关的位。

1 个答案:

答案 0 :(得分:0)

我不完全理解原因,但我需要使用+=代替=

private void minPanel_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button != MouseButtons.Left) return; //ensure user left-clicked

    int pointedValue = Min + e.X * (Max - Min) / Width;
    SelectedMin += pointedValue;
}