Mouse Over Special Efects | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
May 1, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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)
{
	m_Picture.GetWindowRect(&rect);
	ClientToScreen(&point);
	if (rect.PtInRect(point))
	{
		m_Picture.SetBitmap(bitmap2);
	}
	else
	{
		m_Picture.SetBitmap(bitmap1);
	}
	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)
{
	m_Picture.GetWindowRect(&rect);
	ClientToScreen(&point);
	if(rect.PtInRect(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()
{
	CDialog::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
	bitmap1.LoadBitmap(IDB_BITMAP1);
	bitmap2.LoadBitmap(IDB_BITMAP2);
	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.


private:
	CRect rect;
	CBitmap bitmap1;
	CBitmap bitmap2;

Finally:

void CMouseTrackDlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
	CDialog::OnActivate(nState, pWndOther, bMinimized);
	if(nState == WA_INACTIVE)
		m_Picture.SetBitmap(bitmap1);
}

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.