title: Win32: Bring a window to top
body: <p>I have a Windows program which has two 2 windows in it:</p>
  
  <pre><code>hwnd (main interface)
  
  hwnd2 (toplevel window, no parent, created by hwnd)
  </code></pre>
  
  <p>When I double click on hwnd, I need hwnd2 to pop up and show some data, so I use this function to bring hwnd2 to top:</p>
  
  <pre><code>BringWindowToTop(hwnd2);
  </code></pre>
  
  <p>hwnd2 is brought to top, but there is one thing odd. When I click on hwnd2 again, hwnd (main interface) pops itself up again automatically.
  I tried to use the following function to solve this problem, but non of them works.</p>
  
  <pre><code>SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
                                                                    //doesn't work
  
  BringWindowToTop(hwnd2);    //This is the function brings hwnd2 to top
  
  SetForegroundWindow(hwnd2); //doesn't work
  
  SetWindowPos(hwnd2, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); 
                                                                    //doesn't work
  
  SetWindowPos(hwnd2, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
                                         // hwnd2 "always" on top, not what I want
  
  SetActiveWindow(hwnd2); // doesn't work too (for replying to Magnus Skog, thanks)
  
  SwitchToThisWindow(hwnd2, TRUE);// got the same problem with BringWindowToTop function
  SwitchToThisWindow(hwnd2, FALSE);
  </code></pre>
  
  <p>How could I solve this problem?
  Thanks in advance.</p>
  
  <p>(for replying to aJ, hwnd2 doesn't have parent because it needs to be a toplevel window so it can be in front/back of other windows)</p>
  
  <p>(hwnd2 is a media player which is composed of several windows, one of the windows is for video dispaly, two other trackbar controls for progress bar and volume bar, one Toolbar control for control panel.)</p>
  
  <p>(There is one this might help, no matter which window I click on hwnd2, hwnd pops up automatically as loong as "the mouse is on top of hwnd in Z-order", including menu bar and  non-client area, etc.)</p>
  
  <p>(This media player is writen in Direct Show. I use IVideoWindow::put_Owner to put video window as the video owner, Direct Show internally creates a sub-video window as a child of the video window. Except for this sub-video window which I can't see the source code, I don't see any thing suspicious in hwnd2.)</p>
  
  <p>I found the reason, which is because of Direct Show. I use multithread to execute it, and then the problem's solved. But...why??</p>
  
  <p>This problem can be resolved by using PostMessage (rather than SendMessage).</p>