简体   繁体   中英

Draw on parent dialog in mfc

I have a dialog which contains many controls. for ex: edit control. Now I am developing these edit controls which is having colorful border. But control redraw itself each time user enter the input in edit control, so border flickers. Now I want to draw border on the dialog which is having this control. Is it possible in mfc?

You can achieve this by customizing your control class and drawing on non client area. I have implemented this in my project with no flicking issue.

在此处输入图片说明

Here is the idea goes:

/////////////////////////////////////////////////////////////////////////////
///
/// /This method is overriden, to modify the style of editcrtl
///
/////////////////////////////////////////////////////////////////////////////
void CEdit1::PreSubclassWindow()
{
    ModifyStyleEx(0, WS_EX_STATICEDGE, 0); //to make sure your border is static edge
}

and on non client area you just draw red rectangle:

/////////////////////////////////////////////////////////////////////////////
///
/// /This handler is used to paint the non- client area
///
/// /return none
///
/////////////////////////////////////////////////////////////////////////////
void CEdit1::OnNcPaint() 
{
    CDC* pDC = GetWindowDC();

    //work out the coordinates of the window rectangle,
    CRect rect;
    GetWindowRect( &rect);
    rect.OffsetRect( -rect.left, -rect.top);

    //Draw a single line around the outside
    CBrush brush(RGB(255,0,0));
    pDC->FrameRect(&rect, &brush);
    ReleaseDC( pDC);
}

I made changes in Onsize and reduced the control by 1px on each side then draw the border. something like this

rcRichEdit.left += 1;
        rcRichEdit.right -= 1;
        rcRichEdit.bottom -= 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM