⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hanio.cpp

📁 这是汉诺塔一个演示程序
💻 CPP
字号:
#include "Hanio.h"

Hanio Main;
Hanio Demo;
BOOL bAuto;
int iStart;
int iEnd;
BOOL bSel=FALSE;
POINT ptStart;

RECT SelRect;
RECT Temp1;	
RECT Temp2;

int WINAPI WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	MSG msg;

	if(!MyRegisterClass(hInstance))
	{
		MessageBeep(0);
		return FALSE;
	}

	if (!InitInstance (hInstance, nCmdShow)) 
	{
		MessageBeep(0);
		return FALSE;
	}

	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{	
	POINT pt;
	HDC hdc;
	CMap Map;
	int i;
	switch (message) 
	{
		case WM_CREATE:
			bAuto=FALSE;
			break;
		case WM_PAINT:
			Draw();
			break;
		case WM_LBUTTONDOWN:
			ptStart.x=LOWORD(lParam);
			ptStart.y=HIWORD(lParam);
			Map=Main.Record[Main.iStep];
			for(i=0;i<3;i++)
			{
				if(PointInRect(ptStart,Map.Rect[i][Map.iCount[i]-1]))
				{
					iStart=i;
					bSel=TRUE;
					SelRect=Map.Rect[i][Map.iCount[i]-1];
					Temp1=SelRect;
					break;
				}			
			}
			break;
		case WM_MOUSEMOVE:
			if(!bSel)
				break;
			pt.x=LOWORD(lParam);
			pt.y=HIWORD(lParam);
			hdc=GetDC(hWnd);
			SetROP2(hdc,R2_NOTXORPEN);
			Rectangle(hdc,
				Temp1.left,
				Temp1.top,
				Temp1.right,
				Temp1.bottom);
			Temp2=Temp1;
			Temp2.left=SelRect.left+(pt.x-ptStart.x);
			Temp2.right=SelRect.right+(pt.x-ptStart.x);
			Temp2.top=SelRect.top+(pt.y-ptStart.y);
			Temp2.bottom=SelRect.bottom+(pt.y-ptStart.y);
			Rectangle(hdc,
				Temp2.left,
				Temp2.top,
				Temp2.right,
				Temp2.bottom);
			Temp1=Temp2;
			break;
		case WM_LBUTTONUP:
			if(!bSel)
				break;

			bSel=FALSE;
			pt.x=LOWORD(lParam);
			pt.y=HIWORD(lParam);
			iEnd=PointToNum(pt);
			Main.Move(iStart,iEnd);
			if(Main.Record[Main.iStep].iCount[2]==NUM)
			{
				MessageBox(hWnd,"恭喜你,成功了",szTitle,MB_OK);
				SendMessage(hWnd,WM_KEYDOWN,VK_F5,0);
			}
			SendMessage(hWnd,WM_PAINT,0,0);
			break;
		case WM_KEYDOWN:			
			if(wParam==VK_F3)
			{
				bAuto=TRUE;
				Demo=Hanio();
				Demo.AutoMove(0,1,2,NUM);
				bAuto=FALSE;
				SendMessage(hWnd,WM_PAINT,0,0);
				break;
			}
			if(wParam==VK_F2)
			{
				Main.Undo();
				SendMessage(hWnd,WM_PAINT,0,0);
				break;
			}
			if(wParam==VK_F4&&NUM>3)
			{
				NUM--;
				Main=Hanio();
				Demo=Hanio();
				SendMessage(hWnd,WM_PAINT,0,0);
				break;
			}
			if(wParam==VK_F5&&NUM<MAXLEVEL)
			{
				NUM++;
				Main=Hanio();
				Demo=Hanio();
				SendMessage(hWnd,WM_PAINT,0,0);
				break;
			}
			break;
		case WM_CHAR:
			if(wParam==(unsigned char)ESC)
			{
				PostQuitMessage(0);
			}
			break;
		case WM_DESTROY:			
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }

   return 0;
}

BOOL MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASS wcex;	
	
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;	
	wcex.hbrBackground	= (HBRUSH)(GetStockObject(WHITE_BRUSH));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hIcon			= LoadIcon(hInstance,(LPCTSTR)IDI_ICON1);
	wcex.hInstance		= hInstance;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.lpszClassName	= szWndClass;
	wcex.lpszMenuName	= NULL;
	wcex.style			= CS_HREDRAW | CS_VREDRAW;	

	return RegisterClass(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	hWnd = CreateWindow(szWndClass, szTitle, WS_OVERLAPPEDWINDOW&~WS_MAXIMIZEBOX&~WS_THICKFRAME,
		CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

	if (!hWnd)
	{
		return FALSE;
	}

	int xMax=GetSystemMetrics(SM_CXSCREEN);
	int yMax=GetSystemMetrics(SM_CYSCREEN);

	LPRECT lpRect=new RECT;
	GetWindowRect(hWnd,lpRect);

	LPPOINT lpLeftTop=new POINT;
	lpLeftTop->x=lpRect->left;
	lpLeftTop->y=lpRect->top;
	ScreenToClient(hWnd,lpLeftTop);

	int xWnd=X-lpLeftTop->x*2;
	int yWnd=Y-lpLeftTop->y-lpLeftTop->x;

	int xStart=(xMax-xWnd)/2;
	int yStart=(yMax-yWnd)/2;

	MoveWindow(hWnd,xStart,yStart,xWnd,yWnd,TRUE);
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);	

	return TRUE;
}

void Draw()
{
	HDC			hdc;
	PAINTSTRUCT ps;
	BeginPaint(hWnd,&ps);
	hdc=GetDC(hWnd);
	Rectangle(hdc,-1,-1,X+1,Y+1);

	TextOut(hdc,Dx,Y-180+Dx,szAuthor,strlen(szAuthor));
	TextOut(hdc,Dx,Y-180+2*Dx,szMail,strlen(szMail));
	TextOut(hdc,Dx,Y-180+3*Dx,szMsg1,strlen(szMsg1));
	TextOut(hdc,Dx,Y-180+4*Dx,szMsg2,strlen(szMsg2));

	if(!bAuto)
	{
		wsprintf(szCurMsg,"当前 %d 关,第 %d 步",NUM,Main.iStep);
		TextOut(hdc,250+Dx,Y-180+2*Dx,szCurMsg,strlen(szCurMsg));
		Main.OnDraw(hdc);
	}
	else
	{
		wsprintf(szCurMsg,"当前 %d 关,第 %d 步",NUM,Demo.iStep);
		TextOut(hdc,250+Dx,Y-180+2*Dx,szCurMsg,strlen(szCurMsg));
		Demo.OnDraw(hdc);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -