CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2007
    Location
    in your pocket
    Posts
    339

    [RESOLVED] Window and Mouse Move

    Hai all,

    I have a 99% transparent window that kept OnTop that covers the whole screen.
    now i mouse move over that window and what i need is, the WindowFromPoint() should detect the windows under this window insted of detecting this window only.

    is this posible?

    i dont want to 100% transparent the window since if i click that will goto the windows under that. so i really need to detect the windows from point while the clicks should also not go to them.
    "Don't Bring A Knife To A Gun Fight"

  2. #2
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    Re: Window and Mouse Move

    This should work, when you click on the form then it will determine the window behind it. Have fun!
    Code:
    Option Explicit
    
    Private Type POINT_TYPE
      X As Long
      Y As Long
    End Type
    
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function GetCursorPos Lib "user32.dll" (lpPoint As POINT_TYPE) As Long
    Private Declare Function WindowFromPoint Lib "user32.dll" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
    Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
    Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
    Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    
    Private Const GWL_EXSTYLE       As Long = (-20)
    Private Const WS_EX_TRANSPARENT As Long = &H20&
    Private Const WS_EX_LAYERED     As Long = &H80000
    Private Const LWA_ALPHA         As Long = &H2
    Private Const SWP_NOMOVE        As Long = 2
    Private Const SWP_NOSIZE        As Long = 1
    Private Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
    Private Const HWND_TOPMOST      As Long = -1
    Private Const HWND_NOTOPMOST    As Long = -2
    
    Private Sub TranslucentForm(Frm As Form, TranslucenceLevel As Integer)
        ' 0 = completely transparent, 255 = completely opaque
        SetWindowLong Frm.hWnd, GWL_EXSTYLE, WS_EX_LAYERED
        SetLayeredWindowAttributes Frm.hWnd, 0, TranslucenceLevel, LWA_ALPHA
    End Sub
    
    Private Sub Form_Load()
        TranslucentForm Me, 80  ' make form semi-transparent
        SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS  ' set form always on-top
        ClickThru Me, False ' disable click-thru form
    End Sub
    
    Private Sub ClickThru(Frm As Form, bEnabled As Boolean)
        If bEnabled = True Then ' enable click-thru form
            SetWindowLong Frm.hWnd, GWL_EXSTYLE, GetWindowLong(Frm.hWnd, GWL_EXSTYLE) Or WS_EX_TRANSPARENT
        Else ' disable click thru
            SetWindowLong Frm.hWnd, GWL_EXSTYLE, GetWindowLong(Frm.hWnd, GWL_EXSTYLE) And Not WS_EX_TRANSPARENT
        End If
    End Sub
    
    Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        ' Display the text of whatever window the mouse clicks.
        ' Note that this could be a control on a program window.
        Dim mousepos    As POINT_TYPE   ' coordinates of the mouse cursor
        Dim wintext     As String
        Dim slength     As Long         ' receive title bar text and its length
        Dim hWnd        As Long         ' handle to the window found at the point
        Dim retval      As Long         ' return value
        
        ClickThru Me, True
        ' Determine the window the mouse cursor is over.
        retval = GetCursorPos(mousepos)  ' get the location of the mouse
        hWnd = WindowFromPoint(mousepos.X, mousepos.Y)  ' determine the window that's there
        If hWnd = 0 Then  ' error or no window at that point
            Debug.Print "No window exists at that location."
            ClickThru Me, False
            Exit Sub
        End If
        
        ' Display that window's title bar text
        slength = GetWindowTextLength(hWnd)  ' get length of title bar text
        wintext = Space(slength + 1)  ' make room in the buffer to receive the string
        slength = GetWindowText(hWnd, wintext, slength + 1)  ' get the text
        wintext = Left(wintext, slength)  ' extract the returned string from the buffer
        Debug.Print "Window text: "; wintext
        ClickThru Me, False
    End Sub

  3. #3
    Join Date
    Jul 2007
    Location
    in your pocket
    Posts
    339

    Re: Window and Mouse Move

    Hey dee
    sub or func not defined
    ClickThru Me, False



    edited : ooOps, its there.
    "Don't Bring A Knife To A Gun Fight"

  4. #4
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    Re: Window and Mouse Move

    So how did it go?

  5. #5
    Join Date
    Jul 2007
    Location
    in your pocket
    Posts
    339

    Re: Window and Mouse Move

    Thank You dee-u,
    exactly what i want

    one more question
    can you explain me exactly what happen to the window when you install the WS_EX_TRANSPRENT? Coz i wonder y the form does not become fully transprent after installing this style.

    Code:
            SetWindowLong Frm.hWnd, GWL_EXSTYLE, GetWindowLong(Frm.hWnd, GWL_EXSTYLE) Or WS_EX_TRANSPARENT
    "Don't Bring A Knife To A Gun Fight"

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: [RESOLVED] Window and Mouse Move

    because you are using OR with the wrong bracket?

    Code:
    SetWindowLong Frm.hWnd, GWL_EXSTYLE, GetWindowLong(Frm.hWnd, GWL_EXSTYLE Or WS_EX_TRANSPARENT)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Jul 2007
    Location
    in your pocket
    Posts
    339

    Re: [RESOLVED] Window and Mouse Move

    Quote Originally Posted by dglienna View Post
    because you are using OR with the wrong bracket?

    Code:
    SetWindowLong Frm.hWnd, GWL_EXSTYLE, GetWindowLong(Frm.hWnd, GWL_EXSTYLE Or WS_EX_TRANSPARENT)
    what realy or does here ?

    say in this code
    Code:
    ...GWL_EXSTYLE OR WS_EX_TRANSPARENT
    "Don't Bring A Knife To A Gun Fight"

  8. #8
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: [RESOLVED] Window and Mouse Move

    but you posted this:
    Code:
    ...GWL_EXSTYLE) Or WS_...
    which OR's the result
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured