none
Can't get screen/client coordinates right

    Question

  • Hi

    A small class CFrameGroup, derived from CStatic, get's created with parent window (the application dialog) and an IDC_STATIC_xxx of a group box. The CFrameGroup object then draws a "highlight" frame inside the groupbox to get the users attention of that particular groupbox.

    I can't get the position of that frame right! 

    Code of CFrameGroup class:

    IMPLEMENT_DYNAMIC(CFrameGroup, CStatic)
    
    CFrameGroup::CFrameGroup( )
    	: mbHighlighted( FALSE )
    {
    }
    
    CFrameGroup::~CFrameGroup()
    {
    }
    
    
    BEGIN_MESSAGE_MAP(CFrameGroup, CStatic)
    	ON_WM_PAINT()
    END_MESSAGE_MAP()
    
    // CFrameGroup message handlers
    
    void CFrameGroup::OnPaint()
    {
    	CPaintDC dc(this); // device context for painting
    	CRect cRect;
        CPen penBlack;
    
    	GetRect( cRect );
    
    	if( mbHighlighted )
    		penBlack.CreatePen(PS_SOLID, 2, RGB(0xff, 0, 0));
    	else
    		penBlack.CreatePen( PS_SOLID, 2, RGB( 0x00, 0x00, 0x00 ));
    
        CPen* pOldPen = dc.SelectObject(&penBlack);
    
    	dc.MoveTo( cRect.left, cRect.top );
    	dc.LineTo( cRect.right, cRect.top );
    	dc.LineTo( cRect.right, cRect.bottom );
    	dc.LineTo( cRect.left, cRect.bottom );
    	dc.LineTo( cRect.left, cRect.top + 1 );
    
         dc.SelectObject(pOldPen);
    }
    
    void CFrameGroup::SetHighlighted( BOOL highlighted )
    {
    	mbHighlighted = highlighted;
    	Invalidate();
    }
    
    BOOL CFrameGroup::GetHighlighted()
    {
    	return mbHighlighted;
    }
    
    
    BOOL CFrameGroup::Create( UINT groupBoxId, CWnd *parent )
    {
    	CRect cRect;
    	mGroupBoxId = groupBoxId;
    	mpParent = parent;
    	mpGroupBox = parent->GetDlgItem( groupBoxId );
    	GetRect( cRect );
    
    	return CStatic::Create( _T("STATIC"), WS_CHILD | WS_VISIBLE, cRect, parent );
    }
    
    void CFrameGroup::GetRect( CRect& rect )
    {
    	mpGroupBox->GetWindowRect( rect );
    	mpParent->ScreenToClient( rect );
    	rect.DeflateRect( 5, 12, 5, 5 );
    }
    


    From the application dialogue the the CFrameGroup objects are created in OnInitDialogue

    	mFrameG1.Create( IDC_STATIC_1, this );
    	mFrameG2.Create( IDC_STATIC_2, this );

    Best Regards

    Tage

    

    Tuesday, November 20, 2012 4:28 PM

All replies

  • It looks like your question belongs in the MFC forum:

    http://social.msdn.microsoft.com/Forums/en-US/vcmfcatl/threads

    Your problem might be that you are saving the results of GetDlgItem in a member variable (mpGroupBox).

    GetDlgItem returns a temporary pointer that should not be saved. (Just call GetDlgItem in any function where you need its result.)

     

    Tuesday, November 20, 2012 7:36 PM