#include "stdafx.h"
#include "snow.h"
#include "stdio.h"
//gdi+蚚善腔謗跺曹講
GdiplusStartupInput m_gdiplusStartupInput;
ULONG_PTR m_pGdiToken;
Image *image;
Image *src;
//全�姥眻靾翰襲i
HINSTANCE hInst;
HBITMAP map;
HDC hdc,mdc,buffer;
HWND hWnd;
DWORD tPre,tNow;
int x=0,y=0;
int fps=0;
int frame=0;
DWORD tCheck=0;
snow *snowsecen;
char str[40];
//函式宣告
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void MyPaint(HDC hdc);
//****主程式**************************************
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
//場宎趙
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
//秏洘悜遠
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0,0 ,PM_REMOVE) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
tNow = GetTickCount();
if(tNow-tPre >= 15)
{
MyPaint(hdc);
if(GetAsyncKeyState(VK_UP)&0x8000f)
{
y -= 10;
if(y < 0)
y = 0;
}
if(GetAsyncKeyState(VK_DOWN)&0x8000f)
{
y += 10;
if(y > 660)
y = 660;
}
if(GetAsyncKeyState(VK_LEFT)&0x8000f)
{
x -= 10;
if(x < 0)
x = 0;
}
if(GetAsyncKeyState(VK_RIGHT)&0x8000f)
{
x += 10;
if(x > 910)
x = 910;
}
}
}
}
return msg.wParam;
}
//****蛁聊敦諳濬*************************
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "canvas";
wcex.hIconSm = NULL;
return RegisterClassEx(&wcex);
}
//****初始函式*************************************
// 建場宎趙
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HBITMAP bmp;
hInst = hInstance;
hWnd = CreateWindow("canvas", "2D蚔牁遺殤芵隴膝曹髦磁" , WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
MoveWindow(hWnd,0,0,1024,768,true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
hdc = GetDC(hWnd);
mdc = CreateCompatibleDC(hdc);
buffer=CreateCompatibleDC(hdc);
bmp=CreateCompatibleBitmap(hdc,1024,768);
//蚾婥gdi+
GdiplusStartup(&m_pGdiToken,&m_gdiplusStartupInput,NULL);
//
snowsecen=new snow();
src=new Image(L"p.png");
image=new Image(L"f123.png");
map = (HBITMAP)LoadImage(NULL,"black.bmp",IMAGE_BITMAP,1024,768,LR_LOADFROMFILE);
SelectObject(mdc,map);
SelectObject(buffer,bmp);
MyPaint(hdc);
return TRUE;
}
//****自餅芞*********************************
void MyPaint(HDC hdc)
{
frame++; //數呾痋杅
if(tNow - tCheck >= 1000)
{
fps = frame;
frame = 0;
tCheck = tNow;
}
BitBlt(buffer,0,0,1024,768,mdc,0,0,SRCCOPY);
Graphics imgraphics(buffer);
imgraphics.DrawImage(image,0,0,200,200);
imgraphics.DrawImage(src,x,y,50,50);
snowsecen->DrawSnow(buffer,1024,800);
SetBkMode(buffer,TRANSPARENT);
SetTextColor(buffer,RGB(255,0,0));
sprintf(str,"藩 %d 痋藩鏃面 ",fps);
TextOut(buffer,100,100,str,strlen(str));
BitBlt(hdc,0,0,1024,768,buffer,0,0,SRCCOPY);
tPre = GetTickCount();
}
//****訊息處理函式***********************************
// 隙覃
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_KEYDOWN: //按下鍵盤訊息
break;
case WM_DESTROY: //視窗結束訊息
DeleteDC(mdc);
DeleteObject(map);
ReleaseDC(hWnd,hdc);
delete snowsecen;
PostQuitMessage(0);
break;
default: //其它訊息
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
- 1
- 2
前往页