Advertisement
Guest User

ForceForegroundWindow Fix

a guest
Feb 18th, 2012
5,807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.55 KB | None | 0 0
  1. private void ForceForegroundWindow(IntPtr hWnd)
  2.       {
  3.           uint a;
  4.           WinAPI.LockSetForegroundWindow(WinAPI.LSFW_UNLOCK);
  5.           WinAPI.AllowSetForegroundWindow(WinAPI.ASFW_ANY);
  6.  
  7.           IntPtr hWndForeground = WinAPI.GetForegroundWindow();
  8.           SendKeys.SendWait("{UP}");
  9.           if (hWndForeground.ToInt32() != 0)
  10.           {
  11.               if (hWndForeground != hWnd)
  12.               {
  13.                   uint thread1 = WinAPI.GetWindowThreadProcessId(hWndForeground, out a);
  14.                   uint thread2 = WinAPI.GetCurrentThreadId();
  15.  
  16.  
  17.                   if (thread1 != thread2)
  18.                   {
  19.                       WinAPI.AttachThreadInput(thread1, thread2, true);
  20.                       WinAPI.LockSetForegroundWindow(WinAPI.LSFW_UNLOCK);
  21.                       WinAPI.AllowSetForegroundWindow(WinAPI.ASFW_ANY);
  22.                       WinAPI.BringWindowToTop(hWnd);
  23.                       if (WinAPI.IsIconic(hWnd))
  24.                       {
  25.                           WinAPI.ShowWindow(hWnd, WinAPI.ShowWindowFlags.SW_SHOWNORMAL);
  26.                       }
  27.                       else
  28.                       {
  29.                           WinAPI.ShowWindow(hWnd, WinAPI.ShowWindowFlags.SW_SHOW);
  30.                       }
  31.                       WinAPI.SetFocus(hWnd);
  32.                       WinAPI.AttachThreadInput(thread1, thread2, false);
  33.                   }
  34.                   else
  35.                   {
  36.                       WinAPI.AttachThreadInput(thread1, thread2, true);
  37.                       WinAPI.LockSetForegroundWindow(WinAPI.LSFW_UNLOCK);
  38.                       WinAPI.AllowSetForegroundWindow(WinAPI.ASFW_ANY);
  39.                       WinAPI.BringWindowToTop(hWnd);
  40.                       WinAPI.SetForegroundWindow(hWnd);
  41.                       WinAPI.SetFocus(hWnd);
  42.                       WinAPI.AttachThreadInput(thread1, thread2, false);
  43.  
  44.                   }
  45.                   if (WinAPI.IsIconic(hWnd))
  46.                   {
  47.                       WinAPI.AttachThreadInput(thread1, thread2, true);
  48.                       WinAPI.LockSetForegroundWindow(WinAPI.LSFW_UNLOCK);
  49.                       WinAPI.AllowSetForegroundWindow(WinAPI.ASFW_ANY);
  50.                       WinAPI.BringWindowToTop(hWnd);
  51.                       WinAPI.ShowWindow(hWnd, WinAPI.ShowWindowFlags.SW_SHOWNORMAL);
  52.                       WinAPI.SetFocus(hWnd);
  53.                       WinAPI.AttachThreadInput(thread1, thread2, false);
  54.                   }
  55.                   else
  56.                   {
  57.                       WinAPI.BringWindowToTop(hWnd);
  58.                       WinAPI.ShowWindow(hWnd, WinAPI.ShowWindowFlags.SW_SHOW);
  59.                   }
  60.               }
  61.               WinAPI.SetForegroundWindow(hWnd);
  62.               WinAPI.SetFocus(hWnd);
  63.           }
  64.           else
  65.           {
  66.               uint thread1 = WinAPI.GetWindowThreadProcessId(hWndForeground, out a);
  67.               uint thread2 = WinAPI.GetCurrentThreadId();
  68.               try
  69.               {
  70.                   WinAPI.AttachThreadInput(thread1, thread2, true);
  71.               }
  72.               catch
  73.               {
  74.                   uint failure = 1;
  75.               }
  76.               WinAPI.LockSetForegroundWindow(WinAPI.LSFW_UNLOCK);
  77.               WinAPI.AllowSetForegroundWindow(WinAPI.ASFW_ANY);
  78.               WinAPI.BringWindowToTop(hWnd);
  79.               WinAPI.SetForegroundWindow(hWnd);
  80.  
  81.               WinAPI.ShowWindow(hWnd, WinAPI.ShowWindowFlags.SW_SHOW);
  82.               WinAPI.SetFocus(hWnd);
  83.               WinAPI.AttachThreadInput(thread1, thread2, false);
  84.           }
  85.       }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement