程序启动的时候就一直在做循环,当我按某个键时想停止怎么做呢?我用这个但是不起作用Private Sub Timer1_Timer()
    If GetAsyncKeyState(vbKeyF6) Then Unload Me
End Sub

解决方案 »

  1.   

    循环体里加个判断,如果为True就终止
      

  2.   

    If KeyCode = vbKeyF6 Then Unload Me 我直接加这个在循环体但是不起作用
      

  3.   

    以下代码按F2中断循环并退出form:Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As IntegerDim m_blnStop As BooleanPrivate Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        Dim lKeyCode As Long
        
        For lKeyCode = 1 To 127
            If GetAsyncKeyState(lKeyCode) <> 0 Then
                If lKeyCode = vbKeyF2 Then
                    m_blnStop = True
                    DoEvents
                    Unload Me
                End If
            End If
        Next
    End SubPrivate Sub Form_KeyPress(KeyAscii As Integer)
        '
    End SubPrivate Sub Form_Load()
        Dim i As Long
        
        Me.KeyPreview = True
        Me.Show
        
        Do While m_blnStop = False
            i = i + 1
            Debug.Print CStr(i) + ",正在循环中..."
            DoEvents
        Loop
    End Sub
      

  4.   

    如果只是想在按下某键时关闭窗口,则取消Form_Load中的循环即可。
      

  5.   

    本帖最后由 bcrun 于 2010-12-25 16:02:52 编辑
      

  6.   

    实际上楼主的代码是可行的,下面的代码经过测试都可行,只是中断循环与Unload Me是两回事Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As IntegerPrivate Sub Timer1_Timer()
    '    If GetAsyncKeyState(117) = -32767 Then Unload Me
        If GetAsyncKeyState(vbKeyF6) Then Unload Me
    End Sub
      

  7.   

    本帖最后由 bcrun 于 2010-12-25 16:04:20 编辑
      

  8.   

    下面的我测试过了,可用
    Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
    Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
    Dim x As Integer, y As Integer
    Dim Buffer As Long, hBitmap As Long, Desktop As Long, hScreen As Long, ScreenBuffer As Long
    Private Declare Sub InvalidateRect Lib "user32" (ByVal hwnd As Long, lpRect As Any, ByVal bErase As Long)
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Dim EndIt As Boolean
    Private Sub Timer1_Timer()
      If GetAsyncKeyState(vbKeyF6) Then EndIt = True
    End SubPrivate Sub Form_Load()
      Me.Hide
      EndIt = False
      Desktop = GetWindowDC(GetDesktopWindow())
      hBitmap = CreateCompatibleDC(Desktop)
      hScreen = CreateCompatibleDC(Desktop)
      Buffer = CreateCompatibleBitmap(Desktop, 32, 32)
      ScreenBuffer = CreateCompatibleBitmap(Desktop, Screen.Width / 15, Screen.Height / 15)
      SelectObject hBitmap, Buffer
      SelectObject hScreen, ScreenBuffer
      BitBlt hScreen, 0, 0, Screen.Width / 15, Screen.Height / 15, Desktop, 0, 0, SRCCOPY
      Timer1.Interval = 200
      For i = 0 To 1E+17
      If EndIt = True Then Exit For
      y = (Screen.Height / 15) * Rnd
      x = (Screen.Width / 15) * Rnd
      BitBlt hBitmap, 0, 0, 32, 32, Desktop, x, y, SRCCOPY
      BitBlt Desktop, x + (1 - 2 * Rnd), y + (1 - 2 * Rnd), 32, 32, hBitmap, 0, 0, SRCCOPY
      DoEvents
      Next i
      Unload Me
    End Sub