📄 spirit.cpp
字号:
#include <windows.h>
int iCarX=-200,iCarY=200;
HINSTANCE hInstance;
HDC hdcSrc,hdcBack,hdcMem;
HBITMAP hBitmapSrc,hBitmapBack,hBitmapMem;
/*
void CALLBACK TimerProc(
HWND hwnd,
UINT uMsg,
UINT idEvent,
DWORD dwTime )
{
MessageBox(NULL,"Hello!","Caption",0);}
*/
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int Render(HDC hdc);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Spirit") ;
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 ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow( szAppName, TEXT ("Spirit Demo 精灵动画演示"), WS_OVERLAPPEDWINDOW ^ WS_MAXIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, 388, 517, NULL, NULL, hInstance, NULL) ;
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;
PAINTSTRUCT ps;
RECT rect;
switch (message){
case WM_CREATE:
hInstance=((LPCREATESTRUCT)lParam)->hInstance;
hdc=GetDC(hwnd);
hdcSrc=CreateCompatibleDC(hdc); //源图像DC
hdcBack=CreateCompatibleDC(hdc); //目标图像DC
hdcMem=CreateCompatibleDC(hdc); //内存DC
hBitmapSrc=LoadBitmap(hInstance,TEXT("CARS")); //读图像
hBitmapBack=LoadBitmap(hInstance,TEXT("SCENE"));
hBitmapMem=CreateCompatibleBitmap(hdc,388,517); //为内存DC建立一个位图
SelectObject(hdcSrc,hBitmapSrc); //将位图分别选入DC
SelectObject(hdcBack,hBitmapBack);
SelectObject(hdcMem,hBitmapMem);
SetTimer(hwnd,1,10,NULL); //定时器,间隔10ms
return 0 ;
case WM_PAINT:
// Obtain the window's client rectangle
/*
GetClientRect(hwnd, &rect);
// THE FIX: by setting the background mode
// to transparent, the region is the text itself
// SetBkMode(hdc, TRANSPARENT);
// Bracket begin a path
BeginPath(hdc);
// Send some text out into the world
TextOut(hdc, rect.left, rect.top, "Defenestration can be hazardous", 4);
// Bracket end a path
EndPath(hdc);
// Derive a region from that path
SelectClipPath(hdc, RGN_AND);
// This generates the same result as SelectClipPath()
// SelectClipRgn(hdc, PathToRegion(hdc));
// Fill the region with grayness
FillRect(hdc, &rect, (HBRUSH)GetStockObject(GRAY_BRUSH));
*/
hdc=BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
Render(hdc); //我们的渲染函数
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_TIMER:
iCarX+=1; //移动
if (iCarX>389) {
KillTimer(hwnd,1);
return 0;
}
rect.right=300; rect.bottom=300;
InvalidateRect(hwnd,NULL,FALSE); //这两句使窗口内容更新,即调用我们的渲染函数
UpdateWindow(hwnd);
return 0;
case WM_DESTROY:
DeleteDC(hdcSrc);
DeleteDC(hdcBack);
DeleteObject(hBitmapSrc);
DeleteObject(hBitmapBack);
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
int Render(HDC hdc){
BitBlt(hdcMem,0,0,388,517,hdcBack,0,0,SRCCOPY); //将背景拷贝到内存DC上
BitBlt(hdcMem,iCarX,iCarY,220,133,hdcSrc,220,0,SRCAND); //处理掩码图
BitBlt(hdcMem,iCarX,iCarY,220,133,hdcSrc,0,0,SRCPAINT); //处理目标图像
// TextOut(hdcMem,100,100,"Hello!",6);
BitBlt(hdc,0,0,388,517,hdcMem,0,0,SRCCOPY); //画到屏幕上的窗口的DC
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -