启动画面在6秒后变成黑色

本文关键字:黑色 6秒 动画 启动 | 更新日期: 2023-09-27 18:05:55

我有一个闪屏,我正在使用它,当我从智能卡加载数据,因为它需要大约35秒来获得数据,我的加载屏幕有一个白色的背景,我设置透明键白色,使屏幕透明。它可以正常工作,但大约6秒后,背面颜色变成黑色下面是加载屏幕的代码:

partial class LoadingForm : Form
{
    int tickcount = 0;
    public bool CloseIt = false;
    public string Message = "من فضلك إنتظر قليلا ...";
    public Point LocationPoint;
    public LoadingForm()
    {
        SetStyle(ControlStyles.UserPaint, true);
        SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor | ControlStyles.DoubleBuffer, true);
        InitializeComponent();
        SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor | ControlStyles.DoubleBuffer, true);
        LocationPoint = new Point();
        LocationPoint.X = -300;
        LocationPoint.Y = -300;
        lblMessage.Text = Message;
    }
    private void LoadingForm_Load(object sender, EventArgs e)
    {
        Left = LocationPoint.X;
        Top = LocationPoint.Y;
        timer1.Start();
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        if (Created)
        {
            if (tickcount++ == 1)
            {
                LocationPoint.X = Screen.PrimaryScreen.Bounds.Width / 2 - 240;
                LocationPoint.Y = Screen.PrimaryScreen.Bounds.Height / 2 - 140;
                lblMessage.Text = Message;
                Left = LocationPoint.X;
                Top = LocationPoint.Y;
                Width = 480;
                Height = 185;
            }
            if (CloseIt)
            {
                pictureBox1.Image = null;
                Close();
                Application.ExitThread();
            }
        }
    }
    private void LoadingForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        timer1.Stop();
        timer1.Dispose();
    }
}

这个类创建了一个线程来运行表单:

public class LoadingProgress
{
    LoadingForm frm = new LoadingForm();
    string Message = "من فضلك إنتظر قليلا ...";
    Thread th;
    public void StartProgress()
    {
        th = new Thread(new ThreadStart(ShowForm));
        if (frm == null)
            frm = new LoadingForm();
        frm.Message = Message;
        th.Start();
    }
    public void Set_Message(string msg)
    {
        Message = msg;
        frm.Message = Message;
    }
    void ShowForm()
    {
        frm.ShowDialog();
        frm.Dispose();
        frm = null;
        if (th.ThreadState == ThreadState.Running)
            th.Abort();
    }
    public void Stop()
    {
        frm.CloseIt = true;
    }
    public void Set_Position(System.Drawing.Point p)
    {
        frm.LocationPoint = p;
    }
} 

启动画面在6秒后变成黑色

这是一个猜测,但我认为您最好在主应用程序线程(消息泵所在的地方)上创建所有表单,并在单独的线程上旋转实际的工作

我的猜测是,因为你的窗体是不处理windows事件(因为它是在错误的线程),windows本质上是标记为"不响应",并停止任何进一步渲染它

System.Windows.Forms.Timer类上的Tick事件发生在UI线程上。这意味着,如果tick方法中有长时间运行的代码,那么它将挂起应用程序重绘接口的能力。当这种情况发生时,Windows可能会将其绘制为黑色。