【问题标题】:How to get or calculate the sizes of a Panel (including internal ones) to use Custom Scrollbar如何获取或计算面板(包括内部面板)的大小以使用自定义滚动条
【发布时间】:2021-07-17 12:15:53
【问题描述】:

请告诉我如何找出面板的内部尺寸 (MAX) 以便滚动到正确的位置。

截图中的数字是“随机生成的”,代码和项目描述了一切。

获取最后一个内部元素的坐标时,结果值比用于获取该元素的值小很多倍。

public Form_Main() {
    InitializeComponent();

    flp_Elements.MouseWheel += Flp_Els_MouseWheel;
}

// Scrolling the panel (Flp_Els) with the mouse wheel
private void Flp_Els_MouseWheel(object sender, MouseEventArgs e) {
    int step = customScrollBar1.Value -= e.Delta > 0 ? customScrollBar1.SmallStep : -customScrollBar1.SmallStep;
    customScrollBar1.Value = Math.Max(customScrollBar1.Minimum, Math.Min(step, customScrollBar1.Maximum));

    ScrollValue = (int)customScrollBar1.Value;
}

// Scroll the panel (Flp_Els) with the slider on the ScrollBar
private void customScrollBar1_ValueChanged(object sender, EventArgs e) {
    ScrollValue = customScrollBar1.Value;
}

// Method for overriding the maximum value, when adding elements to the panel
private void flp_Elements_ControlAdded(object sender, ControlEventArgs e) {
    // it managed to scroll beyond (3820) max - it does not move further,
    // but the number of max is greater than above

    // ! HOW TO DETERMINE THE SIZES OF THE PANEL, WHEN IT IS FILLED WITH ELEMENTS AND HIDES THEM

    customScrollBar1.Maximum = flp_Elements.VerticalScroll.Maximum + flp_Elements.VerticalScroll.LargeChange * 8;
    flp_Elements.VerticalScroll.Maximum = customScrollBar1.Maximum;
}

// WITHOUT IT, THE CONTENT OF THE PANEL WOULD NOT MOVE
// Setting panel limits
public int scrollValue = 0;
public int ScrollValue {
    get { return scrollValue; }
    set {
        scrollValue = value;

        // Minimum = 0
        if (scrollValue < 0) scrollValue = 0;

        // Maximum
        if (scrollValue > flp_Elements.VerticalScroll.Maximum)
            scrollValue = flp_Elements.VerticalScroll.Maximum;

        flp_Elements.VerticalScroll.Value = scrollValue;
    }
}

}

目前:面板使用 ScrollBar 滚动,但为了让一切正常工作,您需要这个数字。尝试了不同的选项,但它们不能按需要工作:

  1. First option
  2. The second option (described in the Calculations section)

【问题讨论】:

  • 当 ScrollableControl 包含位于 DisplayRectangle 之外的子控件时,其大小由 Control.PreferredSize 返回。您的最小滚动值始终为 0,real 最大值始终考虑 ScrollableControl.ClientSize,需要从最大值中减去(与水平或垂直滚动​​条相关的维度)。
  • 你能写一段代码来描述你想说的话吗?
  • 要编写一段代码,我需要写下整个 ScrollBar 自定义控件。滚动条与承载它的控件协作,以发送/接收与客户区位置、调整大小事件、鼠标事件等相关的消息。 - 您当前正在计算滚动条而不考虑承载滚动条的控件的 ClientSize(它就是在这种情况下,看起来 Scrollbar 不是 ScrollableControl 的子控件)。如前所述,实际的最大滚动值为,例如,ScrollBar.Maximum (= Control.PreferredSize.Height) - Control.ClientSize.Height
  • 为了测试这个行为,构建一个自定义控件继承,例如,面板类。覆盖 WndProc 并处理 WM_MOUSEHWHEELWM_VSCROLLWM_HSCROLL(至少)。打印 VerticalScroll.ValueVerticalScroll.MaximumVerticalScroll.Maximum - ClientSize.HeightHorizontalScroll.Maximum - ClientSize.WidthPreferredSizeClientSize 的值。所以你可以看到是什么。
  • 试试this。将 pnlInner 替换为您的 flp_Elements 控件。

标签: c# windows visual-studio winforms


【解决方案1】:
private void flp_Container_ControlAdded(object sender, ControlEventArgs e) {
  // For Vertical Scrolling
  flp_Container.VerticalScroll.Maximum = flp_Container.GetPreferredSize(flp_Container.Size).Height - flp_Container.ClientSize.Height;
  ScrolBar1.Maximum = flp_Container.VerticalScroll.Maximum;

  // For Horizontal Scrolling
  flp_Container.HorizontalScroll.Maximum = flp_Container.GetPreferredSize(flp_Container.Size).Width - flp_Container.ClientSize.Width;
  ScrolBar1.Maximum = flp_Container.HorizontalScroll.Maximum;      
}

【讨论】:

    猜你喜欢
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    相关资源
    最近更新 更多