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 閱讀(407) 評論(0)  編輯  收藏 所屬分類: dot net相關

          導航

          常用鏈接

          留言簿(44)

          新聞檔案

          2.動態(tài)語言

          3.工具箱

          9.文檔教程

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 曲水县| 本溪市| 扎囊县| 长宁县| 若尔盖县| 方城县| 车险| 济阳县| 崇明县| 临邑县| 资中县| 来宾市| 长武县| 四川省| 筠连县| 金川县| 临澧县| 兰考县| 辽阳市| 长治市| 龙陵县| 涟源市| 泌阳县| 绥化市| 华宁县| 苍梧县| 土默特左旗| 理塘县| 莒南县| 阳原县| 江门市| 英吉沙县| 梁山县| 松溪县| 南江县| 佛教| 汝州市| 沅江市| 桂东县| 水富县| 边坝县|