blogjava's web log

blogjava's web log
...

winform一些技巧[导入]

// 得到路径
             string  strPath = System.IO.Directory.GetCurrentDirectory();
// 分割
string  str1 = " text1,text2,text3,text4 "
string [] stri = str1.Split( new   char [] { ' ' } );

// 启动一个程序

using  System;
using  System.Drawing;
using  System.Collections;
using  System.ComponentModel;
using  System.Windows.Forms;
using  System.Data;
using  System.Diagnostics;
using  System.Reflection;
using  System.Runtime.InteropServices;

        
///   <summary>
        
///  应用程序的主入口点。
        
///   </summary>

        [STAThread]
        
static   void  Main() 
        
{
            
// Get the running instance.
            Process instance  =  RunningInstance();
            
if  (instance  ==   null )
            
{
                
// 新实例
                Application.Run( new  Form1());
            }

            
else
            
{
                
// 相同的实例,并将窗口置前
                HandleRunningInstance(instance);
        }

    }


public   static  Process RunningInstance()
{
    Process current 
=  Process.GetCurrentProcess();
    Process[] processes 
=  Process.GetProcessesByName (current.ProcessName);
    
// 查找相同名的进程
     foreach  (Process process  in  processes)
    
{
    
// 忽略当前线程
     if  (process.Id  !=  current.Id)
    
{
    
// 确认相同名的进程运行位置是否相同
     if  (Assembly.GetExecutingAssembly().Location.Replace( " / " " \\ " == current.MainModule.FileName)
{
    
// 返回相同名进程
     return  process;
}

}

}

    
// 没有其它进程,返回空值
     return   null ;
}


 
public   static   void  HandleRunningInstance(Process instance)
 
{
 
// 确认窗口是最小化或最大化
 ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);

 
// 将窗口置前
 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);
 
private   const   int  WS_SHOWNORMAL  =   1 ;

        
private   void  Form1_Load( object  sender, System.EventArgs e)
        
{
        
        }

 }

posted on 2006-05-28 14:56 record java and net 阅读(393) 评论(0)  编辑  收藏 所属分类: dot net相关


只有注册用户登录后才能发表评论。


网站导航:
 

导航

常用链接

留言簿(44)

新闻档案

2.动态语言

3.工具箱

9.文档教程

友情链接

搜索

最新评论