Solved

Need some help with mouse over, change color

Posted on 2008-09-30
4
268 Views
Last Modified: 2013-11-20
Hello,
I would like to change the color of a static text (like a hyperlink) when I go with my mouse over the
text.
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,
Thomas


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);

 

	pBtn->GetWindowRect(rcBtn);

    GetCursorPos(&ptCursor);

 

	pBtn1->GetWindowRect(rcBtn1);

	GetCursorPos(&ptCursor1);

 

	pBtn2->GetWindowRect(rcBtn2);

	GetCursorPos(&ptCursor1);

 

 

    //===========================================================================

	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

		SetCursor(my_new_cursor);

		 bReturn = TRUE;

 

	}

   	else

	{

		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))

      {

		  pDC->SetTextColor(clr_blue); 		   

	  }

      //---------------------------------------------------------------------------------

  

 

 

 

	  return hbr;

  }

  //###############################################################################

Open in new window

0
Comment
Question by:tsp2002
  • 2
4 Comments
 
LVL 19

Expert Comment

by:alb66
Comment Utility
This is a nice sample (it is for a button, but you can apply the same concept to a static control):
http://www.codeproject.com/KB/buttons/hoverbutton.aspx

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

Expert Comment

by:LordOfPorts
Comment Utility
This article might also be useful http://www.sahirshah.com/cpp/mouseover/MouseOver.html
0
 
LVL 19

Accepted Solution

by:
LordOfPorts earned 500 total points
Comment Utility
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;
m_idc_internet.Invalidate();
m_idc_email.Invalidate();

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

m_bMouseOver = FALSE;
m_idc_internet.Invalidate();
m_idc_email.Invalidate();


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

Alternatively you could e.g. use the ColorStatic control from the project at http://www.codeproject.com/KB/static/coloredit_colorstatic.aspx 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

0
 

Author Closing Comment

by:tsp2002
Comment Utility
Thank you. That was easy and the code I was looking for, thanks a lot
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
GIF file bit manipulation for color change 5 104
dog bark java program 15 81
fix34  challenge 9 95
Problem to event 3 49
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: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

10 Experts available now in Live!

Get 1:1 Help Now