重画和闪烁问题

发布于 2024-08-27 05:15:43 字数 622 浏览 7 评论 0原文

我有一个 Outlook 风格的应用程序。所以基本上我的左侧有一个侧边栏,右侧有一个托管内容的面板控件(pnlMainBody)。

内容通常是一个用户控件,当用户单击侧栏中的相应按钮时,我将其添加到面板中。我将用户控件添加到面板的方式如下:

// _pnlEmails is the User Control that I am adding to the panel
_pnlEmails = new pnlEmails();
_pnlEmails.Dock = DockStyle.Fill;
this.pnlMainBody.Controls.Add(_pnlEmails);

我添加到主面板的一些用户控件在 UI 方面相当复杂。因此,当 this.pnlMainBody.Controls.Add(_pnlEmails); 触发时,我看到该控件出现在屏幕上,然后它调整自身大小以填充面板控件的主体。

实际上它很丑陋,所以我想知道是否有一种方法可以在实际完成调整大小之前不显示调整大小?

我尝试将用户控件的 .Visible 设置为 false。我尝试过 .SuspendLayout,但都无济于事。

有没有办法做到这一点,使屏幕转换平滑?

I have an Outlook style app. So basically I have a sidebar on the left and on the right I have a panel control (pnlMainBody) that hosts content.

The content is typically a user control that I add to the panel when user clicks appropriate button in the side bar. The way I add the user control to the panel is as follows:

// _pnlEmails is the User Control that I am adding to the panel
_pnlEmails = new pnlEmails();
_pnlEmails.Dock = DockStyle.Fill;
this.pnlMainBody.Controls.Add(_pnlEmails);

Some of the user controls that I add to the main panel are quite complex UI-wise. So when this.pnlMainBody.Controls.Add(_pnlEmails); fires, I see the control appear on the screen, then it resizes itself to fill the body of the panel control.

It's quite ugly actually, so I was wondering whether there is a way to not show the resizing until it's actually done resizing?

I've tried setting the user control's .Visible to false. I've tried doing .SuspendLayout, all to no avail.

Is there a way to do this so the screen transitions are smooth?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见 2024-09-03 05:15:43

首先尝试通过设置在父窗体中打开双缓冲区绘制:

this.DoubleBuffered = true;

在负载处理程序或类似的地方执行此操作,看看闪烁是否消失。

如果这不起作用,您还可以尝试将子控件的 DoubleBuffered 属性设置为 true(如果它们是 .NET Control 派生实体)。下面是我最近使用的一些代码,用于获取未公开双缓冲区属性的控件,以便很好地绘制:(vb 版本。您需要 C# 吗?)

Private Sub ForceDoubleBuffering(ByVal o As Object)
    Dim ctrl As Control
    Dim method As Reflection.MethodInfo
    Dim flags As Reflection.BindingFlags
    ctrl = TryCast(o, Control)
    flags = Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic
    method = ctrl.GetType().GetMethod("SetStyle", flags)
    method.Invoke(ctrl, New Object() {ControlStyles.OptimizedDoubleBuffer, True})
End Sub

First try to turn on double buffer painting in the parent form by setting:

this.DoubleBuffered = true;

Do that in your load handler or some such place to see if the flicker goes away.

If that doesn't work you can also try to set the DoubleBuffered property to true for the child controls if they are .NET Control-derived entities. Here's some code that I used recently to get controls that did not expose the double buffer property to paint nicely: (vb version. Do you need C#?)

Private Sub ForceDoubleBuffering(ByVal o As Object)
    Dim ctrl As Control
    Dim method As Reflection.MethodInfo
    Dim flags As Reflection.BindingFlags
    ctrl = TryCast(o, Control)
    flags = Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic
    method = ctrl.GetType().GetMethod("SetStyle", flags)
    method.Invoke(ctrl, New Object() {ControlStyles.OptimizedDoubleBuffer, True})
End Sub
谁的新欢旧爱 2024-09-03 05:15:43

我想出了解决这个问题的窍门。只要我在将控件添加到主面板后设置 Dock.Fill 属性,就不会出现闪烁。

this.pnlMainBody.Controls.Add(_pnlEmails);
_pnlEmails.Dock = DockStyle.Fill;

I figured out the trick to solving the issue. As I long as I set up the Dock.Fill property after adding the control to the main panel, there is no flicker.

this.pnlMainBody.Controls.Add(_pnlEmails);
_pnlEmails.Dock = DockStyle.Fill;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文