【问题标题】:Loader Flickering Not Rendering WinForm加载程序闪烁不呈现 WinForm
【发布时间】:2019-04-25 22:53:37
【问题描述】:

我正在尝试运行一个漫长的过程来检索数据并在检索数据时将加载程序屏幕(简单形式)显示为对话框。我遇到的问题是在后台工作程序运行时,加载程序屏幕只是闪烁并且不会在加载程序表单中呈现标签。当后台工作运行时,您可以看到灰色的矩形,如下所示。

有谁知道为什么加载程序表单没有显示他们的标签以及为什么闪烁?

private void LoadInfo()
{
    try
    {
        workingLoader = new WorkingLoader();
        mainWorker = new BackgroundWorker();

        mainWorker.DoWork += MainWorker_DoWork;
        mainWorker.RunWorkerCompleted += MainWorker_RunWorkerCompleted;

        mainWorker.RunWorkerAsync();
        workingLoader.ShowDialog();
    }
    catch (Exception ex)
    {
        if (workingLoader != null)
        {
            workingLoader.Dispose();
        }
    }
}

【问题讨论】:

  • 也许在加载时调用 WorkingLoader 上的 Update() ?还是将 mainWorker 移动到 workingLoader 表单?
  • 标签在哪里以及如何更新?
  • 标签是静态的——基本上是在加载
  • 您是否尝试将DoubleBuffered 设置为true
  • 我没有看到你订阅BackgroundWorker.ReportProgress?您是否尝试从DoWork 更新这些控件?也许,也在使用.Invoke() 的闭环中? mainWorker如何更新workingLoader中的控件,显示为Modal?

标签: c# winforms


【解决方案1】:

internal static class NativeWinAPI
{
internal static readonly int GWL_EXSTYLE = -20;
internal static readonly int WS_EX_COMPOSITED = 0x02000000;

[DllImport("user32")]
internal static extern int GetWindowLong(IntPtr hWnd, int nIndex);

[DllImport("user32")]
internal static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
}

And your form constructor should look as follows:


public MyForm()
{
InitializeComponent();

int style = NativeWinAPI.GetWindowLong(this.Handle, NativeWinAPI.GWL_EXSTYLE);
style |= NativeWinAPI.WS_EX_COMPOSITED;
NativeWinAPI.SetWindowLong(this.Handle, NativeWinAPI.GWL_EXSTYLE, style);
}

In the code above, you might change this.Handle to something like MyFlickeringPanel.Handle

【讨论】:

    猜你喜欢
    • 2014-11-10
    • 2012-05-12
    • 2012-04-14
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多