UFO ET IT

Windows Forms 응용 프로그램에서 스플래시 화면을 구축하는 방법은 무엇입니까?

ufoet 2020. 12. 12. 11:54
반응형

Windows Forms 응용 프로그램에서 스플래시 화면을 구축하는 방법은 무엇입니까?


몇 초 동안 애플리케이션 시작시 스플래시 화면을 표시해야합니다. 아무도 이것을 구현하는 방법을 알고 있습니까?

도움을 주시면 감사하겠습니다.


먼저, 스플래시 화면을 테두리가없고 움직일 수없는 형태로 만들고, 처음에는 화면 중앙에 원하는 색상으로 표시되도록 설정합니다. 이 모든 것은 디자이너 내에서 설정할 수 있습니다. 구체적으로 다음을 원합니다.

  • 폼의 ControlBox, MaximizeBox, MinimizeBox 및 ShowIcon 속성을 "False"로 설정합니다.
  • StartPosition 속성을 "CenterScreen"으로 설정합니다.
  • FormBorderStyle 속성을 "None"으로 설정합니다.
  • 양식의 MinimumSize 및 MaximumSize를 초기 크기와 동일하게 설정하십시오.

그런 다음 표시 할 위치와 무시할 위치를 결정해야합니다. 이 두 작업은 프로그램의 기본 시작 논리의 반대편에서 발생해야합니다. 이는 애플리케이션의 main () 루틴에 있거나 기본 애플리케이션 양식의로드 핸들러에있을 수 있습니다. 고가의 큰 개체를 만들고, 하드 드라이브에서 설정을 읽고, 일반적으로 기본 응용 프로그램 화면이 표시되기 전에 장면 뒤에서 작업을 수행하는 데 오랜 시간이 걸립니다.

그런 다음 폼의 인스턴스를 만들고 Show ()하고 시작 초기화를 수행하는 동안 참조를 유지하기 만하면됩니다. 메인 폼이로드되면 Close ()합니다.

스플래시 화면에 애니메이션 이미지가있는 경우 창도 "이중 버퍼링"되어야하며 모든 초기화 논리가 GUI 스레드 외부에서 발생하는지 확인해야합니다 (즉, 메인을 가질 수 없음을 의미). 메인폼의로드 핸들러에서 로직을로드합니다 .BackgroundWorker 또는 다른 스레드 루틴을 만들어야합니다.


다음은 몇 가지 지침 단계입니다.

  1. 테두리없는 양식 만들기 (스플래시 화면이 됨)
  2. 응용 프로그램 시작시 타이머 시작 (몇 초 간격)
  3. 스플래시 양식보기
  4. Timer.Tick 이벤트에서 타이머를 중지하고 스플래시 양식을 닫은 다음 기본 신청서를 표시합니다.

이것을 시도하고 막히면 다시 와서 문제와 관련된 더 구체적인 질문을하십시오.


시작 화면을 만드는 간단하고 쉬운 솔루션

  1. 새 양식 열기 이름 "SPLASH"
  2. 원하는대로 배경 이미지 변경
  3. 진행률 표시 줄 선택
  4. 타이머 선택

이제 타이머에서 타이머 틱을 설정하십시오.

private void timer1_Tick(object sender, EventArgs e)
{
    progressBar1.Increment(1);
    if (progressBar1.Value == 100) timer1.Stop();        
}

새 양식 사용 이름 "FORM-1"을 추가하고 FORM 1에서 다음 명령을 사용하십시오.

참고 : 스플래시 양식은 양식을 열기 전에 작동합니다 1

  1. 이 라이브러리 추가

    using System.Threading;
    
  2. 기능 생성

    public void splash()
    {     
        Application.Run(new splash());
    }
    
  3. 아래와 같이 초기화에서 다음 명령을 사용하십시오.

    public partial class login : Form
    {     
        public login()
        {
            Thread t = new Thread(new ThreadStart(splash));
            t.Start();
            Thread.Sleep(15625);
    
            InitializeComponent();
    
            enter code here
    
            t.Abort();
        }
    }
    

http://solutions.musanitech.com/c-create-splash-screen/


꽤 좋은 초기 화면 CodeProject의에 이상은.

함께 제공됩니다

  • 점점 뚜렷해지다
  • 진행 표시 줄
  • 상태 라벨
  • 사라지다
  • 닫으려면 두 번 클릭

저자는 최근에 코드를 검토하고 업데이트했습니다. 그것은 정말 대단한 작업이며 좋은 아이디어를 가진 많은 다른 개발자들 사이의 협력입니다.


스플래시 만들기

private void timer1_Tick(object sender, EventArgs e)
{
    counter++;
    progressBar1.Value = counter *5;
    // label2.Text = (5*counter).ToString();
    if (counter ==20)
    {
        timer1.Stop();
        this.Close();
    }
}
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
this.ClientSize = new System.Drawing.Size(397, 283);
this.ControlBox = false;
this.Controls.Add(this.label2);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.label1);
this.ForeColor = System.Drawing.SystemColors.ControlLightLight;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Splash";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.ResumeLayout(false);
this.PerformLayout();

그런 다음 응용 프로그램에서

sp = new Splash();
sp.ShowDialog();

여기에있는 다른 답변은이를 잘 다루지 만 Visual Studio의 스플래시 화면에 대한 기본 제공 기능이 있다는 것을 알아 두는 것이 좋습니다. Windows Form 앱의 프로젝트 속성을 열고 응용 프로그램 탭을 보면 "Splash screen : "옵션이 있습니다. 앱에서 스플래시 화면으로 표시 할 양식을 선택하기 만하면 앱이 시작될 때 표시되고 기본 양식이 표시되면 숨겨집니다.

