Why is my child window not responding to mouse events?

I created a custom static window that displays a bitmap; this window is a child window of some other window. Now I want to capture mouse events for this window so that I can provide functionality for cropping the image.

But the problem is that mouse events are not being sent to this child window .... The following is a code snippet of the WndProc child window.

 WNDPROC origStatProc; // Variable which stores the handle of BITMAP image HBITMAP hBitmap=NULL; LRESULT CALLBACK dispWndProc(HWND hwnd,UINT msg, WPARAM wParam, LPARAM lParam) { static HDC hdc; static PAINTSTRUCT paintSt; static RECT aRect; switch(msg) { case WM_PAINT: { hdc = BeginPaint(hwnd,&paintSt); GetClientRect(hwnd,&aRect); if(hBitmap!=NULL) { HDC memDC = CreateCompatibleDC(hdc); if(memDC!=NULL) { BITMAP bmp; GetObject(hBitmap,sizeof(bmp),&bmp); SelectObject(memDC,hBitmap); SetStretchBltMode(hdc,HALFTONE); StretchBlt(hdc,0,0,aRect.right,aRect.bottom, memDC,0,0,bmp.bmWidth,bmp.bmHeight, SRCCOPY); DeleteObject(&bmp); ReleaseDC(hwnd,memDC); } } // the code for painting EndPaint(hwnd,&paintSt); } break; case STM_SETIMAGE: { InvalidateRect(hwnd,&aRect,true); } break; case WM_LBUTTONDOWN: { int xPos = GET_X_LPARAM(lParam); int yPos = GET_Y_LPARAM(lParam); char xstr[10]; _itoa(xPos,xstr,10); MessageBox(NULL,xstr,"X Value ",MB_OK); } break; default: return origStatProc(hwnd,msg,wParam,lParam); } return 0; } 

Can someone tell me what else I need to capture mouse events inside this Child window ...

+4
source share
3 answers

The window class that you use for the window will determine the specific default behavior for the window. The Static window class is especially difficult to work because Windows makes the assumption that the window will never change its contents and will not interact with the user in any way. You will probably find that WM_LBUTTONDOWN is passed to the parent window.

+1
source

If I remember correctly: static windows declare themselves invisible to mouse clicks, returning HTTRANSPARENT in response to WM_NCHITTEST. Because of this window, the mouse click on the parent object is skipped. If you want to handle mouse clicks in static, you also need to override this behavior to return HTCLIENT instead.

+1
source

I called DefWndProc () instead of origStatProc (hwnd, msg, wParam, lParam) and the problem is resolved ....

anyWays thanks everyone ....

+1
source

All Articles