简体   繁体   中英

Draw over Dialog Margins in an MFC Dialog

I want a dialog to be borderless and yet have a dialog shadow. I came across this solution Borderless Window Using Areo Snap, Shadow, Minimize Animation, and Shake which uses a workaround by making the Dialog having a Margin of 1 px and extending the Client Area to it.

MARGINS borderless = { 1, 1, 1, 1 };
DwmExtendFrameInfoClientArea(this->GetSafeHwnd(), &borderless);

空白对话框无边框但带有对话框阴影

The post mentioned that the Client Area is literally being extended and Transparent drawing makes the Dialog edges of 1px each visible again.

Now this is exactly what happened, when I tried to paint a Solid Rectangle onto the whole dialog:

// getting the client area
CRect clientRect;
GetClientRect(&clientRect);

// expanding it to the new margins
clientRect.left -= 1;
clientRect.top -= 1;
clientRect.right += 2;
clientRect.bottom += 2;

// set the Device Context to draw non transparent and with a black background
pDC->SetBkMode(OPAQUE);
pDC->SetBkColor(RGB(0, 0, 0));

// finally draw a rectangle to it
CBrush brush_back_ground(RGB(0, 0, 0));
pDC->FillRect(clientRect, &brush_back_ground);

But the dialog is still drawn with its margins: 边框各为1px的空白对话框

How would it be possible to draw something stretched on the margins? Later I'm going to use pictures as dialog Background which should be drawn on the margins aswell.

Thanks to Hans Passant for his comment. The solution is to use GDI+ drawing instead of GDI drawing

// making a Gdi+ graphics object from my CDC*
Graphics g(*pDC);

// getting the client area
CRect clientRect;
GetClientRect(&clientRect);

// making a Gdi+ rect
Rect bkRect(0,0,clientRect.Width(), clientRect.Height());

// making a pen for the Rect Drawing
Pen bkPen(Color(255,0,0,0));

// draw the rectangle over the full dialog
g.DrawRectangle(&bkPen, bkRect);

在此处输入图片说明

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