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

原文网址: http://free56.cn/post/3.html
None.gif using  System;
None.gif
using  System.Runtime.InteropServices;
None.gif
None.gif
namespace  JKLib
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// 
InBlock.gif    
/// 一个程序只能启动一次实现
ExpandedSubBlockEnd.gif    
/// 

InBlock.gif    public class SingleInstance
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.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.gifContractedSubBlock.gif        
/**//// 
InBlock.gif        
/// 显示窗体命令值 
ExpandedSubBlockEnd.gif        
/// 

InBlock.gif        private const int SW_RESTORE = 9;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// 
InBlock.gif        
/// 窗体名称
ExpandedSubBlockEnd.gif        
/// 

InBlock.gif        private string WinTitle;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// 
InBlock.gif        
/// 一个程序只能启动一次实现
InBlock.gif        
/// 

ExpandedSubBlockEnd.gif        
/// 程序名

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

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

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

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// 
InBlock.gif        
/// 使当前程序进程处于活动状态
ExpandedSubBlockEnd.gif        
/// 

InBlock.gif        public void RaiseOtherProcess()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(hWnd == (System.IntPtr)nullreturn;
InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (IsIconic(hWnd))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.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

原文网址:  http://free56.cn/post/3.html 

转载于:https://www.cnblogs.com/skywind/archive/2006/08/08/470682.html

你可能感兴趣的:(一个程序只能启动一次实现)