wxFrame bring to front Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
wxwxwx
I live to help wx-kind
I live to help wx-kind
Posts: 180
Joined: Thu Dec 20, 2012 4:25 pm

wxFrame bring to front

Post by wxwxwx »

Hello,
how to bring a wxFrame definitely to front. wx302 on windows.
Raise does it not. A click on titlebar or task icon does it.

Thank you
DenDev
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 231
Joined: Mon Jan 19, 2015 1:45 pm

Re: wxFrame bring to front

Post by DenDev »

Dunno if this works, but if the window you raise is a child window you might want to raise the app's main window first:

Code: Select all

void RaiseFrame(wxFrame *someFrame)
{
wxWindow pw = (wxWindow*)someFrame;
while (pw->GetParent()) pw = pw->GetParent();
pw->Raise();
someFrame->Raise();
}
Would be nice with a wxApp::Raise(), though ;-)
I have a bad habbit of not testing the code I post :D
wxwxwx
I live to help wx-kind
I live to help wx-kind
Posts: 180
Joined: Thu Dec 20, 2012 4:25 pm

Re: wxFrame bring to front

Post by wxwxwx »

The frame is minimized.
When calling Raise, the task icon get's highlighted but not restored.
When pressing with the mouse on the task icon it is restored correctly.
wxwxwx
I live to help wx-kind
I live to help wx-kind
Posts: 180
Joined: Thu Dec 20, 2012 4:25 pm

Re: wxFrame bring to front

Post by wxwxwx »

wxFrame::Restore
wxFrame::Raise
does the job.

(perhaps ::Raise is not needed)
DenDev
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 231
Joined: Mon Jan 19, 2015 1:45 pm

Re: wxFrame bring to front

Post by DenDev »

wxwxwx wrote:The frame is minimized.
When calling Raise, the task icon get's highlighted but not restored.
When pressing with the mouse on the task icon it is restored correctly.
Maybe giving the information that "the frame is minimized" would have been helpfull in the beginning.. But I'm not entirely sure.... ;-)
I have a bad habbit of not testing the code I post :D
wxwxwx
I live to help wx-kind
I live to help wx-kind
Posts: 180
Joined: Thu Dec 20, 2012 4:25 pm

Re: wxFrame bring to front

Post by wxwxwx »

yes.sorry
ColinTaylor
Earned a small fee
Earned a small fee
Posts: 21
Joined: Wed Oct 30, 2013 9:22 am

Re: wxFrame bring to front

Post by ColinTaylor »

The following works fine on windows (tested extensively on XP, 7 and 8 / 8.1), not so sure how it is on Linux / other platforms. I'd like to claim the credit for writing it, but I can't - I've tried to acknowledge all those who contributed part of the solution ...

Code: Select all

bool CWindowUtilities::ForceWindowToForeground (wxTopLevelWindow* Window)
{
   // Code from Thomas Stutz @ delphi3000.com
   // Converted to Borland C++ Builder Code by Wolfgang Frisch
   // Converted to a Borland independent version by Kai Schaeffer @ schaeffer-ag.de
   // Converted to wxWindows version by me

   bool Result = false;

#ifdef __WXMSW__

   const HWND hwnd = Window->GetHWND ();
   DWORD nullvalue = 0;
   DWORD ForegroundThreadID;
   DWORD ThisThreadID;
   DWORD timeout;

   // restore the window if currently iconised
   if (IsIconic (hwnd))
   {
      ShowWindow (hwnd, SW_RESTORE);
   }

   // nothing more to do if already the fg window
   if (GetForegroundWindow () == hwnd)
   {
      Result = true;
   }
   else
   {
      // Windows 98/2000 doesn't want to foreground a window when some other
      // window has keyboard focus
      OSVERSIONINFO VersionInfo;
      VersionInfo.dwOSVersionInfoSize=sizeof (OSVERSIONINFO);
      GetVersionEx (&VersionInfo);
      int Win32Platform = VersionInfo.dwPlatformId;
      int Win32MajorVersion = VersionInfo.dwMajorVersion;
      int Win32MinorVersion = VersionInfo.dwMinorVersion;
      if ( ((Win32Platform == VER_PLATFORM_WIN32_NT) && (Win32MajorVersion> 4))
            || ((Win32Platform == VER_PLATFORM_WIN32_WINDOWS)
                && ((Win32MajorVersion > 4) || ((Win32MajorVersion == 4) &&
                      (Win32MinorVersion > 0)))) )
      {
         // Code from Karl E. Peterson, www.mvps.org/vb/sample.htm
         // Converted to Delphi by Ray Lischner
         // Published in The Delphi Magazine 55, page 16
         Result = false;
         ForegroundThreadID = GetWindowThreadProcessId (GetForegroundWindow (), NULL);
         ThisThreadID = GetWindowThreadProcessId (hwnd, NULL);
         if (AttachThreadInput (ThisThreadID, ForegroundThreadID, true))
         {
            BringWindowToTop (hwnd); // IE 5.5 related hack
            SetForegroundWindow (hwnd);
            AttachThreadInput (ThisThreadID, ForegroundThreadID, false);
            Result = (GetForegroundWindow () == hwnd);
         }
         if (!Result)
         {
            // Code by Daniel P. Stasinski
            SystemParametersInfo (SPI_GETFOREGROUNDLOCKTIMEOUT, 0, &timeout, 0);
            SystemParametersInfo (SPI_SETFOREGROUNDLOCKTIMEOUT, 0, &nullvalue, SPIF_SENDCHANGE);
            BringWindowToTop (hwnd); // IE 5.5 related hack
            SetForegroundWindow (hwnd);
            SystemParametersInfo (SPI_SETFOREGROUNDLOCKTIMEOUT, 0, &timeout, SPIF_SENDCHANGE);
         }
      }
      else
      {
         BringWindowToTop (hwnd); // IE 5.5 related hack
         SetForegroundWindow (hwnd);
      }
      Result = (GetForegroundWindow () == hwnd);
   }

#else
   Window->Iconize (false);      // restore the window if minimized
   Window->SetFocus ();          // focus on my window
   Window->Raise ();             // bring window to front
   Window->Show (true);          // show the window
   Result = true;
#endif

   return Result;
}
Anil8753
Experienced Solver
Experienced Solver
Posts: 93
Joined: Sat Jan 16, 2016 5:57 am
Location: India

