VB. NET: Handle Titlebar clicks, hovers, and other events

Posted on 2004-11-01
Last Modified: 2010-04-24
Is it possible to wire events to a forms titlebar?

For example I want to do something when the user hovers over the title bar, or clicks the title bar, etc.

Question by:majnun
    LVL 4

    Expert Comment

    You can do as this points out and place the following code in form code.

    Overrides Protected Sub WndProc(ByRef m As Message)

    If m.Msg = &H84 And IntPtr.op_Equality(m.Result, New IntPtr(2))
    m.Result = New IntPtr(1)
    End If
    End Sub

    The if the mouses Y co-ordinates is < 0 then you are on the titlebar.


    Author Comment

    Can you break this down into plain english? It's way over my head.
    LVL 4

    Accepted Solution

    To start with I will make the method a little more clear:

        Protected Overrides Sub WndProc(ByRef windowMessage As Message)
            Const WM_NCHITTEST As Integer = &H84 ' Mouse Event
            Const HTCLIENT As Integer = &H1 ' Form Area
            Const HTCAPTION As Integer = &H2 ' Titlebar Area

            ' Call the standard WndProc Method that would have been called anyway

            ' If a Mouse Event happened AND the Event was that the mouse was moved over the Titlebar
            If windowMessage.Msg = WM_NCHITTEST And IntPtr.op_Equality(windowMessage.Result, New IntPtr(HTCAPTION)) Then
                ' Then "Change" the event to think it was in the Form Area
                m.Result = New IntPtr(HTCLIENT)
            End If
        End Sub

    Have a read of this article...Its pretty good.


    Author Comment

    And then I check for the mouse position in the form's onClick event?

    Author Comment

    I haven't had a chance to try to implement it (this particular project is no longer the clients top priority), so I can't say for sure it works, but it sounds reasonable.

    Sorry for the hold up.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 ( But the ability to create custom scanning profiles a…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    857 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now