Solved

Detecting dialog controls from point

Posted on 1998-09-27
26
432 Views
Last Modified: 2013-12-03
I need to obtain dialog control handle from screen coordinates to display context help for it.

When user right-clicks on dialog item, I process WM_CONTEXTHELP. I have cursor coordinates of clicked area.
Next, I try to detect which item has been clicked.

I call WindowFromPoint. It returns right handles, EXCEPT for static and groupbox controls - in this case it returns dialog handle.
I try to call ChildWindowFromPoint. It returns only groupbox handles even if user clicks on item inside of groupbox (eg, button, static control, etc).
I try to call ChildWindowFromPointEx with all flags - SKIPTRANSPARENT, SKIPDISABLED - it produces the same result.

I cannot use the combination of WindowFromPoint and ChildFromPoint because I cannot obtain static control handle.

I don't want to use WinHelp(... CONTEXTHELP..).

Is there any way to solve this problem except of enumerating all childs, retrieving its coordinates...?
0
Comment
Question by:NickRepin
  • 9
  • 6
  • 4
  • +4
26 Comments
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
Try to check manually if the mouse is in a child control:

// 'pntMouse' should contain the screen coordinates of the mouse pointer
RECT rcItem;
HWND hwndCur=GetWindow(hDlg,GW_CHILD);

for(;;) {
  if(hwndCur==NULL) break;
  GetWindowRect(hwndCur,&rcItem);
  if(PtInRect(&rcItem,pntMouse)==TRUE) break;
  hwndCur=GetWindow(hwndCur,GW_HWNDNEXT);
}

// hwndCur==NULL: no hit, hwndCur!=NULL -> dialog control with mouse in it

Hope this helped :)
0
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
Why not WindowFromPoint or ChildWindowFromPoint API/CWnd-members

These don't require a loop
0
 
LVL 15

Author Comment

by:NickRepin
Comment Utility
snoegler, I'm sorry, but I should reject your answer. And that is why:
1) 'except of enumerating all childs, retrieving its coordinates' - from my question, see above
2) The loop will not so simple because of the group boxes will be retrieved before controls inside this groupboxes. And your loop will always return group box handles, not handles of controls the user really clicked on. Again, I can write this loop but I looking for more easy and fast solution.

answers2000, please, see full text of my question. Window/ChildFromPoint doesn't work.
0
 
LVL 15

Author Comment

by:NickRepin
Comment Utility
To retrive correct results, the loop must be like following:

child=GetWindow( GW_CHILD)
child=GetWindow(child,GW_LAST) // Backward direction
while(child) {
  ...
  child=GetWindow(child,GW_PREV)
}

ChildWindowFromPoint uses 'forward direction' loop.
But what about strange behavior of WindowFromPoint?
0
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
>> answers2000, please, see full text of my question. Window/ChildFromPoint doesn't work.
sorry about that, I read the Q and then go so absorted in snoeggler's efforts, I forgot half the Q!

WindowFromPoint is documented as ignoring certain windows
(begin quote)
WindowFromPoint does not retrieve a hidden, disabled, or transparent window, even if the point is within the window. An application should use the ChildWindowFromPoint member function for a nonrestrictive search.
(end quote)

Dialog's seem to have special knowledge of statics (and for all I know group boxes, quite likely given their effect on tabbing) which is probably why it screws up.  I am fairly sure Windows dialog manager's dialog class takes over painting the statics (not the static controls themselves) at least to some extent so this is probably something to do with it.

>> ChildWindowFromPoint uses 'forward direction' loop.
You are right it is documented as doing so

0
 
LVL 15

Author Comment

by:NickRepin
Comment Utility
I think only MS knows the answer.
Groupboxes and statics are not disabled,invisible or transparent windows. And WindowFromPoint should not ignore them.

As to painting, you are right,
groupbox paints its border, but doesn't paint background. Nevertheless, its not transparent, and WindowFromPoint should return its handle. It seems it's one more Windows bug.

The only solution is a loop with backward child enumeration.
0
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
>> Groupboxes and statics are not disabled,invisible or transparent windows.

Aren't they transparent ?  The parent window determines the color (WM_CTLCOLOR messages)

0
 
LVL 2

Expert Comment

by:duneram
Comment Utility
side question....   Are the statics and group boxes in question happening to use -1 for their control id?

if so, give them a real value instead of -1  like 109, 110, etc
0
 
LVL 15

Author Comment

by:NickRepin
Comment Utility
I think the transparent window is the one which has EX_TRANSPARENT style. WM_CTLCOLOR also sends to buttons, listboxes, etc, but their handles WindowFromPoint returns.

