How to handle CEdit mouse click in parent form?

I'm new to MFC (from C # and Java) and am figuring out what's going on.

Consider a dialog with three text fields. I subclassed CEdit for CMyEdit and three text boxes are connected to CMyEdit variables in the dialog class.

I want to let the dialog class "know" when any of the three text boxes was clicked with the left mouse button. I found examples of how to add an ON_WM_LBUTTONDOWN handler to my CMyEdit class. Works great, but the handler is only in the CMyEdit class. Suppose that whenever one of the text boxes is clicked, I want the dialog to disable the other two. How can I get the left click notification dialog box?

This is a completely contrived and simplified example. I don't actually have an app where I am worried about left clicks on text fields. But I think the fact that I cannot figure out how to do this indicates a fundamental misunderstanding of how to deal with UI events in MFC.

From the C # world where everything is done for me and I have direct access to any of the events I want (got focus, double click, whatever). I am very confused about why certain events are special and provide easy access. In the case of CEdit, I don’t understand why I have no problem to get focus, remove focus, change and several other selves, but there are no other events, such as a mouse click.

But back to my actual question: in the above scenario, how can I get the left click notification dialog in text boxes? Do the text fields need to raise events or send messages (or whatever) to the dialog?

+2


source to share


1 answer


Add WM_LBUTTONUP handler to message map

t

BEGIN_MESSAGE_MAP(CYourDialog, CDialog)
    ON_WM_LBUTTONUP()
END_MESSAGE_MAP()

      

This is most easily accomplished by adding an event handler to the window. This is easiest to do with the resource editor. Go to the properties page, then go to the messages section. Then add a function for WM_LBUTTONUP.



Finally, you can fill in the function like this.

void CYourDialog::OnLButtonUp(UINT nFlags, CPoint point)
{
    // Grab the 3 (or more) edit control
    CEdit* pEdit1   = (CEdit*)GetDlgItem( ID_YOUR_EDIT_CONTROL1 );
    CEdit* pEdit2   = (CEdit*)GetDlgItem( ID_YOUR_EDIT_CONTROL2 );
    CEdit* pEdit3   = (CEdit*)GetDlgItem( ID_YOUR_EDIT_CONTROL3 );

    // Grab the edit control window rects in screen coords.
    CRect edit1Rect;
    CRect edit2Rect;
    CRect edit3Rect;
    pEdit1->GetWindowRect( &edit1Rect );
    pEdit2->GetWindowRect( &edit2Rect );
    pEdit3->GetWindowRect( &edit3Rect );

    // Convert to client coordinates relative to their parent (ie this) window.
    ScreenToClient( edit1Rect );
    ScreenToClient( edit2Rect );
    ScreenToClient( edit3Rect );

    // Test if the point passed in to this function is in the control rectangle.
    const BOOL bEnable1 = edit1Rect.PtInRect( point );
    const BOOL bEnable2 = edit2Rect.PtInRect( point );
    const BOOL bEnable3 = edit3Rect.PtInRect( point );

    // Enable the window that was clicked on and disable the others.
    pEdit1->EnableWindow( bEnable1 );
    pEdit2->EnableWindow( bEnable2 );
    pEdit3->EnableWindow( bEnable3 );

    // Set keyboard focus to the relevant control
    if      ( bEnable1 )
    {
        pEdit1->SetFocus();
    }
    else if ( bEnable1 )
    {
        pEdit2->SetFocus();
    }
    else if ( bEnable1 )
    {
        pEdit3->SetFocus();
    }

    CDialog::OnLButtonUp(nFlags, point);
}

      

This will essentially be a hit test, and if the hit test is in the edit window, it will enable it, disable the others, and give it keyboard focus.

+2


source







All Articles