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
Solved

Need some help with CListCtrl and SetCursor

Posted on 2008-10-13
10
555 Views
Last Modified: 2013-11-20
Hello,
I would like to change the cursor when the mouse is over a cell in my ClistCtrl.
I have enclosed the code when I am with the mouse over a Static Item.
How can I get the row and col into a Crect, to calculate the position of the cusor?
Please let me know. 500 points with a solution.
Please consider I am a beginner in MFC.
Thanks.
Best regards,
Thomas

//#########################################################################
BOOL CMyProgram::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
	BOOL bReturn;
	
	CRect rcBtn;
	CPoint ptCursor;
	
 
      // Calculate the current cursor position
	//-----------------------------------
	CWnd* pBtn  = GetDlgItem(IDC_STATIC_INTERNET);
	
	pBtn->GetWindowRect(rcBtn);
      GetCursorPos(&ptCursor);
 
	 
      //-------------------------------------------------------------------------------
	if((rcBtn.PtInRect(ptCursor)==TRUE ))//Internet
	{
		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);
}
 
//#########################################################################
 
void CMyProgram::OnNMClickList1(NMHDR *pNMHDR, LRESULT *pResult)
{
    //------------------------------------------------
	LPNMITEMACTIVATE pNMIA = (LPNMITEMACTIVATE)pNMHDR;
 
    int row = pNMIA->iItem;
    int col =  pNMIA->iSubItem;
    //------------------------------------------------
.
.
.

Open in new window

0
Comment
Question by:tsp2002
  • 6
  • 4
10 Comments
 
LVL 19

Expert Comment

by:alb66
ID: 22701079
You can use SubItemHitTest().

Here is an example:
int CListViewEx::PointToSubItem( CPoint pt )
{
	CListCtrl& lc = GetListCtrl();
 
	LVHITTESTINFO  hti;
	hti.pt = pt;
	lc.ScreenToClient( &hti.pt );
 
	if ( lc.SubItemHitTest( &hti ) >= 0 )
		return hti.iSubItem;
 
	return -1;
}

Open in new window

0
 
LVL 19

Expert Comment

by:alb66
ID: 22701087
0
 

Author Comment

by:tsp2002
ID: 22701847
how and where should I call this function???
Please let me know, thanks
Here is an example:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
 int CListViewEx::PointToSubItem( CPoint pt )
{
	CListCtrl& lc = GetListCtrl();
 
	LVHITTESTINFO  hti;
	hti.pt = pt;
	lc.ScreenToClient( &hti.pt );
 
	if ( lc.SubItemHitTest( &hti ) >= 0 )
		return hti.iSubItem;
 
	return -1;
} 

Open in new window

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 19

Expert Comment

by:alb66
ID: 22702004
You can add a handler for WM_MOUSEMOVE message and call SubItemHitTest from there.

0
 

Author Comment

by:tsp2002
ID: 22702264
Can you give me an example? Sorry beginner wiht this
void CMyProgram::OnMouseMove(UINT nFlags, CPoint point)
 {
	 CDialog::OnMouseMove(nFlags, point);
 }

Open in new window

0
 
LVL 19

Assisted Solution

by:alb66
alb66 earned 500 total points
ID: 22702468
It should be something like that:
void CMyProgram::OnMouseMove(UINT nFlags, CPoint point)
{
   CDialog::OnMouseMove(nFlags, point);
 
   // convert from dialog coordinate to screen coordinate
   ClientToScreen( &point );
 
   // get the list control
   CListCtrl* pList = GetDlgItem( IDC_LISTCONTROL );
 
   // convert from screen coordinate to control coordinate
   pList->ScreenToClient( &point );
 
   LVHITTESTINFO  hti;
   hti.pt = point;
   switch( pList->SubItemHitTest( &hti ))
   {
     case LVHT_ONITEMICON:
     case LVHT_ONITEMLABEL:
     {
       // you are over an item
       int nRow = hti.iItem;
       int nCol = hti.iSubItem;
 
       ... do something ....
     }
     break;
   }
 
 
   
}

Open in new window

0
 

Author Comment

by:tsp2002
ID: 22709604

Will not work.
When I put here a break point the program do not stop

CString str=m_List.GetItemText(nRow,nCol);

also I have to change you code to this, otherwise I get an error

 CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LIST1);
Please help, thanks

void CKontaktpersonen::OnMouseMove(UINT nFlags, CPoint point)
 {
	 CDialog::OnMouseMove(nFlags, point);
 
   // convert from dialog coordinate to screen coordinate
   ClientToScreen( &point );
 
   // get the list control
   CListCtrl *pList = (CListCtrl*)GetDlgItem(IDC_LIST1);
 
   // convert from screen coordinate to control coordinate
   pList->ScreenToClient( &point );
 
   LVHITTESTINFO  hti;
   hti.pt = point;
   switch( pList->SubItemHitTest( &hti ))
   {
     case LVHT_ONITEMICON:
     case LVHT_ONITEMLABEL:
     {
       // you are over an item
       int nRow = hti.iItem;
       int nCol = hti.iSubItem;
 
	   
 
	   CString str=m_List.GetItemText(nRow,nCol);
       
     }
     break;
   }
 
 
	 CDialog::OnMouseMove(nFlags, point);
 }

Open in new window

0
 

Author Comment

by:tsp2002
ID: 22713621
please help....I don´t know what to do, thanks
0
 

Accepted Solution

by:
tsp2002 earned 0 total points
ID: 22718556
when I change the code and put a stop button there
the program stops when I am over the dialog and not over
the CListCtrl..........please help

aaa.BMP
0
 

Author Comment

by:tsp2002
ID: 22730433
sorry... I think I have closed this one wrong. I would like to give the points....even it is not working. I found the solution with a other question. Thank you
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pressing cancel button with C# UI Automation 3 80
pre4 challenge 19 103
changeXy challenge 13 87
Change to event 1 117
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.

791 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