MFC画矩形边框问题

theme2002 2015-03-05 09:32:38
我最近在画矩形边框时出现了一些问题,麻烦各位帮我看看问题出在哪里。
首先我是这样写的:

CDC tmpDc;
CBitmap tmpBmp;
CBrush nBrush(RGB(255, 255, 255));

tmpBmp.CreateCompatibleBitmap(paintDc, rect.Width(), rect.Height());
tmpDc.CreateCompatibleDC(paintDc);
tmpDc.SelectObject(&tmpBmp);
//Cpen画图省略
tmpDc.SelectObject(&nBrush);
tmpDc.DrawEdge(rect, EDGE_BUMP, BF_RECT);

结果是这样的:


我发现下边框和右边框并没有显示在其中,所以我想是不是
GetClientRect(&rect)
得到的边框宽度和长度超越了,所以我就添加了新的Rect,然后实验,就如下代码:

CRect nR;
CDC tmpDc;
CBitmap tmpBmp;
CBrush nBrush(RGB(255, 255, 255));

tmpBmp.CreateCompatibleBitmap(paintDc, rect.Width(), rect.Height());
tmpDc.CreateCompatibleDC(paintDc);
tmpDc.SelectObject(&tmpBmp);
//Cpen画图省略
tmpDc.SelectObject(&nBrush);
nR.SetRect(rect.left, rect.right, rect.Width()-1, rect.Height()-1);
tmpDc.DrawEdge(nR, EDGE_BUMP, BF_RECT);

然后显示结果一开始是这样的:

在我拖动大小后是这样的:


这样的话我就奇怪了,我画个边框怎么就这样.我也试过用Rectangle函数,效果是一样的。
目前我是用Cpen把底边框和右边框画上去了,但效果却不如DrawEdge。麻烦指导一下
...全文
898 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
web_211xun 2019-03-09
  • 打赏
  • 举报
回复
Visual C++ 画交互矩形框,可改变矩形大小,可移动矩形框源代码:http://www.a9u9.com/download_page_1909_cn.html
向立天 2015-04-07
  • 打赏
  • 举报
回复
您好 我是本版版主 此帖已多日无人关注 请您及时结帖 如您认为问题没有解决可按无满意结帖处理 另外本版设置了疑难问题汇总帖 并已在版面置顶 相关规定其帖子中有说明 您可以根据规定提交您帖子的链接 如您目前不想结帖只需回帖说明 我们会删除此结帖通知 见此回复三日内无回应 我们将强制结帖 相关规定详见界面界面版关于版主结帖工作的具体办法
worldy 2015-03-06
  • 打赏
  • 举报
回复
真不行就使用LineTo吧。无非就是画线
theme2002 2015-03-06
  • 打赏
  • 举报
回复
引用 3 楼 worldy 的回复:
EDGE_BUMP这个参数改为0试试
改为0就什么边框都没有了
worldy 2015-03-06
  • 打赏
  • 举报
回复
EDGE_BUMP这个参数改为0试试
theme2002 2015-03-06
  • 打赏
  • 举报
回复
引用 1 楼 happyparrot 的回复:
你这个rect的left,top值是0吗? 原则上nR.SetRect(rect.left, rect.right, rect.Width()-1, rect.Height()-1);是有问题的。 一方面至少应该是rect.left,rect.top,不能用rect.right 另一方面,你这个nR是相对于内存位图的,而不是客户区,所以不能用客户区的左上角值,而应该直接用(0,0)。虽然可能left和top本身就是0
明白了,是我写错top为right了。刚刚有试过,如果设置nR.SetRect(rect.left, rect.right, rect.Width(), rect.Height());是正好有个矩形边框贴合在网格上(rect的left和top为5,如果为0时,left和top会有偏移。) 但我仍然有一个疑问,在rect和nR的范围都一致的情况下,调用DrawEdge代入rect的下边框和右边框为什么会消失而nR不会?
快乐鹦鹉 2015-03-06
  • 打赏
  • 举报
回复
你这个rect的left,top值是0吗? 原则上nR.SetRect(rect.left, rect.right, rect.Width()-1, rect.Height()-1);是有问题的。 一方面至少应该是rect.left,rect.top,不能用rect.right 另一方面,你这个nR是相对于内存位图的,而不是客户区,所以不能用客户区的左上角值,而应该直接用(0,0)。虽然可能left和top本身就是0
赵4老师 2015-03-06
  • 打赏
  • 举报
回复
CDC::DrawEdge BOOL DrawEdge( LPRECT lpRect, UINT nEdge, UINT nFlags ); Return Value Nonzero if successful; otherwise 0. Parameters lpRect A pointer to a RECT structure that contains the logical coordinates of the rectangle. nEdge Specifies the type of inner and outer edge to draw. This parameter must be a combination of one inner-border flag and one outer-border flag. See the Remarks section for a table of the parameter’s types. nFlags The flags that specify the type of border to be drawn. See the Remarks section for a table of the parameter’s values: Remarks Call this member function to draw the edges of a rectangle of the specified type and style. The inner and outer border flags are as follows: Inner-border flags BDR_RAISEDINNER Raised inner edge. BDR_SUNKENINNER Sunken inner edge. Outer-border flags BDR_RAISEDOUTER Raised outer edge. BDR_SUNKENOUTER Sunken outer edge. The nEdge parameter must be a combination of one inner and one outer border flag. The nEdge parameter can specify one of the following flags: EDGE_BUMP Combination of BDR_RAISEDOUTER and BDR_SUNKENINNER. EDGE_ETCHED Combination of BDR_SUNKENOUTER and BDR_RAISEDINNER. EDGE_RAISED Combination of BDR_RAISEDOUTER and BDR_RAISEDINNER. EDGE_SUNKEN Combination of BDR_SUNKENOUTER and BDR_SUNKENINNER. The nFlags parameter types are as follows: BF_RECT Entire border rectangle. BF_LEFT Left side of border rectangle. BF_BOTTOM Bottom of border rectangle. BF_RIGHT Right side of border rectangle. BF_TOP Top of border rectangle. BF_TOPLEFT Top and left side of border rectangle. BF_TOPRIGHT Top and right side of border rectangle. BF_BOTTOMLEFT Bottom and left side of border rectangle. BF_BOTTOMRIGHT Bottom and right side of border rectangle. For diagonal lines, the BF_RECT flags specify the end point of the vector bounded by the rectangle parameter. BF_DIAGONAL_ENDBOTTOMLEFT Diagonal border. The end point is the bottom-left corner of the rectangle; the origin is top-right corner. BF_DIAGONAL_ENDBOTTOMRIGHT Diagonal border. The end point is the bottom-right corner of the rectangle; the origin is top-left corner. BF_DIAGONAL_ENDTOPLEFT Diagonal border. The end point is the top-left corner of the rectangle; the origin is bottom-right corner. BF_DIAGONAL_ENDTOPRIGHT Diagonal border. The end point is the top-right corner of the rectangle; the origin is bottom-left corner. For more information about the Windows API DrawEdge, see::DrawEdge in the Win32 SDK Programmer’s Reference. CDC Overview | Class Members | Hierarchy Chart See Also ::DrawEdge
theme2002 2015-03-06
  • 打赏
  • 举报
回复
引用 6 楼 zhao4zhong1 的回复:
GDI绘图遵循左上画,右下不画的半开半闭原则。
那个矩形框我已经通过tmpDc.DrawEdge(nR, EDGE_BUMP, BF_RECT);实现了,但仍然不解的是,同样是CRect,同样的矩形大小,使用tmpDc.DrawEdge(rect, EDGE_BUMP, BF_RECT);则是半开半闭的矩形。为什么同一个函数会导致两种结果出来,能帮我解释一下吗?谢谢!
赵4老师 2015-03-06
  • 打赏
  • 举报
回复
GDI绘图遵循左上画,右下不画的半开半闭原则。

15,980

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