請先看下面這個代碼,運行結果是:按下ctrl+c鍵時,窗體最小化。只要程序運行,不論form1是否為最上層的窗口,按此組合鍵都會使form1最小化。
這樣出現的問題是,當我運行了此程序,就不可以用快捷鍵進行電腦裡其它文件的copy操作。我想要解決的是,當此窗口不在最上層時,ctrl+c 在此程序中不起作用。Private Const MOD_ALT = &H1
Private Const MOD_CONTROL = &H2
Private Const MOD_SHIFT = &H4
Private Const PM_REMOVE = &H1
Private Const WM_HOTKEY = &H312
Private Type POINTAPI
    X As Long
    Y As Long
End Type
Private Type Msg
    hWnd As Long
    Message As Long
    wParam As Long
    lParam As Long
    time As Long
    pt As POINTAPI
End Type
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 UnregisterHotKey Lib "user32" (ByVal hWnd As Long, ByVal id As Long) As Long
Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare Function WaitMessage Lib "user32" () As Long
Private bCancel As Boolean
Private Sub ProcessMessages()
    Dim Message As Msg
    'loop until bCancel is set to True
    Do While Not bCancel
        'wait for a message
        WaitMessage
        'check if it's a HOTKEY-message
        If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
            'minimize the form
            WindowState = vbMinimized
        End If
        'let the operating system process other events
        DoEvents
    Loop
End SubPrivate Sub Form_Load()
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    Dim ret As Long
    bCancel = False
    'register the Ctrl-F hotkey
    ret = RegisterHotKey(Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyC)
    'show some information
    Me.AutoRedraw = True
    Me.Print "Press CTRL-C to minimize this form"
    'show the form and
    Show
    'process the Hotkey messages
    ProcessMessages
End Sub
Private Sub Form_Unload(Cancel As Integer)
    bCancel = True
    'unregister hotkey
    Call UnregisterHotKey(Me.hWnd, &HBFFF&)
End Sub

解决方案 »

  1.   

    先判断,再执行PeekMessage
    if form1.WindowState <> vbMinimized then
        If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
           WindowState = vbMinimized
        End If
    end if
      

  2.   

    晕,你用这么一大堆代码把ctrl+c注册成系统热键,当然是在整个系统里有效啦,换句话说,系统的ctrl+c复制热键被你修改掉了,自然是不再起作用了
    如果你只想在自己程序有焦点时按ctrl+c最小化自己,可以这样写Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
        If KeyCode = vbKeyC And Shift = 2 Then Me.WindowState = 1'就这一句就够了
    End Sub
      

  3.   

    BlueBeer(1win)大哥的方法很好,不过我在应用中感到按键反应不是很灵敏,所以我把他的KeyUp事件改为KeyDown感觉快了许多.
      

  4.   

    多謝BlueBeer(1win),剛找到想到此法,就收到。