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

          導航

          常用鏈接

          留言簿(44)

          新聞檔案

          2.動態語言

          3.工具箱

          9.文檔教程

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 集安市| 屯留县| 舞钢市| 车致| 芜湖县| 伊川县| 敦化市| 黄石市| 金寨县| 兴安盟| 富顺县| 阜阳市| 海宁市| 泾源县| 全州县| 赤峰市| 德保县| 金秀| 长宁县| 寿光市| 武清区| 安义县| 墨脱县| 城步| 牡丹江市| 通许县| 勐海县| 鄯善县| 元朗区| 西和县| 友谊县| 庄河市| 绥宁县| 孟州市| 隆子县| 巫溪县| 治多县| 郑州市| 上饶市| 北辰区| 德庆县|