、   启动画面类:
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 调用后添加任何构造函数代码
//
}
//以下省略
、 应用程序加载类:
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 = ; [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);
}
}
、 加载完毕正式运行后的类:
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."); }
C#只能运行一个winForm进程
[STAThread]
static void Main()
{
Process instance = RunningInstance();
if (instance == null)
{
System.Windows.Forms.Application.EnableVisualStyles(); //这两行可以让窗体具有XP风格.
System.Windows.Forms.Application.DoEvents();
Application.Run(new ClientForm());
}
else
{
HandleRunningInstance(instance);
}
}
#region 只运行一个实例
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);
//遍历与当前进程名称相同的进程列表
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
return null;
}
private static void HandleRunningInstance(Process instance)
{
MessageBox.Show("该应用系统已经在运行!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
ShowWindowAsync(instance.MainWindowHandle,WS_SHOWNORMAL); //调用api函数,正常显示窗口
SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端。
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(System.IntPtr hWnd);
private const int WS_SHOWNORMAL = ;
#endregion

引文分享:http://yun.baidu.com/share/link?shareid=2304419568&uk=3221713554

用C#给程序加启动画面并只允许一个应用程序实例运行的更多相关文章

  1. C# WinForm程序添加启动画面

    如果程序在装载时需要进行较长时间的处理,最好使用启动画面,一方面美化程序,一方面可以不使用户面对着一片空白的程序界面. 我手头上一个小项目主界面启动时需要检查用户文件及运行环境是否有效,需要一段时间处 ...

  2. VC++编程中为程序加入启动画面功能

     如何为自己的程序加入启动画面 观察我们平常使用的软件,当我们双击软件的时候,会在主界面出现前,先行出现一个启动画面,由于前一阵子写了一个基于对话框的程序,亲自实验了下,今天就为大家简单的介绍下,在我 ...

  3. IOS编程教程(八):在你的应用程序添加启动画面

    IOS编程教程(八):在你的应用程序添加启动画面   虽然你可能认为你需要编写闪屏的代码,苹果已经可以非常轻松地把它做在Xcode中.不需要任何编码.你只需要做的是设置一些配置. 什么是闪屏 对于那些 ...

  4. apple-touch-startup-image 制作iphone web应用程序的启动画面

    为ipad制作web应用程序的启动画面时发现个问题,只能显示竖屏图,横屏图出不来,如下: 首先页面头部里要加入(这个是APP启动画面图片,如果不设置,启动画面就是白屏,图片像素就是手机全屏的像素) & ...

  5. 【VC编程技巧】窗口☞3.5对单文档或者多文档程序制作启动画面

    (一)概要: 文章描写叙述了如何通过Visual C++ 2012或者Visual C++ .NET,为单文档或者多文档程序制作启动画面.在Microsoft Visual Studio 6.0中对于 ...

  6. WPF应用程序的启动画面[Splash Screen本质分析]

    原文:WPF应用程序的启动画面[Splash Screen本质分析] 不经意间发现了wpf的这个小玩意,感觉蛮有意思的.我在项目中添加了一张图片 如图: wpf-1.JPG(10.73 K) 2010 ...

  7. Qt程序开机启动的怪现象————无法正常显示程序皮肤

    事情很简单:最近公司项目在做即时通讯软件,类似QQ.该软件应该支持开机启动这样的常用功能.但是实际上开发该功能的时候碰到了个问题:开机启动程序无法正常加载皮肤文件. 这个问题让我头疼了很久啊.最终确定 ...

  8. C运行时库(C Run-time Library)详解(提供的另一个最重要的功能是为应用程序添加启动函数。Visual C++对控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)

    一.什么是C运行时库 1)C运行时库就是 C run-time library,是 C 而非 C++ 语言世界的概念:取这个名字就是因为你的 C 程序运行时需要这些库中的函数. 2)C 语言是所谓的“ ...

  9. winform只允许一个应用程序运行 2014-12-08 09:51 31人阅读 评论(0) 收藏

    使用互斥体Mutex类型 导入命名空间 using System.Threading; //声明互斥体 Mutex mutex = new Mutex(false, "ThisShouldO ...

随机推荐

  1. Linux学习8-Linux常用命令(4)

    链接命令     命令名称:ln 命令英文原意:link 命令所在路径:/bin/ln 执行权限:所有用户 功能描述:生成链接文件 语法:ln 选项[-s][原文件] [目标文件] 选项: -s 创建 ...

  2. wamp 安装monggo扩展

    1.下载对应的monggo扩展 http://pecl.php.net/package/mongo 2. 找对应的版本 放在D:\program\wamp\bin\php\php5.5.12\ext ...

  3. COGS2608 [河南省队2016]无根树

    传送门 这题大概就是传说中的动态树形DP了吧,学习了一波…… 首先,对于没有修改的情况,不难想到树形DP,定义$f_i$表示强制必须选$i$且只能再选$i$的子树中的点的最优解,易得转移方程$f_i= ...

  4. 网络I/O模型--02阻塞模式(多线程)

    当服务器收到客户端 X 的请求后(读取到所有请求数据后),将这个请求送入一个独立线程进行处理,然后主线程继续接收客户端 Y 的请求. 客户端一侧也可以使用一个子线程和服务器端进行通信.这样客户端主线程 ...

  5. Java 性能调优指南之 Java 集合概览

    [编者按]本文作者为拥有十年金融软件开发经验的 Mikhail Vorontsov,文章主要概览了所有标准 Java 集合类型.文章系国内 ITOM 管理平台 OneAPM 编译呈现,以下为正文: 本 ...

  6. 【日常记录】【unity3d】 2D跳跃过快导致角色某帧陷入地面

    如果角色运动过快会导致嵌入地面再反弹出来 : 可以使用更高质量的检测方式 "Continuous" :就可以解决这个问题

  7. Prometheus Node_exporter metrics 之 Basic CPU / Mem / Disk Info

    Basic CPU / Mem / Disk Info 1. CPU Cores 物理 CPU 的核数 cat /proc/cpuinfo| grep "cpu cores"| u ...

  8. 数据库对比:选择MariaDB还是MySQL?

    作者 | EverSQL 译者 | 无明 这篇文章的目的主要是比较 MySQL 和 MariaDB 之间的主要相似点和不同点.我们将从性能.安全性和主要功能方面对这两个数据库展开对比,并列出在选择数据 ...

  9. [翻译] SSKeychain

    SSKeychain https://github.com/soffes/sskeychain SSKeychain is a simple wrapper for accessing account ...

  10. 导出Excel 2007 (NPOI)

    今天在导出Excel2007时报了个错,问是否修复,点yes就提示修复正常了,但具体什么原因没说,如图 之前简单的导出代码是这样写的 public static void ExportToWeb(st ...