涉及類:
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")]
private static 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.");
}
先貼過來,回家研究