Solved

weird mouse position from the NMHDR param

Posted on 1998-08-13
8
474 Views
Last Modified: 2008-02-01
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
Comment
Question by:asavard
  • 4
  • 2
  • 2
8 Comments
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
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
 

Author Comment

by:asavard
Comment Utility
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
 

Author Comment

by:asavard
Comment Utility
BTW, NMHDR is defined under win95 too...
0
 

Author Comment

by:asavard
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Expert Comment

by:snoegler
Comment Utility
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
 
LVL 1

Accepted Solution

by:
payn earned 20 total points
Comment Utility
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
 

Author Comment

by:asavard
Comment Utility
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
 
LVL 1

Expert Comment

by:payn
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

728 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