• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 501
  • Last Modified:

weird mouse position from the NMHDR param

Hi!

I have the following function :

void CEditorExeDlg::OnItemexpandingSectionTree(NMHDR* pNMHDR, LRESULT* pResult)
{
    NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
            
   POINT ptExpand = pNMTreeView->ptDrag;

   CTreeCtrl *pTree = (CTreeCtrl *) GetDlgItem(IDC_SECTION_TREE);

   // Make a HitTest to get the current expanding item
   TV_HITTESTINFO HitTestInfo;
   HitTestInfo.pt = ptExpand;
   HTREEITEM hTest = pTree->HitTest(&HitTestInfo);
...

when I debug the app, the coordinates in the pNMTreeView->ptDrag have no sense at all.  I need this to get the item that was clicked in an tree list.  upon expanding the upper left element of the list, the value of the point is something like (1234567, 12345678) (some huge values).  Of course, after the hittest, the flag is set to below & right.

What may be the cause of this problem?
0
asavard
Asked:
asavard
  • 4
  • 2
  • 2
1 Solution
 
snoeglerCommented:
In Windows95 the NMHDR structure is according to my documentation not
defined(NT only).
So you have to fetch the mouse coordinates using either:
- GetCursorPos(), which gives the *actual* coordinates of the mouse; but i think
  the time difference between message and your function should be small enough so
  that this position should be valid,
- or GetMessagePos() which returns the mouse position when the last message was
  sent(see docs), but i am not sure if this function works with common control notification
  messages(i think so though).
In each case you have to use ScreenToClient() as the coordinates returned are both
screen coordinates.
0
 
asavardAuthor Commented:
n Windows95 the NMHDR structure is according to my documentation not
     defined(NT only).
I'm under NT so it should work...

     So you have to fetch the mouse coordinates using either:
     - GetCursorPos(), which gives the *actual* coordinates of the mouse; but i think
       the time difference between message and your function should be small enough so
       that this position should be valid,
     - or GetMessagePos() which returns the mouse position when the last message was
       sent(see docs), but i am not sure if this function works with common control notification
       messages(i think so though).
     In each case you have to use ScreenToClient() as the coordinates returned are both
     screen coordinates.

I tried this method, and although values returned make sense, HitTest doesn't return the handle to the right item (I don't know why)  There should be a way to get the coordinates
 from NM_TREEVIEW structure, why can it be there if it's useless!
0
 
asavardAuthor Commented:
BTW, NMHDR is defined under win95 too...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
asavardAuthor Commented:
I made it work.  I called ScreenToClient for the window, instead of calling it like this :
pTree->ScreenToClient.  So the conversion was relative to the corner of the dialog, instead of being relative to the tree screen...

snoegler, make a dummy answer...
0
 
snoeglerCommented:
NMHDR is defined, but it all NMHDR derivates (NM_TREEVIEW) have in my documentation
the comment that the values are not valid using Windows 95(MSVC 4.0)
Thank you for your offer ... But it seems like you solved your problem yourself :)
0
 
paynCommented:
You're going about this wrong. The NM_TREEVIEW structure includes a TV_ITEM structure that tells you which item is expanding. (Actually, it includes two TV_ITEM structures; the one you want is itemNew).

Try this in place of your code:

    NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
    HTREEITEM hTest = pNMTreeView->itemNew.hItem;

That's all you have to do.

I don't know why you're getting garbage coordinates; presumably Windows doesn't think the point is relevant for this notification message, so it saves a miniscule amount of time by not copying it for you?

The solution above with GetCursorPos will work most of the time, but not all of the time. If the mouse has moved far enough between the sending of the message and the call to GetCursorPos, you'll get the wrong data. This is not only possible, it's almost guaranteed to happen while you're debugging, which will get annoying very fast...

0
 
asavardAuthor Commented:
Thanks payn.  It does work.  That's the kind of method I was looking for without success, thanks to the poor documentation of MS...  This help file really sucks!  I was aware of the itemNew element, but didn't knew how to interpret it.  Do you have any good sites about MFC, I like codeguru, but I miss some information...

Bye!
0
 
paynCommented:
The on-line documentation is not only incomplete, it's often wrong, especially when dealing with less commonplace subjects.

After spending years looking for good websites, books, and other information on MFC, I realized that the only way I was going to figure out how things really work was by looking through Microsoft's code. For example, you can learn lots of great stuff about how owner-draw listboxes really work, and how you should use some of the more obscure features, by looking at the CCheckListBox class, which is implemented as an owner-draw listbox. Also, look through the sample code that comes with VC--many times they use a workaround to get past limitations or bugs in MFC or VC or Windows that's not documented anywhere else.

The most useful thing I've found on the Net, besides codeguru, is the MFC FAQ, which I think is maintained by Stingray Software.

I would also suggest buying the book MFC Internals, by Scot Wingo and someone else. None of the books I've seen dedicated to teaching you how to use MFC seems to answer any of the questions I've ever asked, but MFC Internals answered a lot of the "how do I do this" questions on the way to explaining the "how does it work" questions...

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now