Mouse Over Special Efects

Environment: This was built using Visual C++ 6.0 SP 2.
I used 16-bit color bitmaps, so if your resolution is set to 256 colors, it will probably not look very pleasing.

This is an updated version to my original posting. I have taken the comments that I received and have come up with a better version of the code. My thanks to Anatoly Ivasyuk, Jacques, and Dave Montgomery for their comments.

I needed a way to change a bitmap when the mouse went over it, and also to detect if the user pressed the mouse button. What I came up with was the following quick and easy solution.

void CMouseTrackDlg::OnMouseMove(UINT nFlags, CPoint point) 

	if (rect.PtInRect(point))

	CDialog::OnMouseMove(nFlags, point);
All I did here was check to see if the mouse had entered into the bitmap. If it had, a different bitmap is displayed.

void CMouseTrackDlg::OnLButtonUp(UINT nFlags, CPoint point) 

		AfxMessageBox("You pressed the Visual C++ bitmap", MB_OK);
	CDialog::OnLButtonUp(nFlags, point);
Here I just wanted to check if the user pressed the left mouse button while inside the bitmap.

Make sure to load the bitmaps:

BOOL CMouseTrackDlg::OnInitDialog()

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	// TODO: Add extra initialization here
	return TRUE;  // return TRUE  unless you set the focus to a control
And make sure to initially declare the bitmaps, in this case there are only two. In addition to the bitmaps, you can see I declared a CRect which is used in the code above.

	CRect rect;
	CBitmap bitmap1;
	CBitmap bitmap2;

void CMouseTrackDlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) 
	CDialog::OnActivate(nState, pWndOther, bMinimized);
	if(nState == WA_INACTIVE)
Here I mapped the OnActivate message to the dialog (CMouseTrackDlg) and if the dialog has lost its focus, then the orginal bitmap is displayed.

Download demo project - 13 KB

Download source - 21.7 KB


  • This is not so good...

    Posted by turkinz on 05/08/2004 08:57am

    The common problem with this and similar examples is that if you move your mouse over the button, and then pretty fast move it off the button and window area, the button still remains highlighted (as if there was the mouse pointer over it, but it's not). Look at Internet Explorer web pages. It's impossible to catch that kind of bug, there. I think they used some kind of Timer-based checking.

  • Doing this to more that one image

    Posted by Legacy on 01/20/2000 12:00am

    Originally posted by: Hades

    Is there a way to use this code on multiple images, so you can have two buttons that change?


    Posted by Legacy on 04/07/1999 12:00am

    Originally posted by: Jacques

    you can have a look at WM_MOUSELEAVE

    if you are under Win NT 4 or Win98

    The WM_MOUSELEAVE message is posted to a window when the cursor leaves the client area of the window specified in a prior call to TrackMouseEvent.

    lParam = 0;
    wParam = 0;

    Return Values
    If an application processes this message, it should return zero.

    All tracking requested by TrackMouseEvent is canceled when this message is generated. The application must call TrackMouseEvent when the mouse re-enters its window if it requires further tracking of mouse hover behavior.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's enterprise datacenter can be one of the most complex business environments with dozens (for smaller business), hundreds (for larger and midsize business), even thousands (for hyperscale businesses) of servers that must be managed and monitored. At this level, just managing the cords can be challenging — let alone keeping up with the growing need for more agility and scalability within the datacenter. Simply put, companies are aggressively looking for less complexity and more agility from their …

  • Featuring Art Schoeller, VP and Principal Analyst, at Forrester Research Live Event Date: November 9, 2016 @ 10 AM PT / 1 PM ET Being "proactive" with your customers is not enough to survive in the market today. The truth is, organizations that understand how to engage customers on their preferred channels will create contextual, and relevant experiences for customers. Plus, they will see the financial impact of nurturing long-term customer loyalty. Join our guest speaker Art Schoeller, VP and Principal …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date