Re: wxFrame bring to front

Post by Anil8753 »

It worked.
ColinTaylor wrote: Wed Mar 04, 2015 1:00 pm The following works fine on windows (tested extensively on XP, 7 and 8 / 8.1), not so sure how it is on Linux / other platforms. I'd like to claim the credit for writing it, but I can't - I've tried to acknowledge all those who contributed part of the solution ...

Code: Select all

bool CWindowUtilities::ForceWindowToForeground (wxTopLevelWindow* Window)
{
   // Code from Thomas Stutz @ delphi3000.com
   // Converted to Borland C++ Builder Code by Wolfgang Frisch
   // Converted to a Borland independent version by Kai Schaeffer @ schaeffer-ag.de
   // Converted to wxWindows version by me

   bool Result = false;

#ifdef __WXMSW__

   const HWND hwnd = Window->GetHWND ();
   DWORD nullvalue = 0;
   DWORD ForegroundThreadID;
   DWORD ThisThreadID;
   DWORD timeout;

   // restore the window if currently iconised
   if (IsIconic (hwnd))
   {
      ShowWindow (hwnd, SW_RESTORE);
   }

   // nothing more to do if already the fg window
   if (GetForegroundWindow () == hwnd)
   {
      Result = true;
   }
   else
   {
      // Windows 98/2000 doesn't want to foreground a window when some other
      // window has keyboard focus
      OSVERSIONINFO VersionInfo;
      VersionInfo.dwOSVersionInfoSize=sizeof (OSVERSIONINFO);
      GetVersionEx (&VersionInfo);
      int Win32Platform = VersionInfo.dwPlatformId;
      int Win32MajorVersion = VersionInfo.dwMajorVersion;
      int Win32MinorVersion = VersionInfo.dwMinorVersion;
      if ( ((Win32Platform == VER_PLATFORM_WIN32_NT) && (Win32MajorVersion> 4))
            || ((Win32Platform == VER_PLATFORM_WIN32_WINDOWS)
                && ((Win32MajorVersion > 4) || ((Win32MajorVersion == 4) &&
                      (Win32MinorVersion > 0)))) )
      {
         // Code from Karl E. Peterson, www.mvps.org/vb/sample.htm
         // Converted to Delphi by Ray Lischner
         // Published in The Delphi Magazine 55, page 16
         Result = false;
         ForegroundThreadID = GetWindowThreadProcessId (GetForegroundWindow (), NULL);
         ThisThreadID = GetWindowThreadProcessId (hwnd, NULL);
         if (AttachThreadInput (ThisThreadID, ForegroundThreadID, true))
         {
            BringWindowToTop (hwnd); // IE 5.5 related hack
            SetForegroundWindow (hwnd);
            AttachThreadInput (ThisThreadID, ForegroundThreadID, false);
            Result = (GetForegroundWindow () == hwnd);
         }
         if (!Result)
         {
            // Code by Daniel P. Stasinski
            SystemParametersInfo (SPI_GETFOREGROUNDLOCKTIMEOUT, 0, &timeout, 0);
            SystemParametersInfo (SPI_SETFOREGROUNDLOCKTIMEOUT, 0, &nullvalue, SPIF_SENDCHANGE);
            BringWindowToTop (hwnd); // IE 5.5 related hack
            SetForegroundWindow (hwnd);
            SystemParametersInfo (SPI_SETFOREGROUNDLOCKTIMEOUT, 0, &timeout, SPIF_SENDCHANGE);
         }
      }
      else
      {
         BringWindowToTop (hwnd); // IE 5.5 related hack
         SetForegroundWindow (hwnd);
      }
      Result = (GetForegroundWindow () == hwnd);
   }

#else
   Window->Iconize (false);      // restore the window if minimized
   Window->SetFocus ();          // focus on my window
   Window->Raise ();             // bring window to front
   Window->Show (true);          // show the window
   Result = true;
#endif

   return Result;
}
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxFrame bring to front

Post by ONEEYEMAN »

Hi,
Raise() is doing exactly what is suppose to - bring the application main window to the front.
If you start the application, then switch to another one and then call Raise() the application window will become "raised", i.e. becomes the first one in Z-order
When you talk about minimizing, you should call some other function.

Thank you.
Post Reply