Need some help with mouse over, change color

Posted on 2008-09-30
Last Modified: 2013-11-20
I would like to change the color of a static text (like a hyperlink) when I go with my mouse over the
I already change the cursor to a hand-cursor when my mouse is over the static text.
Please let me know. 500 points
Best regards,

BOOL COffenePostenDialog_Detail::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
	BOOL bReturn;
	CRect rcBtn;
	CRect rcBtn1;
	CRect rcBtn2;
	CPoint ptCursor;
	CPoint ptCursor1;
	CPoint ptCursor2;
    // Calculate the current cursor position
	// Change cursor if we are over Internet and Email link
	CWnd* pBtn  = GetDlgItem(IDC_STATIC_INTERNET);
	CWnd* pBtn1 = GetDlgItem(IDC_STATIC_EMAIL);
	CWnd* pBtn2 = GetDlgItem(IDC_STATIC_TELEFON);
	if(rcBtn.PtInRect(ptCursor)==TRUE ||rcBtn1.PtInRect(ptCursor1)==TRUE||rcBtn2.PtInRect(ptCursor1)==TRUE)
		CWinApp* pApp = AfxGetApp();
		HCURSOR my_new_cursor = pApp->LoadCursor(IDC_CURSOR2); // load new cursor
		 bReturn = TRUE;
		bReturn = CDialog::OnSetCursor(pWnd, nHitTest, message);// default cursor
	return bReturn;
	return CDialog::OnSetCursor(pWnd, nHitTest, message);
HBRUSH COffenePostenDialog_Detail::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
	   HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
       //--------------------------- Link Color Internet/ Email ------------------------------
	  if((pWnd->GetDlgCtrlID()==IDC_STATIC_INTERNET) ||(pWnd->GetDlgCtrlID()==IDC_STATIC_EMAIL))
	  return hbr;

Open in new window

Question by:tsp2002
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 19

Expert Comment

ID: 22604470
This is a nice sample (it is for a button, but you can apply the same concept to a static control):

NOTE: may be you need to change CHoverButton::OnMouseHover from
void CHoverButton::OnMouseHover(WPARAM wparam, LPARAM lparam)
LRESULT CHoverButton::OnMouseHover(WPARAM wparam, LPARAM lparam)
and return 0 from it
LVL 19

Expert Comment

ID: 22609534
This article might also be useful
LVL 19

Accepted Solution

LordOfPorts earned 500 total points
ID: 22619358
Thomas, since you already are able to detect when the mouse is over your CStatic control here is some additional information and an approach I tested successfully:

1. Add a private BOOL variable to your CDialog derived class and name it "m_bMouseOver".

2. Associate control variables with your CStatic controls, you can do so by right-clicking on the static control and selecting "Add Variable" and name them e.g. "m_idc_internet" and "m_idc_email".

3. In your OnSetCursor method add the following code when you detect the on mouse over event in the if statement:

// Trigger a call to OnCtlColor
m_bMouseOver = TRUE;

and in the else case, i.e. mouse out event:

m_bMouseOver = FALSE;

4. Use the OnCtlColor code in the code snippet below.

Alternatively you could e.g. use the ColorStatic control from the project at as it provides a simple member method for changing the text color and might be easier to implement.
HBRUSH COffenePostenDialog_Detail::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	//------- Link Color Internet/ Email ------------------------------
	if(pWnd->GetDlgCtrlID() == IDC_STATIC_INTERNET || pWnd->GetDlgCtrlID() == IDC_STATIC_EMAIL) {
		if(m_bMouseOver == TRUE) {
			pDC->SetTextColor(RGB(0, 0, 255)); // Blue link color
		else {
			pDC->SetTextColor(RGB(1, 1, 1)); // Black text color
	return hbr;

Open in new window


Author Closing Comment

ID: 31501511
Thank you. That was easy and the code I was looking for, thanks a lot

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

735 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