NO

Author Topic: Geting ChildWindowFromPoint over Edit Control is driving me NUTS !  (Read 11488 times)

EdPellesC99

  • Guest


  Why if it works for the Static control, can't I get it to work for the Edit Control !

   What am I missing (besides a brain) ?

   I modified an old example program to demo my problem.
   Code:
Quote



HINSTANCE hInst;   // current instance

LPCTSTR lpszAppName  = "MyApp";
LPCTSTR lpszTitle    = "My Application";

LRESULT CALLBACK WndProc  (HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
             LPTSTR lpCmdLine, int nCmdShow)
{
   MSG     msg;
   HWND    hWnd;
   WNDCLASSEX wc;

   wc.style      = CS_HREDRAW | CS_VREDRAW;
   wc.lpfnWndProc   = (WNDPROC)WndProc;   
   wc.cbClsExtra    = 0;            
   wc.cbWndExtra    = 0;            
   wc.hInstance     = hInstance;       
   wc.hIcon      = LoadIcon( hInstance, lpszAppName );
   wc.hCursor    = LoadCursor(NULL, IDC_ARROW);
   wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
   wc.lpszMenuName  = lpszAppName;       
   wc.lpszClassName = lpszAppName;       
   wc.cbSize     = sizeof(WNDCLASSEX);
   wc.hIconSm    = LoadImage( hInstance, lpszAppName,
                  IMAGE_ICON, 16, 16,
                  LR_DEFAULTCOLOR );

   if ( !RegisterClassEx( &wc ) )
   return( FALSE );

   hInst = hInstance;

   hWnd = CreateWindow( lpszAppName,
            lpszTitle,   
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, 0,
            CW_USEDEFAULT, 0, 
            NULL,       
            NULL,       
            hInstance,      
            NULL         
             );

   if ( !hWnd )
   return( FALSE );

   ShowWindow( hWnd, nCmdShow );
   UpdateWindow( hWnd );      

   while( GetMessage( &msg, NULL, 0, 0) )   
   {
   TranslateMessage( &msg );
   DispatchMessage( &msg )
   }

   return( msg.wParam );
}


LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
static HWND hStatic = NULL;
static HWND hEdit = NULL;

   switch( uMsg )
   {
   case WM_CREATE :
         hStatic = CreateWindow( "STATIC", "Here is Static",
                     WS_CHILD | WS_BORDER |
                     WS_VISIBLE | SS_LEFT,
                     10, 10, 100, 30,
                     hWnd, (HMENU) 101, 
                     hInst,
                   NULL );

         hEdit = CreateWindow( "EDIT", "Here is Edit",
                     WS_CHILD | WS_BORDER |
                     WS_VISIBLE | SS_LEFT,
                     10, 75, 100, 30,
                     hWnd, (HMENU) 102, 
                     hInst,
                   NULL );

         break;

   case WM_LBUTTONDOWN :
      {
         POINT pt;
         HWND  hwndChild;

         pt.x = LOWORD( lParam );
         pt.y = HIWORD( lParam );

         hwndChild = ChildWindowFromPoint( hWnd, pt );

         if ( hwndChild == hStatic )
            MessageBox( hWnd, "static control!", "Hit", MB_OK );
         if ( hwndChild == hEdit)
            MessageBox( hWnd, "Edit control!", "Hit", MB_OK );
         else
            MessageBox( hWnd, "client area!", "Hit", MB_OK );
      }
      break;

   
   case WM_DESTROY :
        PostQuitMessage(0);
        break;

   default :
      return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
   }

   return( 0 );
}




Thanks in advance for any leads/pointers to a brain.


CommonTater

  • Guest
Re: Geting ChildWindowFromPoint over Edit Control is driving me NUTS !
« Reply #1 on: February 17, 2011, 07:01:11 PM »
Ummmm... because the edit control is absorbing the message for it's own uses, but the static control has no need of it so it passes it along to your windproc.

The general message flow is.. control -> mainwind -> defwindowproc  If the control returns 0 for that message it's saying "no more processing" and the main wind never sees it.

EdPellesC99

  • Guest
Re: Geting ChildWindowFromPoint over Edit Control is driving me NUTS !
« Reply #2 on: February 18, 2011, 03:50:45 AM »

  Yes,

   Here:
http://us.generation-nt.com/answer/notify-handler-nm-keydown-help-11398932.html#r

   I justfound this response to essentially my question:

