Bring Another Application To Front?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Octavius Khan

    #1

    Bring Another Application To Front?

    I have an application that runs in the system tray and I use global hotkeys
    to perform functions. When certain hotkeys are pressed, my system tray
    application may display a message box and sometimes a balloontip, but then
    focus is taken away from the application the user is using when this
    happens. How can I return focus to the original application?

    For example, let's say the user is using Notepad and then presses Ctrl-F9.
    This causes the system tray application to paste a block of text into
    Notepad and then display a balloontip saying that it did this. Now, focus
    is taken away from Notepad and the titlebar is grey.

    Thanks for your assistance.


  • ShaneO

    #2
    Re: Bring Another Application To Front?

    Octavius Khan wrote:
    I have an application that runs in the system tray and I use global hotkeys
    to perform functions. When certain hotkeys are pressed, my system tray
    application may display a message box and sometimes a balloontip, but then
    focus is taken away from the application the user is using when this
    happens. How can I return focus to the original application?
    >
    For example, let's say the user is using Notepad and then presses Ctrl-F9.
    This causes the system tray application to paste a block of text into
    Notepad and then display a balloontip saying that it did this. Now, focus
    is taken away from Notepad and the titlebar is grey.
    >
    Thanks for your assistance.
    >
    >
    There are a number of issues that need to be addressed to achieve your
    required result.

    One that I can't answer at the moment (without further research) is how
    to determine which application "had" the focus prior to activating your
    application, however, you've probably already got an answer to that as
    your second paragraph mentions that you're able to paste a block of
    text. (You must know something about the original app, unless you're
    simply using the Clipboard??)

    Anyway, to switch back to the original app you first need to obtain its
    "MainWindowHand le" (ProcessHandle) .

    <AirCode - Not Completely Tested>

    Public Declare Auto Function ShowWindow Lib "user32.dll " (ByVal hwnd As
    IntPtr, ByVal nCmdShow As Int32) As Long
    Const SW_SHOWNORMAL As Int32 = 1
    Const SW_SHOW As Int32 = 5


    Private Function fnGetProcessHan dle(ByVal sAppTitle as String) As Long
    Dim Proc as Process
    For Each Proc in Process.GetProc esses
    If Proc.MainWindow Title.ToUpper.C ontains(sAppTit le) then
    Return Proc.MainWindow Handle
    End If
    Next
    End Function

    ShowWindow(fnGe tProcessHandle( "NOTEPAD", SW_SHOWNORMAL)


    This code will certainly switch you back to your designated app, you
    just need to be able to identify what the original application was first.

    I hope this helps.

    ShaneO

    There are 10 kinds of people - Those who understand Binary and those who
    don't.

    Comment

    • Octavius Khan

      #3
      Re: Bring Another Application To Front?

      Thanks, Shane.

      This was very helpful. You have certainly pointed me in the right
      direction.

      - Octavius

      "ShaneO" <spcacc@optusne t.com.auwrote in message
      news:45f60773$0 $16553$afc38c87 @news.optusnet. com.au...
      Octavius Khan wrote:
      >I have an application that runs in the system tray and I use global
      >hotkeys to perform functions. When certain hotkeys are pressed, my
      >system tray application may display a message box and sometimes a
      >balloontip, but then focus is taken away from the application the user is
      >using when this happens. How can I return focus to the original
      >application?
      >>
      >For example, let's say the user is using Notepad and then presses
      >Ctrl-F9. This causes the system tray application to paste a block of text
      >into Notepad and then display a balloontip saying that it did this. Now,
      >focus is taken away from Notepad and the titlebar is grey.
      >>
      >Thanks for your assistance.
      >
      There are a number of issues that need to be addressed to achieve your
      required result.
      >
      One that I can't answer at the moment (without further research) is how to
      determine which application "had" the focus prior to activating your
      application, however, you've probably already got an answer to that as
      your second paragraph mentions that you're able to paste a block of text.
      (You must know something about the original app, unless you're simply
      using the Clipboard??)
      >
      Anyway, to switch back to the original app you first need to obtain its
      "MainWindowHand le" (ProcessHandle) .
      >
      <AirCode - Not Completely Tested>
      >
      Public Declare Auto Function ShowWindow Lib "user32.dll " (ByVal hwnd As
      IntPtr, ByVal nCmdShow As Int32) As Long
      Const SW_SHOWNORMAL As Int32 = 1
      Const SW_SHOW As Int32 = 5
      >
      >
      Private Function fnGetProcessHan dle(ByVal sAppTitle as String) As Long
      Dim Proc as Process
      For Each Proc in Process.GetProc esses
      If Proc.MainWindow Title.ToUpper.C ontains(sAppTit le) then
      Return Proc.MainWindow Handle
      End If
      Next
      End Function
      >
      ShowWindow(fnGe tProcessHandle( "NOTEPAD", SW_SHOWNORMAL)
      >
      >
      This code will certainly switch you back to your designated app, you just
      need to be able to identify what the original application was first.
      >
      I hope this helps.
      >
      ShaneO
      >
      There are 10 kinds of people - Those who understand Binary and those who
      don't.

      Comment

      • Octavius Khan

        #4
        Re: Bring Another Application To Front?

        Thanks.

        This was very helpful!

        - Octavius

        "ShaneO" <spcacc@optusne t.com.auwrote in message
        news:45f60773$0 $16553$afc38c87 @news.optusnet. com.au...
        Octavius Khan wrote:
        >I have an application that runs in the system tray and I use global
        >hotkeys to perform functions. When certain hotkeys are pressed, my
        >system tray application may display a message box and sometimes a
        >balloontip, but then focus is taken away from the application the user is
        >using when this happens. How can I return focus to the original
        >application?
        >>
        >For example, let's say the user is using Notepad and then presses
        >Ctrl-F9. This causes the system tray application to paste a block of text
        >into Notepad and then display a balloontip saying that it did this. Now,
        >focus is taken away from Notepad and the titlebar is grey.
        >>
        >Thanks for your assistance.
        >
        There are a number of issues that need to be addressed to achieve your
        required result.
        >
        One that I can't answer at the moment (without further research) is how to
        determine which application "had" the focus prior to activating your
        application, however, you've probably already got an answer to that as
        your second paragraph mentions that you're able to paste a block of text.
        (You must know something about the original app, unless you're simply
        using the Clipboard??)
        >
        Anyway, to switch back to the original app you first need to obtain its
        "MainWindowHand le" (ProcessHandle) .
        >
        <AirCode - Not Completely Tested>
        >
        Public Declare Auto Function ShowWindow Lib "user32.dll " (ByVal hwnd As
        IntPtr, ByVal nCmdShow As Int32) As Long
        Const SW_SHOWNORMAL As Int32 = 1
        Const SW_SHOW As Int32 = 5
        >
        >
        Private Function fnGetProcessHan dle(ByVal sAppTitle as String) As Long
        Dim Proc as Process
        For Each Proc in Process.GetProc esses
        If Proc.MainWindow Title.ToUpper.C ontains(sAppTit le) then
        Return Proc.MainWindow Handle
        End If
        Next
        End Function
        >
        ShowWindow(fnGe tProcessHandle( "NOTEPAD", SW_SHOWNORMAL)
        >
        >
        This code will certainly switch you back to your designated app, you just
        need to be able to identify what the original application was first.
        >
        I hope this helps.
        >
        ShaneO
        >
        There are 10 kinds of people - Those who understand Binary and those who
        don't.

        Comment

        • Andrew Morton

          #5
          Re: Bring Another Application To Front?

          ShaneO wrote:
          Octavius Khan wrote:
          >I have an application that runs in the system tray and I use global
          >hotkeys to perform functions. When certain hotkeys are pressed, my
          >system tray application may display a message box and sometimes a
          >balloontip, but then focus is taken away from the application the
          >user is using when this happens. How can I return focus to the
          >original application? For example, let's say the user is using Notepad
          >and then presses
          >Ctrl-F9. This causes the system tray application to paste a block of
          >text into Notepad and then display a balloontip saying that it did
          >this. Now, focus is taken away from Notepad and the titlebar is
          >grey. Thanks for your assistance.
          >>
          >>
          >
          There are a number of issues that need to be addressed to achieve your
          required result.
          >
          One that I can't answer at the moment (without further research) is
          how to determine which application "had" the focus prior to
          activating your application, however, you've probably already got an
          answer to that as your second paragraph mentions that you're able to
          paste a block of text. (You must know something about the original
          app, unless you're simply using the Clipboard??)
          Wouldn't the previous app. be next down in the z-order of windows, so it
          could be found with the GetWindow function in user32.dll?
          http://msdn2.microsoft.com/en-us/library/ms633515.aspx

          Andrew


          Comment

          Working...