?
Solved

weird mouse position from the NMHDR param

Posted on 1998-08-13
8
Medium Priority
?
496 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
[X]
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
  • 4
  • 2
  • 2
8 Comments
 
LVL 6

Expert Comment

by:snoegler
ID: 1170244
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
ID: 1170245
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
ID: 1170246
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.

 

Author Comment

by:asavard
ID: 1170247
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
 
LVL 6

Expert Comment

by:snoegler
ID: 1170248
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 80 total points
ID: 1170249
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
ID: 1170250
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
ID: 1170251
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

Industry Leaders: 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!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

777 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