An edit control doesn't give rise to WM_NOTIFY messages (NM_KEYDOWN).
If you want to handle the key presses in an edit control, sub-class
the edit control and handle WM_KEYDOWN in your function.

 


   Thanks I guess short of subclassing, I am out of luck.
   Life isn't fair.  :'(.......... Thanks






CommonTater

  • Guest
Re: Geting ChildWindowFromPoint over Edit Control is driving me NUTS !
« Reply #3 on: February 18, 2011, 05:32:07 AM »
   Thanks I guess short of subclassing, I am out of luck.
   Life isn't fair.  :'(.......... Thanks

Subclassing a window is no big deal... the SDK gives a pretty good description, just follow the steps...

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Geting ChildWindowFromPoint over Edit Control is driving me NUTS !
« Reply #4 on: February 18, 2011, 08:41:01 AM »
read this. http://msdn.microsoft.com/en-us/library/ms633570(v=vs.85).aspx
Example:
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define IDC_EDIT 4001

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT APIENTRY EditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

WNDPROC wpOrigEditProc;

char *szAppName = "WinFrame";
char *szFrameClass = "cWinFrame";
HWND hFrame;
HANDLE hInst;

HWND hEdit;

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wcx;
MSG msg;

wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.lpfnWndProc = (WNDPROC) WndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hInstance;
wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground= (HBRUSH)COLOR_APPWORKSPACE+1;
wcx.lpszMenuName = NULL;
wcx.lpszClassName= szFrameClass;
wcx.hIconSm = 0;

if (!RegisterClassEx(&wcx))
return 0;
hInst = hInstance;

hFrame = CreateWindowEx(0, szFrameClass, szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInst, NULL);
if(!hFrame)
return 0;
ShowWindow(hFrame, nCmdShow);
UpdateWindow(hFrame);

while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
switch(wMsg) {

case WM_CREATE:
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "Edit test",
WS_CHILD | WS_VISIBLE |
WS_HSCROLL | WS_VSCROLL |
ES_MULTILINE | ES_NOHIDESEL |
ES_AUTOHSCROLL | ES_AUTOVSCROLL,
0, 0, 100, 100, hWnd, (HMENU)IDC_EDIT, hInst, NULL);
SendMessage(hEdit, WM_SETFONT, (UINT)GetStockObject(DEFAULT_GUI_FONT), 0L);
SetFocus(hEdit);
// Subclass the edit control.
wpOrigEditProc = (WNDPROC) SetWindowLongPtr(hEdit, GWLP_WNDPROC, (LONG) EditSubclassProc);
return 0;

case WM_DESTROY:
// Remove the subclass from the edit control.
SetWindowLong(hEdit, GWL_WNDPROC, (LONG) wpOrigEditProc);
PostQuitMessage(0);
return 0;

default:
return DefWindowProc(hWnd, wMsg, wParam, lParam);
}
}

// Subclass procedure
LRESULT APIENTRY EditSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_GETDLGCODE)
return DLGC_WANTALLKEYS;
if (uMsg == WM_KEYDOWN) SetWindowText(hFrame, "WM_KEYDOWN");
if (uMsg == WM_KEYUP) SetWindowText(hFrame, "");
if (uMsg == WM_LBUTTONDOWN) SetWindowText(hFrame, "WM_LBUTTONDOWN");
if (uMsg == WM_LBUTTONUP) SetWindowText(hFrame, "");
if (uMsg == WM_LBUTTONDBLCLK) SetWindowText(hFrame, "WM_LBUTTONDBLCLK");


return CallWindowProc(wpOrigEditProc, hwnd, uMsg, wParam, lParam);
}
« Last Edit: February 18, 2011, 08:59:49 AM by timovjl »
May the source be with you

EdPellesC99

  • Guest
