有个用form2.Show (1)方法显示的窗口,想实现鼠标在窗口外点击(窗口标题栏闪动)时自动实现Unload Me,请问怎么实现

解决方案 »

  1.   

    不行,lostfocus只能用于forme.visible = true 方法显示的子窗体(在任务栏有单独的图标,点击窗体外可以被其他窗体覆盖)有用,但是对于用form.Show(1)方法显示的窗体(在窗体外点击,窗体标题栏会闪动,窗体不会消失,任务栏无图标)没有用。
      

  2.   

    使用GetActiveWindow来获得当前活动的窗口 判断是否为你的form2
    'This Project needs
    '- two timers, interval=100
    '- a button'in general section
    Private Type POINTAPI
        x As Long
        y As Long
    End TypePrivate Declare Function GetActiveWindow Lib "user32" () As Long
    Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function Ellipse Lib "gdi32" (ByVal hdc As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As LongPrivate Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Sub Form_Load()
        Timer1.Interval = 100
        Timer1.Enabled = True
        Timer2.Interval = 100
        Timer2.Enabled = True
        Command1.Caption = "Draw Text"
    End Sub
    'This will draw an Ellipse on the active window
    Sub Timer1_Timer()
        Dim Position As POINTAPI
        'Get the cursor position
        GetCursorPos Position
        'Draw the Ellipse on the Screen's DC
        Ellipse GetWindowDC(0), Position.x - 5, Position.y - 5, Position.x + 5, Position.y + 5
    End Sub
    Sub Command1_Click()
        Dim intCount As Integer, strString As String
        strString = "Cool, text on screen !"
        For intCount = 0 To 30
            'Draw the text on the screen
            TextOut GetWindowDC(0), intCount * 20, intCount * 20, strString, Len(strString)
        Next intCount
    End Sub
    Private Sub Timer2_Timer()
        'Draw the text to the active window
        TextOut GetWindowDC(GetActiveWindow), 50, 50, "This is a form", 14
    End Sub 
      

  3.   

    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
    End Type
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type
    Dim p As POINTAPI, r As RECT
    Private Sub Timer1_Timer()
        a = GetAsyncKeyState(1)
        If a <> -32767 Then Exit Sub
        GetCursorPos p
        GetWindowRect hwnd, r
        If p.X < r.Left Or p.Y < r.Top Or p.X > r.Right Or p.Y > r.Bottom Then Unload Me
    End Sub
      

  4.   

    只需要用 API 函数 SetCapture me.hwnd 将鼠标点击捕捉到窗体上,如果在窗体外点击,那么 Form_Click 中传入的 x,y 坐标就在窗体外
      

  5.   

    Windows API setcapture 正解,顶!!!