/* -------------------------------------------------------------------
MyWindows.c -- 基本窗口模型
《Windows 程序设计(SDK)》视频教程
--------------------------------------------------------------------*/
#include <windows.h>
#include <Windowsx.h>
#include "xinxin.h"
#define NUM 10000
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("MyWindows");//定义一个窗口类名字
HWND hwnd; //窗口句柄
MSG msg; //消息结构
WNDCLASS wndclass; //窗口类结构 //声明
wndclass.style = CS_HREDRAW | CS_VREDRAW; //指定窗口类型
wndclass.lpfnWndProc = WndProc; //指定窗口过程
wndclass.cbClsExtra = 0; //预留的额外空间
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance; //应用程序的实例句柄
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);//为所有基于该窗口类设定一个图标
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //为所有基于该窗口类设定一个鼠标指针
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);//指定窗口背景色
wndclass.lpszMenuName = NULL;//指定窗口菜单
wndclass.lpszClassName = szAppName;//指定窗口类名
if (!RegisterClass(&wndclass))//注册
{
MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName, //窗口类名称lpClassName
TEXT("鱼C工作室"), //窗口标题lpWindowName
WS_OVERLAPPEDWINDOW, //窗口风格dwStyle
CW_USEDEFAULT, //初始x坐标 CW CreatWindow创建窗口选项
CW_USEDEFAULT, //y
CW_USEDEFAULT, //初始x方向尺寸nWidth
CW_USEDEFAULT, //y nHeight
NULL, //父窗口句柄hWndParent
NULL, //窗口菜单句柄hMenu
hInstance, //程序实例句柄hInstance
NULL); //创建参数lpParam
ShowWindow(hwnd, iCmdShow); //显示窗口
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)
{
HDC hdc; //DC:Device Context 设备环境
PAINTSTRUCT ps;
RECT rect;
int i=0;
static POINT apt[NUM+1];
static int x=0,y=0;
static int shiwux,shiwuy;
static int length=1;
static int cxClient,cyClient;
static RECT Rect[2];
static int pd=1;//判断
int sum=0;
switch (message)
{
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
return 0;
case WM_KEYDOWN:
switch(wParam)
{
case VK_UP:y=y-15;apt[0].y=y;InvalidateRect(hwnd,NULL,TRUE);if(PtInRect(Rect,apt[0])){ pd=1;}break;
case VK_DOWN:y=y+15;apt[0].y=y;InvalidateRect(hwnd,NULL,TRUE);if(PtInRect(Rect,apt[0])){ pd=1;}break;
case VK_LEFT:x=x-15;apt[0].x=x;InvalidateRect(hwnd,NULL,TRUE);if(PtInRect(Rect,apt[0])){ pd=1;}break;
case VK_RIGHT:x=x+15;apt[0].x=x;InvalidateRect(hwnd,NULL,TRUE);if(PtInRect(Rect,apt[0])){ pd=1;}break;//键盘移动
}
return 0;
case WM_PAINT: //绘制窗口
hdc = BeginPaint(hwnd, &ps); //绘制的初始化 ps:paintstruct存放绘画有关的参数
GetClientRect(hwnd, &rect); //获得客户区的位置坐标信息
if(pd==1)
{
shiwux=rand()%(cxClient-15);
shiwuy=rand()%(cyClient-15);
pd=0;
length++;
}
SetRect(Rect,shiwux,shiwuy,shiwux+15,shiwuy+15);
FillRect(hdc,Rect,CreateSolidBrush(RGB(0,0,255)));//食物,填充随机矩形
SelectObject(hdc,CreatePen(PS_SOLID,1,RGB(255,0,255)));
for(i=length;i>0;i--)
{
apt[i].x=apt[i-1].x;
apt[i].y=apt[i-1].y;
}//坐标顺移,蛇身体变长
for(i=0;i<length;i++)
{
xin(hdc,1,1,apt[i].x,apt[i].y);
}//画心型的贪吃蛇
EndPaint(hwnd, &ps);
return 0;
case WM_MOUSEMOVE:
x=GET_X_LPARAM(lParam);
y=GET_Y_LPARAM(lParam);
apt[0].x=x;
apt[0].y=y;//获取鼠标坐标
InvalidateRect(hwnd,NULL,TRUE);
if(PtInRect(Rect,apt[0]))//判断坐标是否在矩形中
{
pd=1;
}
return 0;
case WM_LBUTTONUP:
return 0;
case WM_DESTROY: //窗口关闭
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);//windows处理其他消息
}