Re: Geting ChildWindowFromPoint over Edit Control is driving me NUTS !
« Reply #5 on: February 18, 2011, 08:46:01 AM »


   Well my situation (not the above example demo was .... I had a UI window with two edit controls, with some initial boilerplate text.
When it came up, I had the text in the first control conveniently selected. But left clicking in the window or control would negate that.

So I wanted to "make it so" ...that a left click in the control would select the text automatically.

I was able to get what I wanted pretty much.... I now have two cases:
WM_LBUTTONDOWN
WM_LBUTTONDBLCLK

.... so One left click in the client window (of the primary window) selects the text of the first edit control by SetFocus, and a EM_SETSEL message sent.

Then a Double click in the client window .....and you do the same for the second control.

It is 95% of the "shortcut" I wanted without adding the layer of complexity in subclassing.  So far (? brain is tired)  I don't think I have seen it done without using a Dialog Resource and dlg proc.  
Right now, everything I am playing with: is from / in .....the primary window and am not using a Dialog Resource.

Thanks,
Ed


EdPellesC99

  • Guest
Re: Geting ChildWindowFromPoint over Edit Control is driving me NUTS !
« Reply #6 on: February 18, 2011, 08:50:35 AM »

  Yikes,

   Timo as I pressed the preview button, I got a warning that someone had posted and I might want to review mine before I hit the post button.

   OK, I see your post. I will check it out..... thanks.

Right now I need to get some shut-eye as I am falling asleep at the keys.

Thanks much will look over, read at the linked site, and get back tomorrow.

.......Ed

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Geting ChildWindowFromPoint over Edit Control is driving me NUTS !
« Reply #7 on: February 18, 2011, 10:11:21 AM »
To select text when control gets focus ?
Code: [Select]
case WM_COMMAND:
switch(HIWORD(wParam)) {
case EN_SETFOCUS:
if (LOWORD(wParam) == IDC_EDIT1 || LOWORD(wParam) == IDC_EDIT2)
{
//SetWindowText(hFrame, "EN_SETFOCUS");
PostMessage((HWND)lParam, EM_SETSEL, 0, -1);
}
break;
}
break;
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define IDC_EDIT1 4001
#define IDC_EDIT2 4002

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

char *szAppName = "TestEditSelAll";
char *szFrameClass = "cTestEditSelAll";
HWND hFrame;
HANDLE hInst;

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wcx;
MSG msg;

wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.lpfnWndProc = (WNDPROC) WndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hInstance;
wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground= (HBRUSH)COLOR_APPWORKSPACE+1;
wcx.lpszMenuName = NULL;
wcx.lpszClassName= szFrameClass;
wcx.hIconSm = 0;

if (!RegisterClassEx(&wcx))
return 0;
hInst = hInstance;

hFrame = CreateWindowEx(0, szFrameClass, szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
//CW_USEDEFAULT, CW_USEDEFAULT,
230,160,
NULL, NULL, hInst, NULL);
if(!hFrame)
return 0;
ShowWindow(hFrame, nCmdShow);
UpdateWindow(hFrame);

while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
switch(wMsg) {

case WM_COMMAND:
switch(HIWORD(wParam)) {
case EN_SETFOCUS:
if (LOWORD(wParam) == IDC_EDIT1 || LOWORD(wParam) == IDC_EDIT2)
{
//SetWindowText(hFrame, "EN_SETFOCUS");
PostMessage((HWND)lParam, EM_SETSEL, 0, -1);
}
break;
}
break;
case WM_CREATE:
{
HWND hEdit;
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "Edit1 test",
WS_CHILD | WS_VISIBLE | ES_NOHIDESEL,
10, 10, 100, 22, hWnd, (HMENU)IDC_EDIT1, hInst, NULL);
SendMessage(hEdit, WM_SETFONT, (UINT)GetStockObject(DEFAULT_GUI_FONT), 0L);
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "Edit2 test",
WS_CHILD | WS_VISIBLE | ES_NOHIDESEL,
10, 40, 100, 22, hWnd, (HMENU)IDC_EDIT2, hInst, NULL);
SendMessage(hEdit, WM_SETFONT, (UINT)GetStockObject(DEFAULT_GUI_FONT), 0L);
//SetFocus(hEdit);
return 0;
}

case WM_DESTROY:
PostQuitMessage(0);
return 0;

default:
return DefWindowProc(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc(hWnd, wMsg, wParam, lParam);
}
May the source be with you

EdPellesC99

  • Guest
Re: Geting ChildWindowFromPoint over Edit Control is driving me NUTS !
« Reply #8 on: February 18, 2011, 04:50:13 PM »
   Timo,

   Your subclassing example code is very instructive.
I will be looking at this again and again over the next week or two and be looking for an excuse to subclass a control, and get the hang of this.

   With "TestEditSelAll", Wow.
I was thinking I needed to identify the window, when the control was selected...

   case WM_LBUTTONDOWN :
      {
      HWND hFocus = GetFocus();
      char szText[35];
      GetWindowText(hFocus, szText, 34);
      MessageBox(hwnd, szText, "Focus", MB_OK);
      }
Of course would not work. I would think ... what message is sent when the control is clicked I asked? As obviously the window has keyboard focus .... how could I detect it?

   Well, you show how to do it. I did not think it was possible.
I was trying:
HWND hEdwnd;
hEdwnd=ChildWindowFromPoint(hwnd, pt);
if (hEdwnd==hwnd1)
   {
   SendMessage(hEdwnd, EM_SETSEL, 0, -1);
   }