여전히 위에서 설명한대로 양식을 설정해야합니다 (올바른 테두리, 위치, 크기 조정 등).


먼저 테두리가 있거나없는 양식을 만들어야합니다 (이런 경우 테두리없는 것이 선호 됨).

public class SplashForm : Form
{
    Form _Parent;
    BackgroundWorker worker;
    public SplashForm(Form parent)
    {
         InitializeComponent();
         BackgroundWorker worker = new BackgroundWorker();
         this.worker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.worker _DoWork);
         backgroundWorker1.RunWorkerAsync();
         _Parent = parent;
    }
    private void worker _DoWork(object sender, DoWorkEventArgs e)
    {
         Thread.sleep(500);
         this.hide();
         _Parent.show();
    }     
}

Main에서 당신은 그것을 사용해야합니다

   static class Program
        {
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new SplashForm());
            }
        }

메인 프로그램 양식이 표시 될 준비가 될 때까지 표시되는 스플래시 화면을 원했기 때문에 타이머 등은 나에게 쓸모가 없었습니다. 또한 가능한 한 단순하게 유지하고 싶었습니다. 내 응용 프로그램은 (약자)로 시작합니다.

static void Main()
{
    Splash frmSplash = new Splash();
    frmSplash.Show();
    Application.Run(new ReportExplorer(frmSplash));
}

그런 다음 ReportExplorer에는 다음이 있습니다.

public ReportExplorer(Splash frmSplash)
{
    this.frmSplash = frmSplash;
    InitializeComponent();
}

마지막으로 모든 초기화가 완료된 후 :

if (frmSplash != null) 
{
     frmSplash.Close();
     frmSplash = null;
}

아마도 내가 뭔가를 놓치고 있을지도 모르지만 이것은 스레드와 타이머를 비웃는 것보다 훨씬 쉽습니다.


대답하기 조금 늦었을 수도 있지만 내 방식을 공유하고 싶습니다. winform 응용 프로그램의 기본 프로그램에서 스레드를 사용하는 쉬운 방법을 찾았습니다.

애니메이션이있는 "splashscreen"양식과 모든 애플리케이션 코드가있는 "main"이 있다고 가정 해 보겠습니다.

 [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Thread mythread;
        mythread = new Thread(new ThreadStart(ThreadLoop));
        mythread.Start();
        Application.Run(new MainForm(mythread));           
    }

    public static void ThreadLoop()
    {
        Application.Run(new SplashScreenForm());           
    }

생성자의 기본 양식에서 :

 public MainForm(Thread splashscreenthread)
    {
        InitializeComponent();

        //add your constructor code

        splashscreenthread.Abort();            
    }

이렇게하면 스플래시 화면이 기본 양식이로드되는 시간 동안 만 지속됩니다.

splashcreen 양식에는 정보를 애니메이션 / 표시하는 고유 한 방법이 있어야합니다. 내 프로젝트에서 내 스플래시 화면은 새 스레드를 시작하고 x 밀리 초마다 기본 그림을 약간 다른 기어로 변경하여 회전의 ​​환상을 제공합니다.

내 스플래시 화면의 예 :

int status = 0;
private bool IsRunning = false;
    public Form1()
    {
        InitializeComponent();
        StartAnimation();
    }

    public void StartAnimation()
    {
        backgroundWorker1.WorkerReportsProgress = false;
        backgroundWorker1.WorkerSupportsCancellation = true;
        IsRunning = true;
        backgroundWorker1.RunWorkerAsync();
    }


    public void StopAnimation()
    {
        backgroundWorker1.CancelAsync();
    }

    delegate void UpdatingThreadAnimation();
    public void UpdateAnimationFromThread()
    {

        try
        {
            if (label1.InvokeRequired == false)
            {
                UpdateAnimation();
            }
            else
            {
                UpdatingThreadAnimation d = new UpdatingThreadAnimation(UpdateAnimationFromThread);
                this.Invoke(d, new object[] { });
            }
        }
        catch(Exception e)
        {

        }
    }

 private void UpdateAnimation()
    {
    if(status ==0) 
    {
    // mypicture.image = image1
     }else if(status ==1)
     {
    // mypicture.image = image2
     }
    //doing as much as needed

      status++;
        if(status>1) //change here if you have more image, the idea is to set a cycle of images
        {
            status = 0;
        }
        this.Refresh();
    }

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;
        while (IsRunning == true)
        {
            System.Threading.Thread.Sleep(100);
            UpdateAnimationFromThread();
        }
    }

이것이 어떤 사람들에게 도움이되기를 바랍니다. 제가 실수를했다면 죄송합니다. 영어는 제 모국어가 아닙니다.


이 코드 시도

public partial class ssplashscreen : Form
    {
        public ssplashscreen()
        {                
            InitializeComponent();    
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            progressBar1.Increment(1);
            if (progressBar1.Value == 100)
            {
                timer1.Stop();
                this.Hide();
                Form frm = new login();
                frm.Show();
            }
        }
    }

이 시도:

namespace SplashScreen
{
    public partial class frmSplashScreen : Form
    {
        public frmSplashScreen()
        {
            InitializeComponent();
        }

        public int LeftTime { get; set; }

        private void frmSplashScreen_Load(object sender, EventArgs e)
        {
            LeftTime = 20;
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (LeftTime > 0)
            {
                LeftTime--;
            }
            else
            {
                timer1.Stop();
                new frmHomeScreen().Show();
                this.Hide();
            }
        }
    }
}

참고 URL : https://stackoverflow.com/questions/7955663/how-to-build-splash-screen-in-windows-forms-application

반응형