duneram, all my controls have different real values.
0
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
So would I, but there is definitely something funny in the dialog manager

as you say only MS knows the answer
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
Comment Utility
Hi, it is well-nown problem in Win31/Win95! There is no way
to solve its problem, exept numerate all windows,
get all Rect and other, as you write in Comment.
Some times ago we test all other ways, but ....
I don't now is it reply for you   question, but no other!
Alex
0
 
LVL 15

Author Comment

by:NickRepin
Comment Utility
AlexVirochovsky,
you made me to make hard decision. Formally you are right, it's the answer. But it's unfairly to Answers2000 and snoegler to give you points. This answer was mentioned before in my and Answers2000's comments.
Sorry.
0
 
LVL 8

Expert Comment

by:Answers2000
Comment Utility
Nick - reading you last comment I'm not sure how you'd like to procede

Would you like to give (a) snoegler the pts, or (b) me the pts ?  or (c) keep the Q open




0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 15

Author Comment

by:NickRepin
Comment Utility
Answers200, I think the keeping the Q opened is useless.
Answer it, and I'll give you 350 pts.
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 350 total points
Comment Utility
>> Answer it, and I'll give you 350 pts.

OK, thanks, see ya round
0
 
LVL 2

Expert Comment

by:DarrinE
Comment Utility
Am I wrong or is that I just wasted 35 points for an absolutely useless answer - what a joke

The answer to this question is around - why ? Because SPY++ and a number of other spy type programs actually do exactly what NickRepin was (and I am)  looking for

Nick did you ever find a solution ?
0
 
LVL 15

Author Comment

by:NickRepin
Comment Utility
I looked for solution for my particular case (context help in the dialog box) and I already knew about the solution when I asked the question. I asked it just to be sure.
The only answer is that you have to enumerate all windows yourself.

Here is my function which works fine.

HWND FindDialogControl(HWND dlg,POINT p)
{
   HWND w=GetWindow(dlg,GW_CHILD);
   if(w) {
      w=GetWindow(w,GW_HWNDLAST);
      while(w) {
         if(IsWindowVisible(w)) {
            RECT r;
            GetWindowRect(w,&r);
            if(InRect(&r,p))
               return w;
         }
         w=GetWindow(w,GW_HWNDPREV);
      }
   }
   return 0;
}

But if you want to do what spies do,
well, I think that you have to call WindowFromPoint() first, then recursively enumerate all childs, childs of childs etc using the function above. The most grand-grand...-grandchild containing the point is the right window.
0
 
LVL 2

Expert Comment

by:duneram
Comment Utility
Darrin,

Since the asker only gave a 'D' grade, that should have clued you in on the quality of the answer...

But thats the way life gets thrown at you...

If you want to see something weird check out http://www.duneram.com/cam/index.html (that's live!)
0
 
LVL 2

Expert Comment

by:DarrinE
Comment Utility
Nick - thanks for the feed back - is

InRect(&r,p)

a helper function of your own ? Can I see it (please)
               

MTIA


Duneram - nice picture - is there any resemblance between the flash and you ?

DarrinE
0
 
LVL 2

Expert Comment

by:duneram
Comment Utility
LOL.... now thats a good laugh...
0
 
LVL 5

Expert Comment

by:ianB
Comment Utility
Hi,

Unless anyone has any objections, I am going to remove this PAQ as it is not a good question to have in the archives since it has no answer.

Speak up if you dont agree.

Ian
Community Support @ Experts Exchange
0
 
LVL 2

Expert Comment

by:duneram
Comment Utility
Since there is a code solution in it, just make it a free PAQ
0
 
LVL 5

Expert Comment

by:ianB
Comment Utility
Good Idea.

Ian
0
 
LVL 15

Author Comment

by:NickRepin
Comment Utility
inline bool InRect(const RECT* r,int x,int y)
{ return x>=r->left && x<r->right && y>=r->top && y<r->bottom; }

inline bool InRect(const RECT* r,POINT p)
{ return p.x>=r->left && p.x<r->right && p.y>=r->top && p.y<r->bottom; }

inline bool InRect(const RECT* r,LPARAM l)
{ return LOWORD(l)>=r->left && LOWORD(l)<r->right && HIWORD(l)>=r->top && HIWORD(l)<r->bottom; }


0
 
LVL 2

Expert Comment

by:DarrinE
Comment Utility
thanks Nick - have a good one (Eatser Egg that is <s>)
0
 
LVL 15

Author Comment

by:NickRepin
Comment Utility
Thanks, you too.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

762 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

13 Experts available now in Live!

Get 1:1 Help Now