c# - 在拆分器移动时刷新 SplitContainer 的面板

标签 c# winforms

我有一个带有两个面板的 SplitContainer。当我拖动拆分器以调整两个面板的大小时,我在拖动时看到一个灰色条。在我释放鼠标按钮之前,面板实际上并没有被重新绘制。如何让面板随着我拖动拆分器刷新?

Fwiw,这是在 Delphi 中通过将 Splitter 控件的“ResizeStyle”设置为“rsUpdate”来实现的。

我已尝试将以下代码放入 SplitterMoving 事件中,没有任何可见的更改。

private void splitCont_SplitterMoving(object sender, SplitterCancelEventArgs e)
{
    splitCont.Invalidate();
    //also tried this:
    //splitCont.Refresh();
}

最佳答案

您可以尝试使用鼠标事件,详见 in this page :

    //assign this to the SplitContainer's MouseDown event
    private void splitCont_MouseDown(object sender, MouseEventArgs e)
    {
        // This disables the normal move behavior
        ((SplitContainer)sender).IsSplitterFixed = true;
    }

    //assign this to the SplitContainer's MouseUp event
    private void splitCont_MouseUp(object sender, MouseEventArgs e)
    {
        // This allows the splitter to be moved normally again
        ((SplitContainer)sender).IsSplitterFixed = false;
    }

    //assign this to the SplitContainer's MouseMove event
    private void splitCont_MouseMove(object sender, MouseEventArgs e)
    {
        // Check to make sure the splitter won't be updated by the
        // normal move behavior also
        if (((SplitContainer)sender).IsSplitterFixed)
        {
            // Make sure that the button used to move the splitter
            // is the left mouse button
            if (e.Button.Equals(MouseButtons.Left))
            {
                // Checks to see if the splitter is aligned Vertically
                if (((SplitContainer)sender).Orientation.Equals(Orientation.Vertical))
                {
                    // Only move the splitter if the mouse is within
                    // the appropriate bounds
                    if (e.X > 0 && e.X < ((SplitContainer)sender).Width)
                    {
                        // Move the splitter & force a visual refresh
                        ((SplitContainer)sender).SplitterDistance = e.X;
                        ((SplitContainer)sender).Refresh();
                    }
                }
                // If it isn't aligned vertically then it must be
                // horizontal
                else
                {
                    // Only move the splitter if the mouse is within
                    // the appropriate bounds
                    if (e.Y > 0 && e.Y < ((SplitContainer)sender).Height)
                    {
                        // Move the splitter & force a visual refresh
                        ((SplitContainer)sender).SplitterDistance = e.Y;
                        ((SplitContainer)sender).Refresh();
                    }
                }
            }
            // If a button other than left is pressed or no button
            // at all
            else
            {
                // This allows the splitter to be moved normally again
                ((SplitContainer)sender).IsSplitterFixed = false;
            }
        }
    }

关于c# - 在拆分器移动时刷新 SplitContainer 的面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6521731/

相关文章:

visual-studio - Visual Studio 2008 中用于帮助本地化的工具

c# - WPF 自定义 LED 复选框

c# - 克服 32 位限制,当使用由数百万个单位翻译的 System.Drawing.Graphics 表面时

c# - 寻找 .NET C# 每日提示组件

c# - 是否可以重新启动/重新连接 SignalR 连接?

c# - 等待完成所有异常处理任务

c# - POST 变量上每个循环的 ASHX C#

c# - 使用 .net sdk 在 Azure 数据工厂 V2 中重新运行事件

c# - 将表行解析为嵌套集合

winforms - Windows 窗体应用程序中的 OOP 与 case 语句