0

I have a situation that I am not sure how to resolve.

I have a CDialog that supports dynamic resizing and I have a static control on there:

Static Control

I have set the properties of this control to invisible and to move 100 in both directions:

Properties

I have set it as invisible because I want to use it as a placeholder. In OnPaint I then do the following:

void CAssignmentsDlg::OnPaint() 
{
    CResizingDialog::OnPaint();

    CPaintDC dc(this); // device context for painting

    COLORREF    crTextHL, crBackHL;
    COLORREF    crTextOld, crBackOld;
    CString     strText;

    EstablishLegendPosition();

    crBackHL = ::GetSysColor(COLOR_HIGHLIGHT);
    crTextHL = ::GetSysColor(COLOR_HIGHLIGHTTEXT);

    dc.FillSolidRect(m_rcKey, crBackHL);

    crBackOld = dc.SetBkColor(crBackHL);
    crTextOld = dc.SetTextColor(crTextHL);

    strText.LoadString( IDS_STR_SAMPLE_CONFLICT );
    CFont l_font;
    l_font.CreateFont(14, 0, 0, 0, FW_NORMAL,
        FALSE, FALSE, FALSE, 0, 
        OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,   
        DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, _T("Arial"));

    CFont* l_old_font = dc.SelectObject(&l_font);

    dc.DrawText( strText, m_rcKey, DT_SINGLELINE|DT_CENTER|DT_VCENTER );

    dc.SetBkColor(crBackOld);
    dc.SetTextColor(crTextOld);

    // Delete the font object. 
    dc.SelectObject(l_old_font);
    l_font.DeleteObject();
}

EstablishLegendPosition is defined like this:

void CAssignmentsDlg::EstablishLegendPosition()
{
    // establish position of key
    m_lblKey.GetClientRect(m_rcKey);
    m_lblKey.ClientToScreen(m_rcKey);
    ScreenToClient(m_rcKey);
}

It seems that unless the control is visible it will not move. But if it is visible it then seems to overwrite my rendering as I can't see my legend drawn on the dialog:

Result

So how can I manually draw a legend in the bottom right with OnPaint on a dynamic resize dialog?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 1
    Why don't you subclass the "placeholder" static control to do the actual drawing (i. e. set `SS_OWNERDRAW`, handle `CStatic::DrawItem()`)? – zett42 May 13 '19 at 19:26
  • @zett42 Sorted! I created the new class from `CStatic` and overrode `OnPaint` in there instead. Works. :) – Andrew Truckle May 13 '19 at 19:59

0 Answers0