背景:宽度46,高度392/7=56
动物:宽度78/2=39 高度1638/42=39
画图:
步骤一:建立与窗口DC兼容的内存DC
hdc=GetDC(hWnd);
mdc=CreateCompatibleDC(hdc);
步骤二:加载位图:
hbmp=(HBITMAP)LoadImage(NULL,"3DFrames.bmp",IMAGE_BITMAP,46,392,LR_LOADFROMFILE);
步骤三:画图
SelectObject(mdc,hbmp);
BitBlt(hdc,0,0,BGWIDTH,BGHEIGHT,mdc,0,BGHEIGHT,SRCCOPY);
禁止缩放窗口:
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW& ~WS_THICKFRAME,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
// & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX,//禁止最大化最小化
调整窗口大小:
RECT rcClient,rcWind;
POINT ptDiff;
GetClientRect(hWnd,&rcClient);
GetWindowRect(hWnd,&rcWind);
ptDiff.x=(rcWind.right-rcWind.left)-rcClient.right;
ptDiff.y=(rcWind.bottom-rcWind.top)-rcClient.bottom;
MoveWindow(hWnd,100,100,10*BGWIDTH+ptDiff.x,8*BGHEIGHT+ptDiff.y,TRUE);
画动物:
BitBlt(hdc,0*ANIMALWIDTH+3,0*ANIMALHEIGHT+8,ANIMALWIDTH,ANIMALHEIGHT,mdc,ANIMALWIDTH,ANIMALHEIGHT,SRCAND);
BitBlt(hdc,0*ANIMALWIDTH+3,0*ANIMALHEIGHT+8,ANIMALWIDTH,ANIMALHEIGHT,mdc,0,ANIMALHEIGHT,SRCPAINT);
MyPaint示例:
void MyPaint()
{
SelectObject(mdc,hbmp);
int x,y;
for(y=0;y<8;y=y++)
{
for(x=0;x<10;x++)
{
BitBlt(hdc,x*BGWIDTH,y*BGHEIGHT,BGWIDTH,BGHEIGHT,mdc,0,BGHEIGHT,SRCCOPY);
}
}
SelectObject(mdc,hanimal);
for(y=0;y<8;y=y++)
{
for(x=0;x<10;x++)
{
BitBlt(hdc,x*BGWIDTH+3,y*BGHEIGHT+8,ANIMALWIDTH,ANIMALHEIGHT,mdc,ANIMALWIDTH,ANIMALHEIGHT,SRCAND);
BitBlt(hdc,x*BGWIDTH+3,y*BGHEIGHT+8,ANIMALWIDTH,ANIMALHEIGHT,mdc,0,ANIMALHEIGHT,SRCPAINT);
}
}
}
鼠标:
case WM_LBUTTONDOWN:
newpos.x=LOWORD(lParam)/BGWIDTH;
newpos.y=HIWORD(lParam)/BGHEIGHT;
if(check==FALSE)
{
oldpos=newpos;
}
else
{ if(map[oldpos.y*10+oldpos.x]==map[newpos.y*10+newpos.x]&&IsLink(oldpos.x,oldpos.y,newpos.x,newpos.y))
{
if(oldpos.x!=newpos.x||oldpos.y!=newpos.y)
map[oldpos.y*10+oldpos.x]=map[newpos.y*10+newpos.x]=-1;
MyPaint();
}
}
check=!check;
break;
核心算法:
BOOL Y1_Link_Y2(int oldx, int oldy, int newy)
{
if(oldy>newy)
{
//数据交换
int n=oldy;
oldy=newy;
newy=n;
}
//直通
for(int i=oldy+1;i<=newy;i++)
{
if(i==newy)
{
return TRUE;
}
if(map[i*m_nCol+oldx]!=-1)
break;
}
return FALSE;
}
//y相同,x方向直连
BOOL X1_Link_X2(int oldx, int oldy, int newx)
{
if(oldx>newx)
{
int x=oldx;
oldx=newx;
newx=x;
}
//直通
for(int i=oldx+1;i<=newx;i++)
{
if(i==newx)
{
return TRUE;
}
if(map[oldy*m_nCol+i]!=-1)
break;
}
return FALSE;
}
BOOL OneCornerLink(int oldx, int oldy, int newx, int newy)
{
int temp1x,temp1y,temp2x,temp2y;
temp1x=newx;
temp1y=oldy;
temp2x=oldx;
temp2y=newy;
if(map[temp1y*m_nCol+temp1x]==-1)
{
if(X1_Link_X2(oldx,oldy,temp1x)&&Y1_Link_Y2(temp1x,temp1y,newy))
{
return TRUE;
}
}
if(map[temp2y*m_nCol+temp2x]==-1)
{
if(X1_Link_X2(temp2x,temp2y,newx)&&Y1_Link_Y2(oldx,oldy,temp2y))
{
return TRUE;
}
}
return FALSE;
}
BOOL TwoCornerLink(int oldx, int oldy, int newx, int newy)
{
//右
for(int x=oldx+1;x { if(map[oldy*m_nCol+x]!=-1) break; if(OneCornerLink(x,oldy,newx,newy)) return TRUE; } //左 for(int x=oldx-1;x>-1;x--) { if(map[oldy*m_nCol+x]!=-1) break; if(OneCornerLink(x,oldy,newx,newy)) return TRUE; } //向上 for(int y=oldy-1;y>-1;y--) { if(map[y*m_nCol+oldx]!=-1) break; if(OneCornerLink(oldx,y,newx,newy)) return TRUE; } //下 for(int y=oldy+1;y { if(map[y*m_nCol+oldx]!=-1) break; if(OneCornerLink(oldx,y,newx,newy)) return TRUE; } return FALSE; } BOOL IsLink(int oldx, int oldy, int newx, int newy) { //Y直连方式 if(oldx==newx) { if(Y1_Link_Y2(oldx,oldy,newy)) return TRUE; } //X直连方式 else if(oldy==newy) { if(X1_Link_X2(oldx,oldy,newx)) return TRUE; } //一个转弯直角的联通方式 if(OneCornerLink(oldx,oldy,newx,newy)) { return TRUE; } //两个转弯直角的联通方式 else if(TwoCornerLink(oldx,oldy,newx,newy)) { return TRUE; } return FALSE; }