2013-06-28 60 views
0

我讀過幾種強制窗口在C#前臺顯示的方法,利用Win32的user32.dll。在Windows 8上設置前景窗口

這些工作完美,除了一個情況。 在Windows 8上,如果開始菜單或Windows應用商店應用位於前臺,則這些將失敗。

我只需要在開始菜單位於前景時進行此項工作。 有沒有一種隱藏的方式來實現這一點?

+0

我可能是錯的,但我相信你能得到這與http://msdn.microsoft.com/en-us/library/windows/desktop/ms633545(v=vs.85).aspx完成。問題是搜索窗口已經是最頂層的了,所以你需要獲得該窗口的句柄並將其更改爲zOrder,然後使用相同的函數將窗口設置爲最頂層。 – Marco

+1

沒有自己嘗試,但我遇到了一些報告,它沒有:http://stackoverflow.com/questions/15850230/setwindowpos-failing-to-bring-windows-to-front-in-windows- 8-when-windows-8-apps – dcastro

+0

我無法嘗試它,要麼我沒有Windows 8,但你需要明白的是,所有的Windows 8窗口在最終構建使用GDI他們有正常的句柄和你可以像以前一樣使用Win32函數,所以我相信這是值得你嘗試的。 – Marco

回答

2
  DispatcherHelper.CheckBeginInvokeOnUI(async() => 
      { 
       try 
       { 
        if (!this.IsActive) 
        { 
         //pressing windows button 
         InputSimulator.SimulateKeyPress(VirtualKeyCode.LWIN); 
        } 
        await Task.Delay(1000); 

        ApplicationRunningHelper.GetCurrentProcessOnFocus(); 
       } 
       catch (Exception ex) 
       { 
       ... 
       } 
      }); 

public static class ApplicationRunningHelper 
    { 
     [DllImport("user32.dll")] 
     private static extern bool SetForegroundWindow(IntPtr hWnd); 

     [DllImport("user32.dll")] 
     private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); 

     [DllImport("user32.dll")] 
     private static extern bool IsIconic(IntPtr hWnd); 

     [DllImport("user32.dll", SetLastError = true)] 
     public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); 

     // When you don't want the ProcessId, use this overload and pass 
     // IntPtr.Zero for the second parameter 
     [DllImport("user32.dll")] 
     public static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId); 

     [DllImport("kernel32.dll")] 
     public static extern uint GetCurrentThreadId(); 

     /// The GetForegroundWindow function returns a handle to the 
     /// foreground window. 
     [DllImport("user32.dll")] 
     public static extern IntPtr GetForegroundWindow(); 

     [DllImport("user32.dll")] 
     public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach); 

     [DllImport("user32.dll", SetLastError = true)] 
     public static extern bool BringWindowToTop(IntPtr hWnd); 

     [DllImport("user32.dll", SetLastError = true)] 
     public static extern bool BringWindowToTop(HandleRef hWnd); 

     [DllImport("user32.dll")] 
     public static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow); 

     //one source 
     private static int SW_HIDE = 0; 
     private static int SW_SHOWNORMAL = 1; 
     private static int SW_SHOWMINIMIZED = 2; 
     private static int SW_SHOWMAXIMIZED = 3; 
     private static int SW_SHOWNOACTIVATE = 4; 
     private static int SW_RESTORE = 9; 
     private static int SW_SHOWDEFAULT = 10; 

     //other source 
     private static int SW_SHOW = 5; 

     /// <summary> 
     /// check if current process already running. if runnung, set focus to 
     /// existing process and returns true otherwise returns false. 
     /// </summary> 
     /// <returns></returns> 
     public static bool GetCurrentProcessOnFocus() 
     { 
      try 
      { 
       Process me = Process.GetCurrentProcess(); 
       Process[] arrProcesses = Process.GetProcessesByName(me.ProcessName); 
       IntPtr hWnd = arrProcesses[0].MainWindowHandle; 
       ForceForegroundWindow(hWnd); 
       return true; 
      } 
      catch (Exception) 
      { 
       return false; 
      } 
     } 

     public static void ForceForegroundWindow(IntPtr hWnd) 
     { 
      uint foreThread = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero); 
      uint appThread = GetCurrentThreadId(); 
      const uint SW_SHOW = 5; 

      if (foreThread != appThread) 
      { 
       AttachThreadInput(foreThread, appThread, true); 
       BringWindowToTop(hWnd); 
       ShowWindow(hWnd, SW_SHOW); 
       AttachThreadInput(foreThread, appThread, false); 
      } 
      else 
      { 
       BringWindowToTop(hWnd); 
       ShowWindow(hWnd, SW_SHOW); 
      } 
     } 
    } 
+1

你做了我的一天。 – GenericTeaCup