if (hEdwnd==hwnd2)
   {
   SendMessage(hEdwnd, EM_SETSEL, 0, -1);
   }


Your answer to detect the EM_SETSEL message is terrific.

            case EN_SETFOCUS:
               if (LOWORD(wParam) == IDC_EDIT1 || LOWORD(wParam) == IDC_EDIT2)
               {
                  //SetWindowText(hFrame, "EN_SETFOCUS");
                  PostMessage((HWND)lParam, EM_SETSEL, 0, -1);
               }
            break;

I never got a lead on *this* ANYWHERE ! I googled many times with different search terms over days.
It is so irritating to think a link might lead somewhere and it is to an aggregator that has copied the same thread you just read on the original forum.

Lately, I have seen three hits on the first screen of a google search ....that were actually to one thread! ...the original and copies of it ...on aggregating sites .

Seems like you are wasting a lot of time ..........that you never used to.

   It would be nice if there were a tool to detect the messages sent to a particular window, when you "do something".
e.g. "watch the primary window messages" while you click in the edit control.

   Thanks so much Timo for all your input.

..........An Appreciative ......Ed **** ;D ;D ;D  ;) *****

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Geting ChildWindowFromPoint over Edit Control is driving me NUTS !
« Reply #9 on: February 19, 2011, 12:12:19 AM »
   It would be nice if there were a tool to detect the messages sent to a particular window, when you "do something".
e.g. "watch the primary window messages" while you click in the edit control.

There is a window spier available called Winspector.
Microsoft includes a similar tool with their Windows SDK.
---
Stefan

Proud member of the UltraDefrag Development Team

EdPellesC99

  • Guest
Re: Geting ChildWindowFromPoint over Edit Control is driving me NUTS !
« Reply #10 on: February 19, 2011, 12:57:36 AM »

  Thanks Stefan,

  Just downloaded it. My computer can't handle the SDK (it is kind of Peeked) for the winspy++ which is ONLY available in the darn SDK.

Winspector will be given a workout !

...Ed

EdPellesC99

  • Guest
Re: Geting ChildWindowFromPoint over Edit Control is driving me NUTS !
« Reply #11 on: February 19, 2011, 03:51:05 AM »


   Well for me,

   Winspector was unstable (quite). w XP SP2.

I found other comments on this.

   If anyone finds a link to something that works let me know.
I have even found links that say it is not included in Express versions, only in Studio versions fr Microsoft.

I have read it is called Spy.exe and SPYXX.exe.  I have seen many that have looked, but been unable to find it.
Microsoft has a home page for Spy.exe, with nothing about a source.

I would gladly pay for something that worked, the managed version is out there that depends on the .net framework... so I am not interested.

Many tools (but ones that do not show window messages), are available and are good for class, and mouse coordinates etc like the AutoIt v3 Window Information tool.

Until I get a lead to a download, I am out of luck...... until .........I get smart enough to make one myself !

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: Geting ChildWindowFromPoint over Edit Control is driving me NUTS !
« Reply #12 on: February 19, 2011, 10:53:58 AM »
You may check out Control Spy v2.0 a free Microsoft download.
---
Stefan

Proud member of the UltraDefrag Development Team

EdPellesC99

  • Guest
Re: Geting ChildWindowFromPoint over Edit Control is driving me NUTS !
« Reply #13 on: February 19, 2011, 04:04:04 PM »

    Thanks Stefan,

   I was able get a copy of SpyXX.exe also.

I have downloaded and installed Control Spy 2.0

   Though the program was similar in size, it went through a 45 second plus Install process!

   Now, ......just have to put my football helmet on and go play with these toys !
.... Ed

EdPellesC99

  • Guest
Re: Geting ChildWindowFromPoint over Edit Control is driving me NUTS !
« Reply #14 on: February 19, 2011, 04:29:20 PM »


  Ok,

  I see the two tools have entirely different purposes.
Control Spy 2.0 is one heck of a nice instruction tool !
Wow, you could play with that every day for a long time, and (eventually) get the impression you were getting smart!
Super find. (I never saw this tool in any google search I made for utilities to view window messages).

  SpyXX is a super duty analyzer/troubleshooter for your program executables. Light is size, powerful and stable.
I had seen Window Detective:
http://sourceforge.net/projects/windowdetective/
The filesize is 8.3 Mb ..... I was scared to install it !

  Ok I am settup with two great utilities for me now!

  Thanks ALL !