Solved

weird mouse position from the NMHDR param

Posted on 1998-08-13
8
483 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
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 20 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 73
how to convert c++ code to Android App 3 109
Compile GLUT with Visual Studio 2015 1 175
Gaming Software 1 19
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

830 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