涉及類:
1、 啟動畫面類:
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;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public SplashForm()
{
//
// Windows 窗體設計器支持所必需的
//
InitializeComponent();
lbl_version.Text = "版本:" + Application.ProductVersion;
//
// TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
//
}
//以下省略
2、 應用程序加載類:
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
{
/// <summary>
/// AppLoader 的摘要說明。
/// </summary>
public class AppLoader
{
private static ApplicationContext context;
private static SplashForm sForm = new SplashForm();
private static MainWindow mForm = null;
//0不可見但仍然運行,1居中,2最小化,3最大化
private const int WS_SHOWNORMAL = 3;
[STAThread]
static void Main(string[] args)
{
// [8/12/2004]用於更新該程序。
doUpData();
// [7/19/2004] 改變順序,目的使得開始加載速度加快
//得到正在運行的例程
Process instance = RunningInstance();
if(instance == null)
{
sForm.Show();
mForm = new MainWindow();
context = new ApplicationContext();
Application.Idle += new EventHandler(OnAppIdle);
Application.Run(context);
}
else
{
//處理發現的例程
HandleRunningInstance(instance);
//MessageBox.Show("當前程序已經運行了!");
}
}
//在線更新用,不再本文范圍
private static void doUpData()
{
System.Diagnostics.Process.Start(Application.StartupPath+@"\update.exe",Application.StartupPath+@"\Heroic.TempAnalyse.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;
}
}
//不允許有兩個程序同時啟動
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);
//遍歷正在有相同名字運行的例程
foreach (Process process in processes)
{
//忽略現有的例程
if (process.Id != current.Id)
{
//確保例程從EXE文件運行
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
current.MainModule.FileName)
{
//返回另一個例程實例
return process;
}
}
}
//沒有其它的例程,返回Null
return null;
}
public static void HandleRunningInstance(Process instance)
{
//確保窗口沒有被最小化或最大化
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
//設置真實例程為foreground window
SetForegroundWindow (instance.MainWindowHandle);
}
[DllImport("User32.dll")]
extern bool ShowWindowAsync(
IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")] private static extern bool
SetForegroundWindow(IntPtr hWnd);
}
}
3、 加載完畢正式運行後的類:
public void PreLoad()
{
// 如果已經加載畢,則返回
if (_Loaded)
return;
// 把機器生成的代碼放到這裡
initCustomControl();
_Loaded = true;
}
// 是否加載完畢
private bool _Loaded = false;
protected override void OnLoad(EventArgs e)
{
// 確保 PreLoad()函數已經調用
if (!_Loaded)
throw new InvalidOperationException("Must call PreLoad before calling this function.");
}