#include <windows.h>
#include "Tower.h"
#include "Hanoi.h"
HWND hwnd;
HWND hButton1 = NULL;
HWND hButton2 = NULL;
HWND hEdit1 = NULL;
HWND hEdit2 = NULL;
int num=0;
int speed=0;
int move_detal=1;
RECT update_rect;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
{
MSG Msg;
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,"Icon");
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName="汉诺塔";
wndclass.lpszMenuName=NULL;
wndclass.style=0;
RegisterClass(&wndclass);
hwnd=CreateWindow(
"汉诺塔",
"汉诺塔",
WS_OVERLAPPEDWINDOW|WS_BORDER,
200,
100,
900,
700,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);
while (GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
BOOL flag=0;
HDC hdc;
HPEN hPen;
HBRUSH hBrush;
HFONT hf1,hf2,hf3;
POINT center;
Hanoi hanoi;
TEXTMETRIC tm;
PAINTSTRUCT ps;
char pch1[10];
char pch2[10];
char ch1[]="汉诺塔";
char ch2[]="汉诺塔的层数:";
char ch3[]="速度:";
char ch4[]="(1--TO--10)";
char ch5[]="(1--TO--20)";
char ch6[]="本程序开发者: 湖南师范大学数学与计算机科学学院06级计算机专业----肖先敏";
center.x=170;
center.y=600;
Tower tower1(center,130,200,0);
center.x=450;
Tower tower2(center,130,200,0);
center.x=750;
Tower tower3(center,130,200,0);
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_ACTIVATE: // 当窗口被激活时,将焦点设置在文本框上
::SetFocus(hEdit1);
break;
case WM_CREATE:
hButton1 = CreateWindow("button", "开始", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
720, 100, 50, 30, hwnd, HMENU(100), NULL, NULL);
hButton2 = CreateWindow("button", "重来", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
800, 100, 50, 30, hwnd, HMENU(100), NULL, NULL);
hEdit1 = CreateWindow("edit", NULL, WS_CHILD | WS_VISIBLE|WS_BORDER ,
150, 100, 50, 20, hwnd, NULL, NULL, NULL);
hEdit2 = CreateWindow("edit", NULL, WS_CHILD | WS_VISIBLE|WS_BORDER ,
450, 100, 50, 20, hwnd, NULL, NULL, NULL);
break;
case WM_COMMAND:
if (((HWND)lParam == hEdit2 ))
{
GetWindowText(hEdit1,pch1,10);
num=atoi(pch1);
tower1.Insert(num);
hdc=::GetDC(hwnd);
hPen=(HPEN)GetStockObject(PS_SOLID);
SelectObject(hdc,hPen);
tower1.DrawTower(hdc);
tower2.DrawTower(hdc);
tower3.DrawTower(hdc);
DeleteObject(hPen);
}
else if (((HWND)lParam == hButton1 )&&( HIWORD(wParam) == BN_CLICKED))
{
GetWindowText(hEdit2,pch2,10);
if(pch2[0]==NULL)
{
GetWindowText(hEdit1,pch1,10);
num=atoi(pch1);
tower1.Insert(num);
}
else
{
speed=atoi(pch2);
if(speed<6)
move_detal=1;
else
move_detal=2;
speed=10-speed;
hanoi.SetMoveDetal(move_detal);
hanoi.SetSpeed(speed);
}
tower1.Insert(num);
hdc=::GetDC(hwnd);
hPen=(HPEN)GetStockObject(PS_SOLID);
SelectObject(hdc,hPen);
tower1.DrawTower(hdc);
tower2.DrawTower(hdc);
tower3.DrawTower(hdc);
DeleteObject(hPen);
if (num!=0)
hanoi.Hanoi_begin(hdc,tower1,tower2,tower3,num);
ReleaseDC(hwnd,hdc);
}
else if (((HWND)lParam == hButton2)&&( HIWORD(wParam) == BN_CLICKED))
{
hdc=::GetDC(hwnd);
hanoi.SetMoveDetal(move_detal);
hanoi.SetSpeed(speed);
tower1.Insert(num);
tower2.Insert(0);
tower3.Insert(0);
update_rect.bottom=680;
update_rect.left=300;
update_rect.right=890;
update_rect.top=350;
hPen=CreatePen(PS_NULL,0,0xffffff);
hBrush=(HBRUSH)GetStockObject(WHITE_BRUSH);
SelectObject(hdc,hPen);
SelectObject(hdc,hBrush);
::Rectangle(hdc,update_rect.left,update_rect.top,update_rect.right,update_rect.bottom);
DeleteObject(hPen);
DeleteObject(hBrush);
hPen=(HPEN)GetStockObject(PS_SOLID);
SelectObject(hdc,hPen);
tower1.DrawTower(hdc);
tower2.DrawTower(hdc);
tower3.DrawTower(hdc);
DeleteObject(hPen);
if (num!=0)
hanoi.Hanoi_begin(hdc,tower1,tower2,tower3,num);
ReleaseDC(hwnd,hdc);
}
break;
case WM_PAINT:
hf1=CreateFont(
50,
0,
0,
0,
FW_HEAVY,
0,
0,
0,
GB2312_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
VARIABLE_PITCH,
"粗体字"
);
hf2=CreateFont(
20,
0,
0,
0,
400,
0,
0,
0,
GB2312_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
VARIABLE_PITCH,
"粗体字"
);
hf3=CreateFont(
15,
0,
0,
0,
400,
0,
0,
0,
GB2312_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,
VARIABLE_PITCH,
"粗体字"
);
hdc=BeginPaint(hwnd,&ps);
SetTextColor(hdc,RGB(255,0,0));
SelectObject(hdc,hf1);
GetTextMetrics(hdc,&tm);
TextOut(hdc,350,25,ch1,strlen(ch1));
SetTextColor(hdc,RGB(0,0,0));
SelectObject(hdc,hf2);
GetTextMetrics(hdc,&tm);
TextOut(hdc,20,100,ch2,strlen(ch2));
SetTextColor(hdc,RGB(0,0,0));
SelectObject(hdc,hf2);
GetTextMetrics(hdc,&tm);
TextOut(hdc,400,100,ch3,strlen(ch3));
SetTextColor(hdc,RGB(0,0,0));
SelectObject(hdc,hf2);
GetTextMetrics(hdc,&tm);
TextOut(hdc,530,100,ch4,strlen(ch4));
SetTextColor(hdc,RGB(0,0,0));
SelectObject(hdc,hf2);
GetTextMetrics(hdc,&tm);
TextOut(hdc,230,100,ch5,strlen(ch5));
SetTextColor(hdc,RGB(0,0,0));
SelectObject(hdc,hf3);
GetTextMetrics(hdc,&tm);
TextOut(hdc,30,650,ch6,strlen(ch6));
EndPaint(hwnd,&ps);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return(0);
}
评论0
最新资源