Results 1 to 9 of 9

Thread: 2 Questions --- Window Focus and Menus

  1. #1

    Thread Starter
    Fanatic Member Illspirit's Avatar
    Join Date
    Mar 2001
    Location
    Blackpool, England
    Posts
    815

    2 Questions --- Window Focus and Menus

    Hi,

    I have 2 questions:

    1) I have a form, and i want it so that when it looses focus, it hides. Ive tried putting me.hide in the forms Lost_Focus event, but if it only works if i move the focus to another form in my app If i click on the dsktop or another program's window, then i want it to hide.

    2) I have a three picture boxes, and when the user holds the mouse over them, they each show a different popup menu. How do i make it so that when one popup menu is shown, and the user moves the mouse pointer over another picturebox, then the popup menu that is visible hides, and the one for that picturebox shows???

    Thanx for any ideas
    Illspirit - [email protected]

    SmartBarXP Lead Developer
    SmartBarXP - The leading desktop sidebar application for Microsoft Windows XP

  2. #2

    Thread Starter
    Fanatic Member Illspirit's Avatar
    Join Date
    Mar 2001
    Location
    Blackpool, England
    Posts
    815
    any ideas??
    Illspirit - [email protected]

    SmartBarXP Lead Developer
    SmartBarXP - The leading desktop sidebar application for Microsoft Windows XP

  3. #3
    Addicted Member Michael Woolsey's Avatar
    Join Date
    Nov 2000
    Location
    Calgary, Alberta, Canada.
    Posts
    243
    There is probably a much better way of doing this, but this should aclomplish what you are looking for in regards to the lost focus question (#1).

    In a module, declare the following function:
    Code:
    Declare Function GetActiveWindow& Lib "user32" ()
    Place the following code on a simple form to see what is is doing...
    Code:
    Option Explicit
    Private myId As Long
    
    Private Sub Form_Activate()
      myId = GetActiveWindow
      
      Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
      Text1.Text = GetActiveWindow
      If myId <> GetActiveWindow Then
        Form1.Hide
        Timer1.Enabled = False
      End If
    End Sub
    This code assumes you have a timer control on your main form that is disabled at design time.

    You will probably want to minimize your form rather than .hide it, but that is your call. If you want to minimize it use Form1.WindowState = vbMinimized.


    Question #2:

    Why don't you set a flag or something similar when the user triggers your MouseMove Event for the given picture box? That way you know which picture box they are hovering over and you can act properly.


    Hope this helps.
    Michael
    Application/Web Developer

    Visual Basic 6.0 SP5
    Active Server Pages
    Oracle 9i
    - I'm going to live forever, or die trying!

  4. #4

    Thread Starter
    Fanatic Member Illspirit's Avatar
    Join Date
    Mar 2001
    Location
    Blackpool, England
    Posts
    815
    Michael, thanx for you reply

    As for your answer to question 1, that would work (btw, i do want to hide the form, its a form that acts like the start menu, which is why i want it to hide when it looses focus)

    For question 2, the problem is that when one popupmenu shows, the Mouse_Move event no longer picks up mouse movement over the other picture boxes, unless i click somewhere to close it Thanx anyways

    Any ideas for question 2
    Illspirit - [email protected]

    SmartBarXP Lead Developer
    SmartBarXP - The leading desktop sidebar application for Microsoft Windows XP

  5. #5
    Addicted Member Michael Woolsey's Avatar
    Join Date
    Nov 2000
    Location
    Calgary, Alberta, Canada.
    Posts
    243
    I'm pretty sure when you invoke "PopupMenu" it stops the code in its tracks, and only continues when you have closed the popupwindow.

    You may want to have another timer that checks the position of the mouse and if it is over the other picture boxes have it close the original popupmenu. You could have it do a sendkeys "{ESC}" if you want to do a quick hack type release. You would probably have to use a api function that gets the position of the mouse... you could potentially call the picturebox1.Mouse_Move event in the timer, but that may cause problems I don't know.

    I don't really like the use of timers personally. Since I haven't played with subclassing, I couldn't advise you on how to catch all the windows events and filter the ones you want for your form.

    I hope this gives you some ideas, if you need some more by all means ask.

    Michael
    Application/Web Developer

    Visual Basic 6.0 SP5
    Active Server Pages
    Oracle 9i
    - I'm going to live forever, or die trying!

  6. #6
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    To tell when focus leaves a form:

    'In a form (Form1)
    Code:
    Private Sub Form_Click()
        Dim ET As TRACKMOUSEEVENTTYPE
        'initialize structure
        ET.cbSize = Len(ET)
        ET.hwndTrack = Me.hWnd
        ET.dwFlags = TME_LEAVE
        'start the tracking
        TrackMouseEvent ET
        'show a message To the user
        Me.Print "Move the mouse cursor outside the form" + vbCrLf + "to generate a WM_MOUSELEAVE event"
    End Sub
    Private Sub Form_Load()
        'KPD-Team 2001
        'URL: [email protected]
        'E-Mail: [email protected]
        'show a warning message
        MsgBox "WARNING: This sample uses subclassing." + vbCrLf + "To End this program, always use the X button of the form." + vbCrLf + "Do Not use VB's Stop button And Do Not use the 'End' keyword In your VB code." + vbCrLf + vbCrLf + "For more information about subclassing, check out" + vbCrLf + "our subclassing tutorial at http://www.allapi.net/", vbExclamation
        'set the graphics mode To persistent
        Me.AutoRedraw = True
        Me.Print "Click the form To begin"
        'start subclassing this form
        HookForm Me
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
        'stop subclassing this form
        UnHookForm Me
    End Sub
    'In a module
    Code:
    Public Const TME_CANCEL = &H80000000
    Public Const TME_HOVER = &H1&
    Public Const TME_LEAVE = &H2&
    Public Const TME_NONCLIENT = &H10&
    Public Const TME_QUERY = &H40000000
    Public Const WM_MOUSELEAVE = &H2A3&
    Public Type TRACKMOUSEEVENTTYPE
        cbSize As Long
        dwFlags As Long
        hwndTrack As Long
        dwHoverTime As Long
    End Type
    
    Public Declare Function TrackMouseEvent Lib "user32" (lpEventTrack As TRACKMOUSEEVENTTYPE) As Long
    Public Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long
    'Subclassing is explained In our subclassing tutorial at http://www.allapi.net/
    Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Declare 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 Long
    Public Const GWL_WNDPROC = (-4)
    Public PrevProc As Long
    Public Sub HookForm(F As Form)
        PrevProc = SetWindowLong(F.hWnd, GWL_WNDPROC, AddressOf WindowProc)
    End Sub
    Public Sub UnHookForm(F As Form)
        SetWindowLong F.hWnd, GWL_WNDPROC, PrevProc
    End Sub
    Public Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        If uMsg = WM_MOUSELEAVE Then
            'if we receive a WM_MOUSELEAVE message, show it
            Form1.Print "The mouse left the form!"
        End If
        WindowProc = CallWindowProc(PrevProc, hWnd, uMsg, wParam, lParam)
    End Function

  7. #7

    Thread Starter
    Fanatic Member Illspirit's Avatar
    Join Date
    Mar 2001
    Location
    Blackpool, England
    Posts
    815

    Any Ideas???

    Thanx Nucleus!

    Can anyone help me out with the menu thing??
    Illspirit - [email protected]

    SmartBarXP Lead Developer
    SmartBarXP - The leading desktop sidebar application for Microsoft Windows XP

  8. #8

    Thread Starter
    Fanatic Member Illspirit's Avatar
    Join Date
    Mar 2001
    Location
    Blackpool, England
    Posts
    815
    Anyone?
    Illspirit - [email protected]

    SmartBarXP Lead Developer
    SmartBarXP - The leading desktop sidebar application for Microsoft Windows XP

  9. #9
    Tygur
    Guest
    What are you using for the popup menu?
    Another form? Or is is a real menu? (using PopupMenu)

    If it is a real menu, what's wrong with Michael's suggestion? Do you just need code?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width