C_Sharp 板


LINE

※ 引述《yeahhey (神秘人)》之銘言: : 大家好 : 小弟最近在用process.StartInfo來啟動外部程式 : 功能上想要達到 : buttom_click後,可以依序把多個外部程式啟動完(利用來處理資料的外部程式) : (例如:先A,A結束後再B,B結束在C...) : 這些執行序的視窗,目前程式都設定縮到最小 : 現在問題是 : ... 一開始其實看不懂你在寫什麼 :p (抱歉),後來才發現原來是指 A 程式結束後, 接下來要啟動 B 程式時,最前景視窗會失去視窗焦點(focus)。 請這樣試試看合不合你的需要: 1. 新建一個 Windows Form 專案,名稱(例如)叫: StartOneByOne 2. 在 Form1 擺一個 Button,Button1 滑鼠點兩下,準備寫 Click 事件。 3. using 以下 namespace: using System.Diagnostics; using System.Runtime.InteropServices; 4. 在 Click 事件前,貼入下列 Windows API 宣告: [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow); enum ShowWindowCommands : int { /// <summary> /// Hides the window and activates another window. /// </summary> Hide = 0, /// <summary> /// Activates and displays a window. If the window is minimized or /// maximized, the system restores it to its original size and position. /// An application should specify this flag when displaying the window /// for the first time. /// </summary> Normal = 1, /// <summary> /// Activates the window and displays it as a minimized window. /// </summary> ShowMinimized = 2, /// <summary> /// Maximizes the specified window. /// </summary> Maximize = 3, // is this the right value? /// <summary> /// Activates the window and displays it as a maximized window. /// </summary> ShowMaximized = 3, /// <summary> /// Displays a window in its most recent size and position. This value /// is similar to <see cref="Win32.ShowWindowCommand.Normal"/>, except /// the window is not activated. /// </summary> ShowNoActivate = 4, /// <summary> /// Activates the window and displays it in its current size and /// position. /// </summary> Show = 5, /// <summary> /// Minimizes the specified window and activates the next top-level /// window in the Z order. /// </summary> Minimize = 6, /// <summary> /// Displays the window as a minimized window. This value is similar to /// <see cref="Win32.ShowWindowCommand.ShowMinimized"/>, except the /// window is not activated. /// </summary> ShowMinNoActive = 7, /// <summary> /// Displays the window in its current size and position. This value is /// similar to <see cref="Win32.ShowWindowCommand.Show"/>, except the /// window is not activated. /// </summary> ShowNA = 8, /// <summary> /// Activates and displays the window. If the window is minimized or /// maximized, the system restores it to its original size and position. /// An application should specify this flag when restoring a minimized /// window. /// </summary> Restore = 9, /// <summary> /// Sets the show state based on the SW_* value specified in the /// STARTUPINFO structure passed to the CreateProcess function by the /// program that started the application. /// </summary> ShowDefault = 10, /// <summary> /// <b>Windows 2000/XP:</b> Minimizes a window, even if the thread /// that owns the window is not responding. This flag should only be /// used when minimizing windows from a different thread. /// </summary> ForceMinimize = 11 } 5. 按鈕的 Click 事件處理函式則類似這樣: private void button1_Click(object sender, EventArgs e) { // testLab.exe 是我另外寫的測試程式,它五秒後會自動結束 string[] Programs = new string[] { @"c:\temp\testLab.exe", @"notepad.exe" }; foreach (string sProgram in Programs) { ProcessStartInfo psi = new ProcessStartInfo(sProgram); // 一開始是極小化 psi.WindowStyle = ProcessWindowStyle.Minimized; Process p = Process.Start(psi); // 等待程式確實 run 起來... while (p.MainWindowHandle == IntPtr.Zero) { System.Threading.Thread.Sleep(100); p.Refresh(); } // 測試後,發現主視窗也要極小化。 ShowWindow(this.Handle, ShowWindowCommands.Minimize); // 隱藏工作視窗,這樣 focus 自然釋出 ShowWindow(p.MainWindowHandle, ShowWindowCommands.Hide); // 重新將工作視窗設為極小化 ShowWindow(p.MainWindowHandle, ShowWindowCommands.Minimize); // p.WaitForExit(); while (!p.HasExited) { System.Threading.Thread.Sleep(100); Application.DoEvents(); } } } --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.170.128.145
1F:推 yeahhey:先感謝你!!我有個問題是 他這樣會有焦點變化嗎? 07/29 00:47
2F:→ yeahhey:start後的瞬間 焦點還是會被啟動的程式奪去 在還回? 07/29 00:48
3F:→ yeahhey:我原本想找看看有沒有函示可以直接設定不要搶走焦點= = 07/29 00:50
4F:→ yeahhey:但最後也是找到你這邊提到的ShowWindowCommands! 07/29 00:52
5F:→ TeemingVoid:一旦呼叫 Process.Start(),focus 就會被新視窗搶走, 07/29 01:00
6F:推 yeahhey:會問有沒有焦點變化 主要是擔心某個使用中全螢幕的視窗 07/29 01:00
7F:→ TeemingVoid:ShowWindow(..., Hide); 的作用在於隱藏新的視窗, 07/29 01:01
8F:→ yeahhey:會因為focus改變而縮小或是瞬間縮小又切回來 07/29 01:01
9F:→ TeemingVoid:這樣 focus 自然釋出。 07/29 01:02
10F:→ yeahhey:因為Process.Start()會搶focus,我現在改用CreateProcess 07/29 01:03
11F:→ yeahhey:外部程式的作法除了process,另外查到shellExecute 07/29 01:04
12F:→ yeahhey:跟CreateProcess~~ shellExecute跟CreateProcess可以直接 07/29 01:04
13F:→ TeemingVoid:我猜你這樣的結果可能一樣,視窗焦點還是會被搶走 :) 07/29 01:05
14F:→ yeahhey:設定ShowWindowCommands(與啟動同一行指令下設定) 07/29 01:05
15F:→ yeahhey:你這樣一講我也覺得 那兩種內部運作應該也跟你的一樣= = 07/29 01:06
16F:→ yeahhey:我開個youtube測測看會不會跳掉好了= = 07/29 01:07
17F:→ TeemingVoid:以前我曾用過 Sendkeys.Send("+{Tab}"); // Alt + Tab 07/29 01:07
18F:→ TeemingVoid:但是效果並不好... 07/29 01:08
19F:推 yeahhey:youtube全螢幕看起來沒影響..kmp全螢幕會突然跳出 07/29 01:10
20F:→ TeemingVoid:隱藏視窗自然釋出焦點,比較自然,你再試試囉!晚安!^^ 07/29 01:10
21F:→ yeahhey:跳出最下排的程式縮圖= =..所以內部運作應該跟寫法你一樣! 07/29 01:11
22F:→ yeahhey:嗯嗯感謝你~~ 07/29 01:11







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:e-shopping站內搜尋

TOP