一个程序只能启动一次实现

None.gif using  System;
None.gif
using  System.Runtime.InteropServices;
None.gif
None.gif
namespace  JKLib
ExpandedBlockStart.gif
{
InBlock.gif    
ExpandedSubBlockStart.gif    
/// <summary>
InBlock.gif    
/// 一个程序只能启动一次实现
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SingleInstance
ExpandedSubBlockStart.gif    
{
InBlock.gif        [DllImport(
"user32.dll")] 
InBlock.gif        
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
InBlock.gif        [DllImport(
"user32.dll")] 
InBlock.gif        
private static extern bool SetForegroundWindow(IntPtr hWnd);
InBlock.gif        [DllImport(
"user32.dll")] 
InBlock.gif        
private static extern bool ShowWindowAsync(IntPtr hWnd,int nCmdShow);
InBlock.gif        [DllImport(
"user32.dll")] 
InBlock.gif        
private static extern bool IsIconic(IntPtr hWnd);
InBlock.gif
ExpandedSubBlockStart.gif        
/// <summary>
InBlock.gif        
/// 显示窗体命令值 
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private const int SW_RESTORE = 9;
InBlock.gif
ExpandedSubBlockStart.gif        
/// <summary>
InBlock.gif        
/// 窗体名称
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private string WinTitle;
InBlock.gif
ExpandedSubBlockStart.gif        
/// <summary>
InBlock.gif        
/// 一个程序只能启动一次实现
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="_WinTitle">程序名</param>

InBlock.gif        public SingleInstance(string _WinTitle)
ExpandedSubBlockStart.gif        
{
InBlock.gif            WinTitle 
= _WinTitle;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private IntPtr hWnd = (System.IntPtr)null;
InBlock.gif
ExpandedSubBlockStart.gif        
/// <summary>
InBlock.gif        
/// 是否只有一个窗口
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public bool IsSingleInstance
ExpandedSubBlockStart.gif        
{
InBlock.gif            
get
ExpandedSubBlockStart.gif            
{
InBlock.gif                hWnd 
= FindWindow(null,WinTitle);
InBlock.gif                
return hWnd == (System.IntPtr)null;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gif        
/// <summary>
InBlock.gif        
/// 使当前程序进程处于活动状态
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void RaiseOtherProcess()
ExpandedSubBlockStart.gif        
{
InBlock.gif            
if(hWnd == (System.IntPtr)nullreturn;
InBlock.gif            
else
ExpandedSubBlockStart.gif            
{
InBlock.gif                
if (IsIconic(hWnd))
ExpandedSubBlockStart.gif                
{
InBlock.gif                    ShowWindowAsync(hWnd,SW_RESTORE);
ExpandedSubBlockEnd.gif                }

InBlock.gif                SetForegroundWindow(hWnd);
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

本文转自Jack Niu博客园博客,原文链接:http://www.cnblogs.com/skywind/archive/2006/08/08/470682.html,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值