且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在父窗体中处理 CEdit 鼠标单击?

更新时间:2022-12-23 15:00:00

向您的消息映射添加 WM_LBUTTONUP 处理程序

Add a WM_LBUTTONUP handler to your message map

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

最简单的方法是向窗口添加事件处理程序.这最容易通过资源编辑器完成.转到属性页面,然后转到消息部分.然后为 WM_LBUTTONUP 添加一个函数.

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

最后你可以填写如下函数.

Finally you could fill in the function as follows.

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's 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, hit test and if the hit test is in an edit window it will enable it, disable the others and give it keyboard focus.