Bring A Window to the Front

  1. 4 years ago

    Stephen D

    20 Mar 2014 Pre-Release Testers

    Anyone know of a Cocoa declare to bring a specific window (not all the windows) to the front?

    ( Window.show only works if the application is already in the front.)

    BTW, you can do this on Windows using the MBS Plugin:

    dim h as integer = theWindow.Handle
     call RemoteControlMBS.WinBringWindowToTop h
  2. Kem T

    20 Mar 2014 Pre-Release Testers, Xojo Pro New York

    So let's say the app has 5 open windows, and you're in Safari. You want the app to activate, but only, say, window3 to come to the front? The layering would be: window3, Safari windows, window5, window4, window2, window1?

  3. Stephen D

    20 Mar 2014 Pre-Release Testers

    That's right. Essentially I want to display an interactive dialog for someone to answer which then closes itself to let them get back to what they were doing.

  4. Kem T

    20 Mar 2014 Pre-Release Testers, Xojo Pro New York

    It took a little looking around, but I found it in MacOSLib. Here is the code with that package installed:

      dim nsw as NSWindow = windowToActivate
      
      dim cntrl as new NSWindowController( nsw )
      cntrl.ShowWindow
      NSApplication.App.Activate( true )

    Otherwise, look at the declares behind those calls.

  5. 2 years ago
    Edited 2 years ago

    Thanks Kem.

    Any code for Win32?

    Lennox

  6. Michel B

    24 May 2015 Pre-Release Testers, Xojo Pro

    @Lennox J Thanks Kem.

    Any code for Win32?

    Lennox

    https://forum.xojo.com/4603-make-a-window-topmost

  7. Thanks Michel,

    I haven't tried it as yet, but reading the code it appears that the window will always be topmost. I will try that code and see what happens...

    What I was hoping for was that if the window is behind another window, e.g. I have myAppWindow in front then I open another app e.g. Internet Explorer, then Internet Explorer will open a window in front of myAppWindow.

    I now forgot that I have myAppWindow opened, but it is in the background, completely hidden by Internet Explorer, and I now try to open myApp.

    I already have a mutex to prevent myApp from creating a new window, so the message indicating same is show.

    When I dismiss that msgbox,, I would like myAppWindow to now be topmost.

    Kem's code above does that on Mac.

    Thanks again.

    Lennox

  8. Michel B

    24 May 2015 Pre-Release Testers, Xojo Pro
    Edited 2 years ago

    @Lennox J Thanks Michel,

    I haven't tried it as yet, but reading the code it appears that the window will always be topmost. I will try that code and see what happens...

    What I was hoping for was that if the window is behind another window, e.g. I have myAppWindow in front then I open another app e.g. Internet Explorer, then Internet Explorer will open a window in front of myAppWindow.

    I now forgot that I have myAppWindow opened, but it is in the background, completely hidden by Internet Explorer, and I now try to open myApp.

    I already have a mutex to prevent myApp from creating a new window, so the message indicating same is show.

    When I dismiss that msgbox,, I would like myAppWindow to now be topmost.

    Kem's code above does that on Mac.

    Thanks again.

    Lennox

    That code does not make the window global floating, it just brings it to front when you run it. If any other app is run afterward or another window is displayed by the app, it works as usual.

    Place the code in the place where you need it to bring the window to front. In what you describe, that would be after the MsgBox.

    I placed it in a 10 seconds timer Action, it does bring the window to the front from behind. It does not activate it, though. You may need some additional code for that.

      dim l,t,h,w as int32
      l = self.left
      t=self.top
      h=self.Height
      w=self.Width
      
      Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Int32, ByVal hWndInsertAfter As Int32, ByVal x As Int32, ByVal y As Int32, ByVal cx As Int32, ByVal cy As Int32, ByVal uFlags As Int32) As Int32
      
      const HWND_BOTTOM = 1
      const HWND_NOTOPMOST = -2
      const HWND_TOP = 0
      const HWND_TOPMOST = -1
      
      call SetWindowPos(Self.Handle, HWND_TOPMOST, l, t,w,h,0)
  9. OK Michel Thanks,

    I am not on PC today, so I will try it tomorrow.

    Thanks again.

    Lennox

or Sign Up to reply!