CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    May 2009
    Posts
    23

    CListCtrl Column-wise dblclick handling

    Hi,

    I am new VC++, iam working on a CListCtrl which has two columns. I need to handle NM_DBLCLICK separately for each of the two columns. The first column contains item names while the second contains their corresponding prices. If the user double clicks the item name column, the items name is appended to an edit box, similarly if the user double clicks the item price its price is appended to the editbox.

    how can this be done?

    thanks in advance

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: CListCtrl Column-wise dblclick handling

    Try to use GetMessagePos to get cursor coordinates and CListCtrl::SubItemHitTest to obtain list control column
    Victor Nijegorodov

  3. #3
    Join Date
    May 2009
    Posts
    23

    Re: CListCtrl Column-wise dblclick handling

    Thanks for the response Victor, heres what the code for the right click looks like:

    Code:
    void MyProject::OnRclickHistList(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    
    	DWORD dwPos = GetMessagePos();
    	CPoint pt( GET_X_LPARAM( dwPos ), GET_Y_LPARAM( dwPos ) ), spt;
    	spt = pt;
    	ScreenToClient( &spt );
    
    	//Check if list is empty
    	if( GetItemCount() == 0 )
    		iMenuType = 0;
    	//list not empty but no item clicked
    	//check if any item was clicked
    	//fill up LVHITTESTINFO and call SubItemHitTest
    	LVHITTESTINFO test;
    	test.pt		= pt;
    	test.flags	= LVHT_ONITEM ;
    
    	int retval	= SubItemHitTest(&test);
    
    	*pResult = 0;
    }
    the trouble is that retval is always -1 regardless of whether i click on any item on the list or outside it.

    Another issue iam facing is that first row of the list control doesn't respond to any Events. I have NM_DBLCLICK and NM_RCLICK message maps, but nothing works on the first row.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: CListCtrl Column-wise dblclick handling

    What message are you trying to handle: NM_DBLCLICK or right mouse click?
    If the latter - then use WM_CONTEXTMENU instead!
    Victor Nijegorodov

  5. #5
    Join Date
    May 2009
    Posts
    23

    Re: CListCtrl Column-wise dblclick handling

    Hi Victor,

    Sorry i got a little mixed up, I have to handle both RClick and DBLClick. I tried ON_WM_CONTEXTMENU for rclick - and got the following error:

    Code:
    error C2440: 'static_cast' : cannot convert from 'void (__thiscall MyProject::* )(CWnd *,CPoint,int)' to 'void (__thiscall CWnd::* )(CWnd *,CPoint)'
            Cast from base to derived requires dynamic_cast or static_cast
    Iam new to this, i've been calling OnContextMenu from the function for handling right clicks, for using WM_CONTEXTMENU...i added ON_WM_CONTEXTMENU to the message map and commented out the code handling the right click except for the OnContextMenu(...) function. Is this the right way? Another thing is that I have derived a class from CListCtrl and am using an object of that class to add a CListCtrl to the dialog at runtime.
    Last edited by austinium; September 28th, 2009 at 09:28 AM. Reason: added details

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: CListCtrl Column-wise dblclick handling

    Quote Originally Posted by austinium View Post
    Hi Victor,

    Sorry i got a little mixed up, I have to handle both RClick and DBLClick. I tried ON_WM_CONTEXTMENU for rclick - and got the following error:

    Code:
    error C2440: 'static_cast' : cannot convert from 'void (__thiscall MyProject::* )(CWnd *,CPoint,int)' to 'void (__thiscall CWnd::* )(CWnd *,CPoint)'
            Cast from base to derived requires dynamic_cast or static_cast
    You should use class Wizard to add message handlers! Don't do it by hand!
    Victor Nijegorodov

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: CListCtrl Column-wise dblclick handling

    Quote Originally Posted by austinium View Post
    Iam new to this, i've been calling OnContextMenu from the function for handling right clicks, ...
    No, it is wrong!
    OnContextMenu handler must be called from the framework, not from your code!
    Victor Nijegorodov

  8. #8
    Join Date
    May 2009
    Posts
    23

    Re: CListCtrl Column-wise dblclick handling

    hi,

    ok ... kinda messed up here haven't i! Thank you so much Victor for helping out

    Heres the situation:
    I have a class that derives CListCtrl (All the event handling goes in here)
    I create an object of this class to create a listview at runtime.
    The list view is a two column setup.
    The user can right click to bring up a context menu (there are different types of these menu, depending on if the user has clicked on empty space in the list or on an item in the list)
    The user can also dbl click to perform separate actions depending on which column he has clicked.
    I cannot use the wizard to add event handling code because the List is created at runtime( it has to be - specification )

    Heres what i have been doing...
    I register a handler for NM_RCLICK and call OnContextMenu from the handler. - this is wrong, i havent been able to get WM_CONTEXTMENU to work.
    for DBL_CLICK, i registered NM_DBLCLICK it works fine, but i need to find a way to differentiate between the items of the two columns of the list. May be i could store items from each list in a CStringArray and use GetMessagePos ,CListCtrl::SubItemHitTest to match from the arrays to figure out which column the item dbl clicked belongs to...i'll try that.

    I am not sure about the right click though, for some strange reason i get an error on using ON_WM_CONTEXTMENU and even if i stick to my method(which is wrong as u pointed out) the very first row doesn't respond to clicks (both rclicks and dblclicks).

    thanks for helping out so far Victor, any ideas about this?

    thanks again

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: CListCtrl Column-wise dblclick handling

    Quote Originally Posted by austinium View Post
    ... I cannot use the wizard to add event handling code because the List is created at runtime( it has to be - specification )
    Why "at runtime"?
    Just create it in resource editor and make it invisible (hide it).
    Then add the control member variamle of your derived from CListCtrl class type.
    Then when you will need "to create" this control - just show it! (CWnd::ShowWindow)
    Victor Nijegorodov

  10. #10
    Join Date
    Feb 2005
    Posts
    2,160

    Re: CListCtrl Column-wise dblclick handling

    You CAN use the wizard. Just locate your derived class in the class wizard, add handler for WM_CONTEXTMENU.

  11. #11
    Join Date
    May 2009
    Posts
    23

    Re: CListCtrl Column-wise dblclick handling

    Thanks for helping out VictorN , hoxsiew. I used SubItemHitTest for extracting item text. based it on code from MSDN's example for the function. I will change to WM_CONTEXTMENU as soon as i get some other issues fixed. for now the current setup works.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured