这样的代码可不可以简化下,感觉很麻烦

qscool1987 2011-11-22 05:35:44

void CImgDisplay::DrawItemRect()
{
CDC *pdc = GetDC();
CRect rect;
GetClientRect(&rect);
m_rect[0].left = rect.right - 58;
m_rect[0].top = rect.top + 53;
m_rect[0].right = rect.right - 28;
m_rect[0].bottom = rect.top + 58;
m_dc.FillSolidRect(&m_rect[0],CLR_ITEMRECT);
m_rect[1].left = m_rect[0].left - 5;
m_rect[1].top = m_rect[0].top + 5;
m_rect[1].right = m_rect[0].left;
m_rect[1].bottom = m_rect[0].top + 35;
m_dc.FillSolidRect(&m_rect[1],CLR_ITEMRECT);
m_rect[2].left = m_rect[0].left;
m_rect[2].top = m_rect[0].top + 35;
m_rect[2].right = m_rect[0].right;
m_rect[2].bottom = m_rect[0].bottom + 35;
m_dc.FillSolidRect(&m_rect[2],CLR_CHARRECT);
m_rect[3].left = m_rect[0].right;
m_rect[3].top = m_rect[0].top + 5;
m_rect[3].right = m_rect[0].right + 5;
m_rect[3].bottom = m_rect[0].bottom + 30;
m_dc.FillSolidRect(&m_rect[3],CLR_CHARRECT);
m_rect[4].left = m_rect[0].left - 5;
m_rect[4].top = m_rect[0].top + 40;
m_rect[4].right = m_rect[0].left;
m_rect[4].bottom = m_rect[0].bottom + 65;
m_dc.FillSolidRect(&m_rect[4],CLR_CHARRECT);
m_rect[5].left = m_rect[0].left;
m_rect[5].top = m_rect[0].top + 70;
m_rect[5].right = m_rect[0].right;
m_rect[5].bottom = m_rect[0].bottom + 70;
m_dc.FillSolidRect(&m_rect[5],CLR_CHARRECT);
m_rect[6].left = m_rect[0].right;
m_rect[6].top = m_rect[0].top + 40;
m_rect[6].right = m_rect[0].right + 5;
m_rect[6].bottom = m_rect[0].bottom + 65;
m_dc.FillSolidRect(&m_rect[6],CLR_CHARRECT);
m_rect[7].left = m_rect[0].right + 9;
m_rect[7].top = m_rect[0].top + 70;
m_rect[7].right = m_rect[0].right + 14;
m_rect[7].bottom = m_rect[0].bottom + 70;
m_dc.FillSolidRect(&m_rect[7],CLR_CHARRECT);
//-----------------------------------------
m_rect[8].left = m_rect[0].left - 66;
m_rect[8].top = m_rect[0].top;
m_rect[8].right = m_rect[0].right - 66;
m_rect[8].bottom = m_rect[0].bottom;
m_dc.FillSolidRect(&m_rect[8],CLR_CHARRECT);
m_rect[9].left = m_rect[8].left - 5;
m_rect[9].top = m_rect[8].top + 5;
m_rect[9].right = m_rect[8].left;
m_rect[9].bottom = m_rect[8].top + 35;
m_dc.FillSolidRect(&m_rect[9],CLR_ITEMRECT);
m_rect[10].left = m_rect[8].left;
m_rect[10].top = m_rect[8].top + 35;
m_rect[10].right = m_rect[8].right;
m_rect[10].bottom = m_rect[8].bottom + 35;
m_dc.FillSolidRect(&m_rect[10],CLR_CHARRECT);
m_rect[11].left = m_rect[8].right;
m_rect[11].top = m_rect[8].top + 5;
m_rect[11].right = m_rect[8].right + 5;
m_rect[11].bottom = m_rect[8].bottom + 30;
m_dc.FillSolidRect(&m_rect[11],CLR_CHARRECT);
m_rect[12].left = m_rect[8].left - 5;
m_rect[12].top = m_rect[8].top + 40;
m_rect[12].right = m_rect[8].left;
m_rect[12].bottom = m_rect[8].bottom + 65;
m_dc.FillSolidRect(&m_rect[12],CLR_CHARRECT);
m_rect[13].left = m_rect[8].left;
m_rect[13].top = m_rect[8].top + 70;
m_rect[13].right = m_rect[8].right;
m_rect[13].bottom = m_rect[8].bottom + 70;
m_dc.FillSolidRect(&m_rect[13],CLR_CHARRECT);
m_rect[14].left = m_rect[8].right;
m_rect[14].top = m_rect[8].top + 40;
m_rect[14].right = m_rect[8].right + 5;
m_rect[14].bottom = m_rect[8].bottom + 65;
m_dc.FillSolidRect(&m_rect[14],CLR_CHARRECT);
m_rect[15].left = m_rect[8].right + 9;
m_rect[15].top = m_rect[8].top + 70;
m_rect[15].right = m_rect[8].right + 14;
m_rect[15].bottom = m_rect[8].bottom + 70;
m_dc.FillSolidRect(&m_rect[15],CLR_CHARRECT);
//....
}

