在MFC对话框中绘制对话框边距

时间:2014-10-03 11:16:55

标签: c++ mfc dialog border shadow

我想要一个无边框的对话框,但却有一个对话框阴影。我遇到了这个解决方案Borderless Window Using Areo Snap, Shadow, Minimize Animation, and Shake,它通过使Dialog具有1 px的保证金并将客户区扩展到它来使用解决方法。

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

Blank Dialog without a Border but with a Dialog Shadow

帖子提到客户区域实际上是扩展的,而透明绘图使得每个1px的D​​ialog边缘再次可见。

现在这正是发生的事情,当我试图在整个对话框上绘制一个Solid Rectangle时:

// 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);

但是对话框仍然以其边距绘制: Blank Dialog with a Border of 1px each

怎样才能画出边缘拉伸的东西?后来我将使用图片作为对话框背景,应该在边缘绘制。

1 个答案:

答案 0 :(得分:0)

感谢Hans Passant的评论。解决方案是使用GDI +绘图而不是GDI绘图

// 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);

enter image description here