Add a splash screen to a program in C # and allow only one application instance to run

Source: Internet
Author: User

1, Start screen class: Public classSplashForm:System.Windows.Forms.Form {PrivateSystem.Windows.Forms.PictureBox PictureBox1; PrivateSystem.Windows.Forms.Label Label1; PrivateSystem.Windows.Forms.Label lbl_version; /// <summary>        ///the required designer variables. /// </summary>        PrivateSystem.ComponentModel.Container components =NULL;  PublicSplashform () {//            //The Windows Forms Designer supports the required//InitializeComponent (); Lbl_version. Text="version:"+application.productversion; //            //TODO: Add any constructor code after the InitializeComponent call//        }        //the following ellipsis2, Application Load class:usingSystem;usingSystem.Drawing;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Windows.Forms;usingSystem.Data;usingSystem.Runtime.InteropServices;usingSystem.Diagnostics;usingSystem.Reflection;usingSystem.IO;namespaceHeroic.TempAnalyse.TempGui {/// <summary>     ///a summary description of the Apploader. /// </summary>      Public classApploader {Private StaticApplicationContext context; Private StaticSplashform sform =NewSplashform (); Private StaticMainWindow Mform =NULL; //0 not visible but still running, 1 centered, 2 minimized, 3 maximized        Private Const intWs_shownormal =3; [STAThread]Static voidMain (string[] args) {                  //[8/12/2004] used to update the program. Doupdata (); //[7/19/2004] Change the order to start loading faster//get a running routineProcess instance =runninginstance (); if(Instance = =NULL) {sform.show (); Mform=NewMainWindow (); Context=NewApplicationContext (); Application.idle+=NewEventHandler (Onappidle);             Application.Run (context); }             Else             {                 //routines for processing discoveriesHandlerunninginstance (instance); //MessageBox.Show ("The current program is already running!");            }         }         //online update with, no longer this article scope        Private Static voidDoupdata () {System.Diagnostics.Process.Start (Application.startuppath+@"\update.exe", application.startuppath+@"\heroic.tempanalyse.tempgui.exe 0");//                }        Private Static voidOnappidle (Objectsender, EventArgs e) {             if(Context. MainForm = =NULL) {Application.idle-=NewEventHandler (Onappidle);                 Mform.preload (); Context. MainForm=Mform; Context.                         Mainform.show ();                         Sform.close (); Sform=NULL; }           }         //Two programs are not allowed to start at the same time         Public Staticprocess Runninginstance () {Process current=process.getcurrentprocess (); Process[] Processes=Process.getprocessesbyname (current.             ProcessName); //traverse a routine that is running with the same name            foreach(Process processinchprocesses) {                 //Ignore existing routines                if(Process. Id! =Current . Id) {//Make sure routines run from EXE file                    if(Assembly.getexecutingassembly (). Location.replace ("/","\\") ==Current . Mainmodule.filename) {//returns another instance of a routine                        returnprocess; }                 }             }             //no other routines, return null            return NULL; }          Public Static voidHandlerunninginstance (Process instance) {//ensure that the window is not minimized or maximizedShowwindowasync (instance.             Mainwindowhandle, Ws_shownormal); //set the real routine to foreground windowSetForegroundWindow (instance.         Mainwindowhandle); } [DllImport ("User32.dll")]         Private Static extern BOOLShowwindowasync (IntPtr hWnd,intcmdshow); [DllImport ("User32.dll")]Private Static extern BOOLSetForegroundWindow (IntPtr hWnd); } }3, load the class after the formal run: Public voidpreload () {//if it is already loaded, it returns            if(_loaded)return; //Put the machine-generated code here.Initcustomcontrol (); _loaded=true; }        //is loading complete        Private BOOL_loaded =false; protected Override voidOnLoad (EventArgs e) {//make sure that the preload () function has been called            if(!_loaded)Throw NewInvalidOperationException ("must call preload before calling this function."); }

C # can only run one WinForm process [STAThread]Static voidMain () {Process instance=runninginstance (); if(Instance = =NULL) {System.Windows.Forms.Application.EnableVisualStyles (); //These two lines allow the form to have XP-style.System.Windows.Forms.Application.DoEvents (); Application.Run (Newclientform ()); }            Else{handlerunninginstance (instance); }   }#regionRun only one instance Public Staticprocess Runninginstance () {Process current=process.getcurrentprocess (); Process[] Processes=Process.getprocessesbyname (current. ProcessName); //traverse a list of processes with the same name as the current processforeach(Process processinchprocesses) { //Ignore The current processif(Process. Id! =Current . Id) {//Make sure, the process is running from the exe file.if(Assembly.getexecutingassembly (). Location.replace ("/","\\") ==Current . Mainmodule.filename) {//Return The other process instance.returnprocess;} } } return NULL; } Private Static voidHandlerunninginstance (Process instance) {MessageBox.Show ("the application system is already running! ","Prompt Information", messageboxbuttons.ok,messageboxicon.information); Showwindowasync (instance. Mainwindowhandle,ws_shownormal); //Call API function, normal display windowSetForegroundWindow (instance. Mainwindowhandle);//places the window at the front. } [DllImport ("User32.dll")] Private Static extern BOOLShowwindowasync (System.IntPtr hWnd,intcmdshow); [DllImport ("User32.dll")] Private Static extern BOOLSetForegroundWindow (System.IntPtr hWnd);Private Const intWs_shownormal =1; #endregion 

Citation sharing: http://yun.baidu.com/share/link?shareid=2304419568&uk=3221713554

Add a splash screen to a program in C # and allow only one application instance to run

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.