for(int i=1;i<=25;i++)
{
switch(i)
{
case 1:
BitBlt(hdc,0,0,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 2:
BitBlt(hdc,120,0,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 3:
BitBlt(hdc,240,0,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 4:
BitBlt(hdc,360,0,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 5:
BitBlt(hdc,480,0,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 6:
BitBlt(hdc,0,96,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 7:
BitBlt(hdc,120,96,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 8:
BitBlt(hdc,240,96,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 9:
BitBlt(hdc,360,96,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 10:
BitBlt(hdc,480,96,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 11:
BitBlt(hdc,0,192,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 12:
BitBlt(hdc,120,192,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 13:
BitBlt(hdc,240,192,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 14:
BitBlt(hdc,360,192,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 15:
BitBlt(hdc,480,192,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 16:
BitBlt(hdc,0,288,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 17:
BitBlt(hdc,120,288,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 18:
BitBlt(hdc,240,288,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 19:
BitBlt(hdc,360,288,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 20:
BitBlt(hdc,480,288,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 21:
BitBlt(hdc,0,384,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 22:
BitBlt(hdc,120,384,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 23:
BitBlt(hdc,240,384,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 24:
BitBlt(hdc,360,384,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
case 25:
BitBlt(hdc,480,384,120,96,mdc,point[i].x,point[i].y,SRCCOPY);
break;
default:
MessageBox(NULL,"贴图序列号错误!","",NULL);
}
-----------------
好比这段代码吧,大量的重复,而且是手工硬编码,代码灵活性很差。(因为代码都写死了,一旦修改一下贴图快大小,不知道程序中要改多少个地方);
那个default分支显然也是没用的。
看上面这段代码,可见所有case中的代码都是那么一句BitBlt,唯一不同的是目标矩形的起始点(TopLeft)在变化而已。实际上完全可以改成循环。
for(j=0;j<5;j++)
for(i=0;i<5;i++)
BitBlt(hdc, i*120, j*96, 120, 96, mdc, point[j*5+i+1].x, point[j*5+i+1].y, SRCCOPY);
关于贴图,msdn在讲c#和C++互操作的时候有一个很经典的例子,大概叫做tilepuzzle吧。底层算法是用c++写的,
里面用自动求解的过程,用4种算法各开一个线程寻解。
[[it] 本帖最后由 hoodlum1980 于 2008-10-23 12:01 编辑 [/it]]