Keep form active?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bullitt
    New Member
    • Dec 2007
    • 21

    Keep form active?

    Hello,

    Is it possible to keep a form active, also when clicking on another application? I don't mean in the foreground, because I got that already, but now when I click on another application, my form becomes inactive.

    Already thanks for the help.
  • lotus18
    Contributor
    • Nov 2007
    • 865

    #2
    Originally posted by Bullitt
    Hello,

    Is it possible to keep a form active, also when clicking on another application? I don't mean in the foreground, because I got that already, but now when I click on another application, my form becomes inactive.

    Already thanks for the help.
    I don't think it would be possible (i think). BTW, for what purpose?

    Comment

    • QVeen72
      Recognized Expert Top Contributor
      • Oct 2006
      • 1445

      #3
      Originally posted by Bullitt
      Hello,

      Is it possible to keep a form active, also when clicking on another application? I don't mean in the foreground, because I got that already, but now when I click on another application, my form becomes inactive.

      Already thanks for the help.
      Hi,

      What Version of VB?
      you want it to be on Top of all other windows...?

      Regards
      Veena

      Comment

      • Bullitt
        New Member
        • Dec 2007
        • 21

        #4
        It's because the script isn't noticing keypresses while it's inactive, and it would be handy if I can use the keyboard for navigating between different screens or applications. I can open other applications now, but I cant go back, except when using alt-tab. So it works but I can be improved.

        Comment

        • Bullitt
          New Member
          • Dec 2007
          • 21

          #5
          It is VB6.0, and it doesn't need to be on top, but it needs to be active. Another inactive application may be on top.

          Comment

          • Ali Rizwan
            Banned
            Contributor
            • Aug 2007
            • 931

            #6
            Originally posted by Bullitt
            It is VB6.0, and it doesn't need to be on top, but it needs to be active. Another inactive application may be on top.
            Use timer and code

            Form1.setfocus

            or want code for setting window always on top to other all windows??

            REGARDS

            >> ALI <<

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Windows sends keypresses to the window which has the focus. The only way (as far as I know) that you could see them when you don't, is to use the API to intercept the windows messages being sent to the other application. This topic has been covered a number of times. Just try searching on "keylogger" . You'll find that we're not very keen on helping people build them, though.

              Comment

              • Bullitt
                New Member
                • Dec 2007
                • 21

                #8
                Thanks for the tip, I'm going to try it with RegisterHotKey from the user32.dll.

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by Bullitt
                  Thanks for the tip, I'm going to try it with RegisterHotKey from the user32.dll.
                  Let us know how it turns out.

                  Comment

                  • Bullitt
                    New Member
                    • Dec 2007
                    • 21

                    #10
                    I got it all worked out now and it works.

                    I got a module called modHotkey.bas that holds the following script:

                    [CODE=vb]Option Explicit

                    Private Declare Function RegisterHotKey Lib "user32" _
                    (ByVal hwnd As Long, ByVal id As Long, _
                    ByVal fsModifiers As Long, ByVal vk As Long) As Long
                    Private Declare Function UnregisterHotKe y Lib "user32" _
                    (ByVal hwnd As Long, ByVal id As Long) As Long

                    Public Enum ModConst
                    MOD_ALT = &H1
                    MOD_CONTROL = &H2
                    MOD_SHIFT = &H4
                    End Enum

                    Public Const WM_HOTKEY = &H312

                    Private m_hkCount As Long

                    Function HotKeyActivate( ByVal hwnd As Long, _
                    Modifier As ModConst, Optional KeyCode As Integer) As Long

                    m_hkCount = m_hkCount + 1

                    ' 0 for no success, otherwise success
                    HotKeyActivate = RegisterHotKey( hwnd, m_hkCount, Modifier, KeyCode)

                    End Function

                    Function HotKeyDeactivat e(ByVal hwnd As Long)
                    Dim i As Integer
                    For i = 1 To m_hkCount
                    UnregisterHotKe y hwnd, i
                    Next i
                    m_hkCount = 0
                    End Function[/CODE]

                    Then I also have a module called modSubClass.bas with the following script.

                    [CODE=vb]Option Explicit

                    Private Declare Function CallWindowProcA Lib "user32" ( _
                    ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, _
                    ByVal Msg As Long, ByVal wParam As Long, _
                    ByVal lParam As Long) As Long

                    Public oldProc As Long

                    Public Function WndProc(ByVal hwnd As Long, ByVal uMsg As Long, _
                    ByVal wParam As Long, ByVal lParam As Long) As Long

                    'wParam - the number of the hotkey, its identification.
                    'lParam - HiWord is the Modifiere e.g. Shift, Ctrl, Alt
                    'lParam . LoWord is the KeyCode

                    WndProc = 0
                    If uMsg = WM_HOTKEY Then
                    If wParam = 1 Then
                    MsgBox "This is the script he runs when I press my hotkey"
                    End If
                    Else
                    'All other messages to the old Windowprocedure
                    WndProc = CallWindowProcA (oldProc, hwnd, uMsg, wParam, lParam)
                    End If
                    End Function[/CODE]

                    And in my form I have the following to activate the hotkey:

                    [CODE=vb]Option Explicit

                    Private Declare Function SetWindowLongA Lib "user32" ( _
                    ByVal hwnd As Long, ByVal nIndex As Long, _
                    ByVal dwNewLong As Long) As Long
                    Private Const GWL_WNDPROC = -4

                    Private Sub Form_Load()
                    oldProc = SetWindowLongA( Me.hwnd, GWL_WNDPROC, AddressOf WndProc)
                    HotKeyActivate Me.hwnd, MOD_ALT, Asc("S") 'hotkey 1
                    End Sub

                    Private Sub Form_Unload(Can cel As Integer)
                    HotKeyDeactivat e Me.hwnd
                    SetWindowLongA Me.hwnd, GWL_WNDPROC, oldProc
                    End Sub[/CODE]

                    This example only uses ALT+S as a hotkey. When you want to add hotkeys, just make them in the form script where the other one is. And in the module modSubClass, you just make in the wndProc.

                    If wParam = ? Then
                    MsgBox "This is the script he runs when I press my hotkey"
                    End If

                    With the '?' the numer of the hotkey. This number is given by m_hkCount and the first hotkey has 1, the second has 2 and so on.

                    Thanks for bringing me on the right track.
                    Last edited by Killer42; Jan 10 '08, 12:37 AM. Reason: Changed CODE tags to CODE=vb

                    Comment

                    Working...