First, I want to ignore a mouse button click when my window is in the background. I want the Window to become active and come to the front but I don’t want a mouse message about it.

Microsoft does some really odd things. I found is that there is a simple way to accomplish what I want but it doesn’t work.

int CMyWindow::OnMouseActivate( CWnd* pDesktopWnd, UINT nHitTest, UINT message )
{
	int Result = CWnd::OnMouseActivate( pDesktopWnd, nHitTest, message );

	if( Result == MA_ACTIVATE )
		return MA_ACTIVATEANDEAT;

	return Result;
}

The code above looks great. All you need to do is catch that message and return that special MA_ACTIVATEANDEAT value that tells windows to discard the mouse message. This should work exactly as planned. It doesn’t.

I need to use this for a CView derived class so that my view window can know what to do with the mouse clicks. Here is the code that actually works:

int CChildView::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message)
{
	int Result = CView::OnMouseActivate( pDesktopWnd, nHitTest, message );

	if( Result == MA_ACTIVATE && this != GetFocus() )
		return MA_ACTIVATEANDEAT;

	return Result;
}

The reason that the GetFocus() function call is in there is that this message function gets called for every single mouse click in the child view window.

This just seems wrong but apparently, it is supposed to work this way. Notepad exhibits the same behavior and there is a WM_MOUSEACTIVATE message just before every WM_LBUTTONDOWN message that is sent to the window. I don’t get the point of this and only the first message that actually activates the window would make sense. I guess this is a hold-over from the days when GetActiveWindow() woulds return the one system-wide active window and now it returns the active window for the current thread message queue, or something like that.

Here is the documentation for WM_MOUSEACTIVATE that suggests that the behavior is just wrong:

Sent when the cursor is in an inactive window and the user presses a mouse button.

Is my window and even Notepad staying inactive because they are child windows? Is the documentation just wrong? Hell if I know.