例如:
启动程序后,让程序在后台运行,当按F5时,开始执行程序,按F6停止执行,
怎么实现?

解决方案 »

  1.   

    後台是指什麼?
    比如 象vb 響應F5嘛?
      

  2.   

    剛看見一個熱鍵的exp
    Option Explicit
    ' ***********************************
    ' Author : Imran Zaheer
    ' Email  : [email protected]
    ' Web    : www.imraanz.com
    ' Y2K
    Dim retVal0 As Boolean, retVal1 As Boolean, retVal2 As BooleanPrivate Sub Form_Load()    MsgBox "Registering CTRL+F10, CTRL+F11, CTRL+F12 as hot keys..."
        
        retVal0 = RegisterHotKey(Me.hwnd, 0, MOD_CTRL, VK_F10)
        If Not retVal0 Then
            MsgBox "Can not register all or one of the hotkeys CTRL+F10 ... Try other keys this key is already registered by some other running applications.", vbCritical
        End If
        
        retVal1 = RegisterHotKey(Me.hwnd, 1, MOD_CTRL, VK_F11)
        If Not retVal1 Then
            MsgBox "Can not register all or one of the hotkeys CTRL+F11 ... Try other keys this key is already registered by some other running applications.", vbCritical
        End If
        
        retVal2 = RegisterHotKey(Me.hwnd, 2, MOD_CTRL, VK_F12)
        If Not retVal2 Then
            MsgBox "Can not register all or one of the hotkeys CTRL+F12 ... Try other keys this key is already registered by some other running applications.", vbCritical
        End If
        
        If (retVal0 = False And retVal1 = False And retVal2 = False) Then
            MsgBox "No Hotkey could be registered ...!", vbCritical
            End
        End If
        
        ' Subclassing the form to get the Windows callback msgs.
        glWinRet = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf CallbackMsgs)
        Me.Hide
        
    End Sub
    Private Sub Form_Resize()    If Me.WindowState = 1 Then
            Me.Hide
        End If
        
    End Sub
    Private Sub Form_Unload(Cancel As Integer)    ' If first hotkey is registered then
        ' unregister it.
        If retVal0 Then
            UnregisterHotKey Me.hwnd, 0
        End If
        
        ' If second hotkey is registered then
        ' unregister it.
        If retVal1 Then
            UnregisterHotKey Me.hwnd, 1
        End If
        
        ' If third hotkey is registered then
        ' unregister it.
        If retVal2 Then
            UnregisterHotKey Me.hwnd, 2
        End If
        
    End Sub
      

  3.   

    Option Explicit' ***********************************
    ' Author : Imran Zaheer
    ' Email  : [email protected]
    ' Web    : www.imraanz.com
    ' Y2K
    ' Module : Contains declarations and functions for
    '          vHotKeys.Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, _
        ByVal ID As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
        
    Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, _
        ByVal ID As Long) As LongDeclare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
        (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongDeclare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
        (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongPublic Const WM_HOTKEY = &H312
    Public Const GWL_WNDPROC = -4Public Const MOD_CTRL = &H2 'This example uses CTRL
    Public Const MOD_SHFT = &H4
    Public Const MOD_ALT = &H1' VK_A thru VK_Z are the same as their ASCII equivalents: 'A' thru 'Z'
    ' VK_0 thru VK_9 are the same as their ASCII equivalents: '0' thru '9'
    ' and others are listed belowPublic Const VK_NUMPAD0 = &H60
    Public Const VK_NUMPAD1 = &H61
    Public Const VK_NUMPAD2 = &H62
    Public Const VK_NUMPAD3 = &H63
    Public Const VK_NUMPAD4 = &H64
    Public Const VK_NUMPAD5 = &H65
    Public Const VK_NUMPAD6 = &H66
    Public Const VK_NUMPAD7 = &H67
    Public Const VK_NUMPAD8 = &H68
    Public Const VK_NUMPAD9 = &H69
    Public Const VK_MULTIPLY = &H6A
    Public Const VK_ADD = &H6B
    Public Const VK_SEPARATOR = &H6C
    Public Const VK_SUBTRACT = &H6D
    Public Const VK_DECIMAL = &H6E
    Public Const VK_DIVIDE = &H6F
    Public Const VK_F1 = &H70
    Public Const VK_F2 = &H71
    Public Const VK_F3 = &H72
    Public Const VK_F4 = &H73
    Public Const VK_F5 = &H74
    Public Const VK_F6 = &H75
    Public Const VK_F7 = &H76
    Public Const VK_F8 = &H77
    Public Const VK_F9 = &H78
    Public Const VK_F10 = &H79 ' This example uses F10
    Public Const VK_F11 = &H7A ' This example uses F11
    Public Const VK_F12 = &H7B ' This example uses F12
    Public Const VK_F13 = &H7C
    Public Const VK_F14 = &H7D
    Public Const VK_F15 = &H7E
    Public Const VK_F16 = &H7F
    Public Const VK_F17 = &H80
    Public Const VK_F18 = &H81
    Public Const VK_F19 = &H82
    Public Const VK_F20 = &H83
    Public Const VK_F21 = &H84
    Public Const VK_F22 = &H85
    Public Const VK_F23 = &H86
    Public Const VK_F24 = &H87Public glWinRet As Long
    ' Function : CallbackMsgs
    ' This functions is used as a parameter in the
    ' API SetWindowLong(), by AddresOf operator, so as to
    ' Subclass the form to get the Windows Callback msgs...
    Public Function CallbackMsgs(ByVal wHwnd As Long, ByVal wmsg As Long, ByVal wp_id As Long, ByVal lp_id As Long) As Long
        If wmsg = WM_HOTKEY Then
            Call DoFunctions(wp_id)
            CallbackMsgs = 1
            Exit Function
        End If
        CallbackMsgs = CallWindowProc(glWinRet, wHwnd, wmsg, wp_id, lp_id)
    End Function
    ' Sub : DoFunction
    ' Activated by the Function "CallbackMsgs()" whenever
    ' a hotkey is pressed.
    Public Sub DoFunctions(ByVal vKeyID As Byte)
        ' Important Notes :
        ' Do not include any msgboxes or Modal forms in
        ' this procedure, else if you include then by
        ' pressing the Hotkey twice/thrice the application
        ' will be terminated abnormally.
        '
        ' But if it is a requirement for you to include the
        ' Modal forms or msgbox in this procedure then put
        ' the RegisterHotKey() API before hiding the Form
        ' and put the UnRegisterHotKey() API before Showing
        ' the form.
        
        Form1.Show
        Form1.WindowState = 0
        DoEvents
        ' When the Hotkey is pressed once
        ' check if the Dofunctions() has completed
        ' before the CallbackMsgs().
        ' This check is not required if the form is
        ' minimized in the SysTray ...
        If Form1.Visible = False Then
            Form1.Show
            Form1.WindowState = 0
        End If
        
        Form1.Cls
        
        If vKeyID = 0 Then
            Form1.Label1.Caption = "1st HotKey Pressed !"
        Else
            If vKeyID = 1 Then
                Form1.Label1.Caption = "2nd HotKey Pressed !"
            Else
                Form1.Label1.Caption = "3rd HotKey Pressed !"
            End If
        End If
    End Sub
      

  4.   

    感谢楼上兄弟的解答先!
    我的意思是,A程序运行后,我可以浏览网页,听音乐,看电影.....做很多事情,(此时A程序窗体不在最上方)但无论在做什么,只要按F5,A程序就开始执行某段代码,怎么实现?