Use C # to the program to boot screen and only one application instance is allowed

xiaoxiao2021-03-06  55

Recommended class:

1, start the screen class:

Public Class Splashform: System.Windows.Forms.form

{

Private system.windows.Forms.PictureBox PictureBox1;

Private system.windows.Forms.Label label1;

Private system.windows.forms.label lbl_version;

///

/// The required designer variable.

///

Private system.componentmodel.Container Components = NULL;

Public Splashform ()

{

//

// Windows Form Designer Support

//

InitializationComponent ();

LBL_VERSION.TEXT = "Version:" Application.ProductVersion;

//

// Todo: Add any constructor code after INITIALIZECOMPONENT call

//

}

// 省 省 省

2, the application load class:

Using system;

Using system.drawing;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

Using system.Runtime.InteropServices;

Using system.diagnostics;

Using system.reflection;

Using system.io;

Namespace heroic.tempanalyse.tempgui

{

///

/// AppLoader's summary description.

///

Public Class Aploader

{

Private static applicationContext context;

Private static splashform sform = new splashform ();

Private static mainwindow mform = NULL;

// 0 No visible but still run, 1 home, 2 minimize, 3 maximize

Private const Int ws_shownormal = 3;

[Stathread]

Static void

Main

(String [] ARGS)

{

//

8/12/2004

] Used to update the program.

Doupdata ();

//

7/19/2004

] Change the order, the purpose makes it possible to speed up the loading speed

// Get running routines

Process Instance = RunningInstance ();

IF (instance == null)

{

Sform.show ();

MFORM = new mainwindow ();

Context = new applibilityContext ();

Application.idle = New EventHandler (onappidle);

Application.run (context);

}

Else

{

// Processing the discovery routine

HandlerunningInstance (Instance);

//MessageBox.show ("The current program has been run!");

}

}

/ / Update online, no longer scope

Private static void doupdata () {

System.Diagnostics.Process.Start (Application.Startuppath @ "/ update.exe", Application.startuppath @ "/ HEROIC.TEMPANALSE.TEMPGUI.EXE 0"); ////

}

Private Static Void onappidle (Object Sender, Eventargs E)

{

IF (context.mainform == null)

{

Application.idle - = New EventHandler (onappidle);

mform.preload ();

CONTEXT.MAINFORM = MFORM;

CONTEXT.MAINFORM.SHOW ();

Sform.close ();

Sform = NULL;

}

}

// Do not allow two programs to start at the same time

Public Static Process RunningInstance ()

{

Process Current = process.getcurrentProcess ();

Process [] processes = process.getProcesSbyName (current.ProcessName);

// Traverse the routine that is running with the same name

Foreach (Process Process In Processes)

{

/ / Ignore the existing routine

IF (Process.id! = Current.ID)

{

/ / Make sure the routine runs from the EXE file

IF (assembly.getexecutingassembly (). location.replace ("/", "//") ==

Current.mainmodule.FileName)

{

/ / Return another routine instance

Return Process;

}

}

}

/ / There is no other routine, return NULL

Return NULL;

}

Public Static Void HandlerunningInstance (Process Instance)

{

/ / Make sure the window is not minimized or maximized

ShowWindowasync (Instance.mainWindowHandle, WS_Shownormal);

/ / Set the real routine for Foreground Window

SetForegroundWindow (Instance.MainWindowHandle);

}

[DLLIMPORT ("User32.dll")]]]]]

Private static Extern Bool ShowWindowasync (

INTPTR HWND, INT CMDSHOW);

[DLLIMPORT ("User32.dll")] Private Static Extern Bool

SetForegroundwindow (INTPTR HWND);

}

}

3, the class after loading is officially running:

Public void preload ()

{

// If you have already loaded, return

IF (_Loaded)

Return;

// put the code generated by the machine here

INITCUSTOMCONTROL ();

_Loaded = true;

}

/ / Whether it is loaded

Private bool _loaded = false;

Protected Override Void OnLoad (Eventargs E)

{

/ / Make sure the preload () function has been called

IF (! _loaded)

Throw New InvalidOperationException ("Must Call Prelioad Before Calling this function.");

转载请注明原文地址:https://www.9cbs.com/read-91526.html

New Post(0)