【问题标题】:Detection of Scrollbar height and current Position检测滚动条高度和当前位置
【发布时间】:2018-07-05 09:58:54
【问题描述】:

我想检测表单中的滚动条高度和当前位置。

对于我正在使用的高度 systeminformation.horizontalscrollbarheight

如何检测当前位置?

编辑

我想从库中检测高度和位置。

【问题讨论】:

  • “当前位置”光标位置或表单位置是什么意思?
  • 滚动条的终点,,,假设如果用户滚动到表单的一半并停止,那么我想要滚动条的位置
  • Winforms 中有许多不同种类的滚动条。如果您谈论专用控件,那就太明显了。通常,当您将 AutoScroll 属性设置为 true 时会得到它们,然后您会从 AutoScrollPosition 中获取位置。有些控件根本不允许你找出来,ListView 就是一个例子。当您谈到“滚动到表单的一半”时,您需要 AutoScrollPosition。

标签: c# winforms


【解决方案1】:

使用 VerticalScroll 和 Horizo​​ntalScroll 更多信息MSDN

示例代码,显示表单滚动后的滚动信息:

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Scroll(object sender, ScrollEventArgs e)
        {
            this.label1.Text = $"scrollbarHeight: {SystemInformation.HorizontalScrollBarHeight}\n"
                                + $"verticalScrollPos: {this.VerticalScroll.Value}\n"
                                + $"horizontalScrollPos: {this.HorizontalScroll.Value}";
        }

        /// <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.label1 = new System.Windows.Forms.Label();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.label1);
            this.panel1.Location = new System.Drawing.Point(134, 165);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(458, 459);
            this.panel1.TabIndex = 0;
            this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(36, 23);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(64, 25);
            this.label1.TabIndex = 1;
            this.label1.Text = "label1";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(11F, 24F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.AutoScroll = true;
            this.ClientSize = new System.Drawing.Size(505, 421);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Scroll += new System.Windows.Forms.ScrollEventHandler(this.Form1_Scroll);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Label label1;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 1970-01-01
    • 2015-04-09
    • 2013-09-26
    • 2012-12-07
    • 1970-01-01
    • 2017-03-22
    相关资源
    最近更新 更多