42个小矩形,模拟7段数码管的6个8,在对话框的一排上显示,我这一个一个矩形的去确定坐标,一个一个去校验很麻烦,可不可以简化,几行代码OK的??
...全文
215 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
pathuang68 2011-11-23
  • 打赏
  • 举报
回复
0. 代码的确有优化的潜力。
1. 写一个数码管的矩形类(八段),在类中,各段之间的相对位置是固定的。
2. 然后只要确定矩形类对象的top-left那点的坐标,就可以构造一个对象。这样,每增加一个“8”,仅需提供top-left的坐标就OK了,楼主现在的代码,每增加一个“8”还是相当麻烦的。
BT六眼飞鱼 2011-11-23
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 iblold 的回复:]

C/C++ code

void CImgDisplay::DrawItemRect()
{
CDC *pdc = GetDC();
CRect rect;
GetClientRect(&rect);

RECT roffset[] =
{
{-5, 5, 0, 35},
{0, 35, 0, 35},
……
[/Quote]
恩 循环下就可以了~!
qscool1987 2011-11-23
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 iblold 的回复:]
C/C++ code

void CImgDisplay::DrawItemRect()
{
CDC *pdc = GetDC();
CRect rect;
GetClientRect(&rect);

RECT roffset[] =
{
{-5, 5, 0, 35},
{0, 35, 0,……
[/Quote]
值得借鉴啊,谢了
iblold 2011-11-23
  • 打赏
  • 举报
回复

void CImgDisplay::DrawItemRect()
{
CDC *pdc = GetDC();
CRect rect;
GetClientRect(&rect);

RECT roffset[] =
{
{-5, 5, 0, 35},
{0, 35, 0, 35},
{0, 5, 5, 30},
{-5, 40, 0, 65},
{0, 70, 0, 70},
{0, 40, 5, 65},
{9, 70, 14, 70},
};

RECT rbase[] =
{
{-58 + rect.left, 53 + rect.top, -28 + rect.right, 58 + rect.bottom},
{-66 + rbase[0].left, 0 + rbase[0].top, -66 + rbase[0].right, 0 + rbase[0].bottom},
};

for(int j = 0; j < 2; ++j)
{
for(int i = 0; i < 7; ++i)
{
RECT rtmp = {rbase[j].left + roffset[i].left, rbase[j].top + roffset[i].top,
rbase[j].right + roffset[i].right, rbase[j].bottom + roffset[i].bottom};

CRect r = rtmp;
m_dc.FillSolidRect(&r,CLR_ITEMRECT);

}
}
//....
}
ybjx111 2011-11-23
  • 打赏
  • 举报
回复
建立一个结构体 {CRect re;bool bVisible;};
每一个数码管含有7个这种类型的变量。外加一个CPoint pt就可以了。
由pt能够得到7个结构体变量的re,因为数码管大小是一致的。
由结构体的bVisible变量确定这个结构体的re是否绘制,来实现不同的数字。
具体算法不是很麻烦。可以写成类,也可以写成函数。不过写成类要容易管理一下(只有6个变量,函数形式的换有n个变量要管理)。
就是那个党伟 2011-11-23
  • 打赏
  • 举报
回复
貌似
挺啰嗦
等高人解答吧
碎碎念 2011-11-23
  • 打赏
  • 举报
回复
没看出啥规律,如果还有其他的地方用到相同的图像画法的话,就封装成函数吧
赵4老师 2011-11-23
  • 打赏
  • 举报
回复
void CImgDisplay::DrawItemRect()
{
CDC *pdc = GetDC();
CRect rect;
GetClientRect(&rect);
//填表式编程:(再一次体现出C/C++的强大!)
m_rect[ 0].left= rect .right-58;m_rect[ 0].top= rect .top+53;m_rect[ 0].right= rect .right-28;m_rect[ 0].bottom= rect .top +58;m_dc.FillSolidRect(&m_rect[ 0],CLR_ITEMRECT);
m_rect[ 1].left=m_rect[0].left - 5;m_rect[ 1].top=m_rect[0].top+ 5;m_rect[ 1].right=m_rect[0].left ;m_rect[ 1].bottom=m_rect[0].top +35;m_dc.FillSolidRect(&m_rect[ 1],CLR_ITEMRECT);
m_rect[ 2].left=m_rect[0].left ;m_rect[ 2].top=m_rect[0].top+35;m_rect[ 2].right=m_rect[0].right ;m_rect[ 2].bottom=m_rect[0].bottom+35;m_dc.FillSolidRect(&m_rect[ 2],CLR_CHARRECT);
m_rect[ 3].left=m_rect[0].right ;m_rect[ 3].top=m_rect[0].top+ 5;m_rect[ 3].right=m_rect[0].right+5 ;m_rect[ 3].bottom=m_rect[0].bottom+30;m_dc.FillSolidRect(&m_rect[ 3],CLR_CHARRECT);
m_rect[ 4].left=m_rect[0].left - 5;m_rect[ 4].top=m_rect[0].top+40;m_rect[ 4].right=m_rect[0].left ;m_rect[ 4].bottom=m_rect[0].bottom+65;m_dc.FillSolidRect(&m_rect[ 4],CLR_CHARRECT);
m_rect[ 5].left=m_rect[0].left ;m_rect[ 5].top=m_rect[0].top+70;m_rect[ 5].right=m_rect[0].right ;m_rect[ 5].bottom=m_rect[0].bottom+70;m_dc.FillSolidRect(&m_rect[ 5],CLR_CHARRECT);
m_rect[ 6].left=m_rect[0].right ;m_rect[ 6].top=m_rect[0].top+40;m_rect[ 6].right=m_rect[0].right+5 ;m_rect[ 6].bottom=m_rect[0].bottom+65;m_dc.FillSolidRect(&m_rect[ 6],CLR_CHARRECT);
m_rect[ 7].left=m_rect[0].right+ 9;m_rect[ 7].top=m_rect[0].top+70;m_rect[ 7].right=m_rect[0].right+14;m_rect[ 7].bottom=m_rect[0].bottom+70;m_dc.FillSolidRect(&m_rect[ 7],CLR_CHARRECT);
m_rect[ 8].left=m_rect[0].left -66;m_rect[ 8].top=m_rect[0].top ;m_rect[ 8].right=m_rect[0].right-66;m_rect[ 8].bottom=m_rect[0].bottom ;m_dc.FillSolidRect(&m_rect[ 8],CLR_CHARRECT);
m_rect[ 9].left=m_rect[8].left - 5;m_rect[ 9].top=m_rect[8].top+ 5;m_rect[ 9].right=m_rect[8].left ;m_rect[ 9].bottom=m_rect[8].top +35;m_dc.FillSolidRect(&m_rect[ 9],CLR_ITEMRECT);
m_rect[10].left=m_rect[8].left ;m_rect[10].top=m_rect[8].top+35;m_rect[10].right=m_rect[8].right ;m_rect[10].bottom=m_rect[8].bottom+35;m_dc.FillSolidRect(&m_rect[10],CLR_CHARRECT);
m_rect[11].left=m_rect[8].right ;m_rect[11].top=m_rect[8].top+ 5;m_rect[11].right=m_rect[8].right+5 ;m_rect[11].bottom=m_rect[8].bottom+30;m_dc.FillSolidRect(&m_rect[11],CLR_CHARRECT);
m_rect[12].left=m_rect[8].left - 5;m_rect[12].top=m_rect[8].top+40;m_rect[12].right=m_rect[8].left ;m_rect[12].bottom=m_rect[8].bottom+65;m_dc.FillSolidRect(&m_rect[12],CLR_CHARRECT);
m_rect[13].left=m_rect[8].left ;m_rect[13].top=m_rect[8].top+70;m_rect[13].right=m_rect[8].right ;m_rect[13].bottom=m_rect[8].bottom+70;m_dc.FillSolidRect(&m_rect[13],CLR_CHARRECT);
m_rect[14].left=m_rect[8].right ;m_rect[14].top=m_rect[8].top+40;m_rect[14].right=m_rect[8].right+5 ;m_rect[14].bottom=m_rect[8].bottom+65;m_dc.FillSolidRect(&m_rect[14],CLR_CHARRECT);
m_rect[15].left=m_rect[8].right+ 9;m_rect[15].top=m_rect[8].top+70;m_rect[15].right=m_rect[8].right+14;m_rect[15].bottom=m_rect[8].bottom+70;m_dc.FillSolidRect(&m_rect[15],CLR_CHARRECT);
//....
}
luciferisnotsatan 2011-11-23
  • 打赏
  • 举报
回复
写个循环就行了,然后数组映射加减值
h415640525h 2011-11-23
  • 打赏
  • 举报
回复
我感觉你应该将你要画的这个东西等量化
将这个8字等量化为20个相同大小的正方形
这样你就可以专门写个换正方形的函数,然后就是改变它的摆放位置了
我想,实际上我们日常看到的8应该也类似于我这种思路吧,1个小灯标示一块区域
这样应该会轻松不少
拉卡尼休 2011-11-23
  • 打赏
  • 举报
回复
骂界面库,骂工具,骂操作系统,骂...的人,你们是怎么想的呢,为什么呢?
ybjx111 2011-11-23
  • 打赏
  • 举报
回复

struct B
{
CRect re;
bool bVisible;
};

class A
{
public:
B m_Rect[7];
CPoint pt;
void Draw(CDC* pDC,int Num,CPoint point);
protected:
void InitRect(int Num);
};

void A::Draw(CDC* pDC,int Num,CPoint point)
{
int i;
for(i=0;i<7;i++)
{
if(m_Rect[i].bVisible)
{
pDC->FillSolidRect(&m_Rect[i].re,RGB(0,0,0));
}
}
}

void A::InitRect(int Num)
{
int i;
for(i=0;i<7;i++)
{
m_Rect[i].bVisible=true;
}
m_Rect[0].re.SetRect(pt.x,pt.y,pt.x+3,pt.y+10);
m_Rect[1].re.SetRect(pt.x,pt.y+11,pt.x+3,pt.y+21);
m_Rect[2].re.SetRect(pt.x+4,pt.y+18,pt.x+8,pt.y+21);
m_Rect[3].re.SetRect(pt.x+9,pt.y+11,pt.x+12,pt.y+21);
m_Rect[4].re.SetRect(pt.x+9,pt.y,pt.x+12,pt.y+10);
m_Rect[5].re.SetRect(pt.x+4,pt.y,pt.x+8,pt.y+3);
m_Rect[6].re.SetRect(pt.x+4,pt.y+9,pt.x+8,pt.y+12);
if(Num==0)
{
m_Rect[6].bVisible=false;
}
else if(Num==1)
{
m_Rect[0].bVisible=false;
m_Rect[1].bVisible=false;
m_Rect[2].bVisible=false;
m_Rect[5].bVisible=false;
m_Rect[6].bVisible=false;
}
else if(Num==2)
{
m_Rect[0].bVisible=false;
m_Rect[3].bVisible=false;
}
else if(Num==3)
{
m_Rect[0].bVisible=false;
m_Rect[1].bVisible=false;
}
else if(Num==4)
{
m_Rect[2].bVisible=false;
m_Rect[5].bVisible=false;
}
else if(Num==5)
{
m_Rect[1].bVisible=false;
m_Rect[4].bVisible=false;
}
else if(Num==6)
{
m_Rect[4].bVisible=false;
}
else if(Num==7)
{
m_Rect[0].bVisible=false;
m_Rect[1].bVisible=false;
m_Rect[2].bVisible=false;
m_Rect[6].bVisible=false;
}
else if(Num==8)
{
}
else if(Num==9)
{
m_Rect[1].bVisible=false;
}
else
{
for(i=0;i<7;i++)
{
m_Rect[i].bVisible=false;
}
}
}
taodm 2011-11-22
  • 打赏
  • 举报
回复
楼主可以去买本《重构:改善既有代码的设计》认真学学,也许有答案
波杰克男 2011-11-22
  • 打赏
  • 举报
回复
感觉很是复杂,我对MFC不怎么懂,学习来了
qscool1987 2011-11-22
  • 打赏
  • 举报
回复
可以想象下,这每个小矩形位置不一样的,循环赋值考虑过不行,写个类的话似乎更麻烦了,本来就在对话框类里面,有点相同的地方就是每8个小矩形组成一个8,然后其他七个小矩形根据第一个小矩形的位置来确定坐标,但这也不能用循环赋值,因为不是固定的加多少减多少
h415640525h 2011-11-22
  • 打赏
  • 举报
回复
你都知道怎么算了,那就是你自己找规律的问题了
我有个拙劣的办法,(还是需要根据你自己的具体情况找到对应的方法才是王道)
你写个赋值函数(rect1,rect2,value1,value2,value3,value4)
这样你就可以只写16个函数调用了
自由建客 2011-11-22
  • 打赏
  • 举报
回复
是我我就做个数码管类了!
xiejijun_05 2011-11-22
  • 打赏
  • 举报
回复

m_rect[1].left = m_rect[0].left - 5;

// 把后面的常数存放到数组里面,然后用循环赋值.

Ulfsaar 2011-11-22
  • 打赏
  • 举报
回复
找出形式相同的区段,封装成函数,然后就是函数的调用了

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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