Forcing a dialog box on top when the current form does not have focus.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • codedlogic
    New Member
    • Aug 2008
    • 3

    Forcing a dialog box on top when the current form does not have focus.

    Here is my problem, I have a Windows form (which may in the future be switched to WPF form) that gets minimized and hidden to the tray. At a set time an event is fired in a new thread which shows a dialog box that needs to be displayed on top of any current application windows for the user to interact with.

    I've spent several hours researching and playing with various methods trying to get this to work. I would prefer not to resort to direct win32 API calls, but if I need to, its a possibility I haven't ruled out. (I'm currently using C# on Windows Vista SP1)

    Any ideas on how to do this?
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Perhaps you can create your own dialog form that has it's TopMost property set to true. Then just launch that when you need to.

    Comment

    • codedlogic
      New Member
      • Aug 2008
      • 3

      #3
      Nope... doesn't work. If the parent form does not have focus, the dialog (even with topmost set to true) doesn't automatically get focus over top of other windows (firefox for example).

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Hmm. Well, that's no good. I'd never tried it, it was just an idea I had.

        Comment

        • codedlogic
          New Member
          • Aug 2008
          • 3

          #5
          Originally posted by insertAlias
          Hmm. Well, that's no good. I'd never tried it, it was just an idea I had.
          No problem,

          I finally figured out a solution which involves hitting the unmanaged User32.DLL, not the prettiest solution when your writting in .NET, but it gets the job done.

          C# functions to get and set the focus of the window on top of all the other windows:
          Code:
                  //     CLR   wrapping of unmanaged functions
          
                  public static int GetWindowHandle(System.String WindowTitle)
                  {
                      //a return value of 0 indicates that the function did not find the window
                      return FindWindow(null, WindowTitle);
                  }
          
                  public static void SetWindowFocus(int windowHandle)
                  {
                      SetForegroundWindow(windowHandle);
                  }
          
          
                  //DLL external functions hitting User32.dll
          
                  [DllImport("User32.dll")]
                  private static extern Int32 FindWindow(String lpClassName, String lpWindowName);
          
          
                  [DllImport("User32.dll")]
                  private static extern Boolean SetForegroundWindow( Int32 hWnd );

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            Good to know. I'll probably use that solution myself.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              What happens if you toss a MessageBox In there? Those darn things love to popup over the top of everything else. And clicking the "ok" button them will force your application into focus.

              Comment

              Working...