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

          導航

          常用鏈接

          留言簿(44)

          新聞檔案

          2.動態語言

          3.工具箱

          9.文檔教程

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 双桥区| 仪征市| 平罗县| 崇左市| 泌阳县| 凉山| 荆州市| 扎鲁特旗| 青河县| 新平| 新龙县| 丰镇市| 启东市| 遂溪县| 湘潭市| 莆田市| 徐汇区| 历史| 安泽县| 新营市| 汉沽区| 聂拉木县| 南华县| 浦县| 兖州市| 固始县| 乾安县| 新沂市| 浪卡子县| 诸城市| 酒泉市| 鹤壁市| 综艺| 大邑县| 买车| 河曲县| 兴山县| 蒙山县| 林周县| 白水县| 皮山县|