簡體   English   中英

面板的自定義滾動條滾動不順暢

[英]Custom scrollbar for panel does not scroll smoothly

customBtn1 是自定義滾動條。 下面的代碼示例有效,但滾動動作是緊張和口吃。 它滾動不流暢。 知道為什么會發生這種情況以及如何解決嗎?

    int PreviousBarLoc;
    private void MainForm_Load(object sender, EventArgs e)
    {
        PreviousBarLoc= customBtn1.Location.Y;
    }

    //move the scrollbar up and down when the user drags it
    private void customBtn1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        { MouseDownLocation = e.Location; }
    }
    private Point MouseDownLocation;
    private void customBtn1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            customBtn1.Top = e.Y + customBtn1.Top - MouseDownLocation.Y;
        }
    }
    
    //scroll the panel
    private void customBtn1_LocationChanged(object sender, EventArgs e)
    {
        int locDifference = customBtn1.Location.Y - PreviousBarLoc;
        if (steamSrvListPnl.VerticalScroll.Value + locDifference <= 255 && steamSrvListPnl.VerticalScroll.Value + locDifference >= 0)
        {
            steamSrvListPnl.VerticalScroll.Value += locDifference;
        }
        PreviousBarLoc= customBtn1.Location.Y;

    }

您的增量不太正確。 它目前的寫法,如果你向下拖動鼠標然后開始向上拖動,它不會向上滾動,因為你當前的 eY 仍然低於原始的 MouseDownLocation.Y。 我不完全確定您的設置,但下面是一個示例,說明如何在按下鼠標左鍵並在按鈕上拖動時平滑滾動:

    private int _prevY = 0;
    private bool _mouseDown = false;
    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            _mouseDown = true;
            _prevY = e.Y;
        }
    }

    private void button1_MouseMove(object sender, MouseEventArgs e)
    {
        if (_mouseDown)
        {
            panel1.VerticalScroll.Value = Math.Max(panel1.VerticalScroll.Minimum, Math.Min(panel1.VerticalScroll.Maximum, panel1.VerticalScroll.Value + e.Y - _prevY));
            _prevY = e.Y;
        }
    }

    private void button1_MouseUp(object sender, MouseEventArgs e)
    {
        if (_mouseDown)
        {
            _mouseDown = false;
            _prevY = 0;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM