我使用Process.Start()
從我的程序中打開了一個記事本,但新打開的記事本覆蓋了屏幕。但我確實希望我的應用程序能夠保持其重點。如何在打開過程(記事本)後將焦點設置回窗體?
我類似(使用相同的Process.Start)打開MS Excel和Word,但讓焦點回到我的形式,我需要寫的是:
this.Focus();
但是用記事本怪癖:我打開記事本(以及所有其他類似的過程)
Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.EnableRaisingEvents = true;
process.StartInfo.FileName = @"abc.log";
process.Start();
現在記事本佔據了焦點。
我嘗試這些:
this.Activate()
,this.Focus()
,不用提[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)] public static extern IntPtr SetFocus(HandleRef hWnd); { IntPtr hWnd = myProcess.Handle; SetFocus(new HandleRef(null, hWnd)); }
[DllImport("User32")] private static extern int SetForegroundWindow(IntPtr hwnd); [DllImportAttribute("User32.DLL")] private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); private const int SW_SHOW = 5; private const int SW_MINIMIZE = 6; private const int SW_RESTORE = 9; { ShowWindow(Process.GetCurrentProcess().MainWindowHandle, SW_RESTORE); SetForegroundWindow(Process.GetCurrentProcess().MainWindowHandle); }
另一個更長的解決方案,從here了。
所有這些仍然把重點放在記事本上。爲什麼僅僅把焦點放在一個窗口上太難了,應用程序自己的窗口也是如此?
編輯:至多我可以打開記事本最小化,但它仍然不會在嘗試所有上述代碼後將焦點放在窗體上。記事本打開時最小化,但重點仍然放在記事本上(有時我們在Windows XP中看到的東西),而形式將會失去重點。
它是確定啓動窗口處於最小化狀態? – NoviceProgrammer 2012-01-16 14:14:16
是的,沒關係,但它仍然不起作用。我會在我的問題中更新 – nawfal 2012-01-16 14:14:57
你試過這個嗎? http://stackoverflow.com/questions/2121911/starting-a-process-without-stealing-focus-c – NoviceProgrammer 2012-01-16 14:17:37