Win32 API로 "움직이는 공" 프로그램을 만들어봤습니다. 삽질 조금 했더니 재미있는 프로그램이 나오군요.
//ball.cpp
#include <windows.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
LPSTR lpszClass="ball";
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance
,LPSTR lpszCmdParam,int nCmdShow)
{
HWND hWnd;
MSG Message;
WNDCLASS WndClass;
g_hInst=hInstance;
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
WndClass.hInstance=hInstance;
WndClass.lpfnWndProc=(WNDPROC)WndProc;
WndClass.lpszClassName=lpszClass;
WndClass.lpszMenuName=NULL;
WndClass.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&WndClass);
hWnd=CreateWindow(lpszClass,"움직이는공",WS_OVERLAPPEDWINDOW ^WS_MINIMIZEBOX^ WS_MAXIMIZEBOX^WS_THICKFRAME,
CW_USEDEFAULT,CW_USEDEFAULT,550,800,
NULL,(HMENU)NULL,hInstance,NULL);
ShowWindow(hWnd,nCmdShow);
while(GetMessage(&Message,0,0,0)) {
TranslateMessage(&Message);
DispatchMessage(&Message);
}
return Message.wParam;
}
#include "resource.h"
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
HDC hdc,MemDC;
PAINTSTRUCT ps;
HBITMAP MyBitmap, OldBitmap;
static int x, y, vx, vy;
static RECT rt;
const int speed = 5;
switch(iMessage) {
case WM_CREATE:
MyBitmap=LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP1));
x = 0, y = 0;
vx = speed;
vy = speed;
SetTimer(hWnd, 1, 100, NULL);
return 0;
case WM_TIMER:
GetClientRect(hWnd, &rt);
x +=vx;
y +=vy;
if(x<0)
vx = speed;
if(y<0)
vy = speed;
if(x+100>rt.right)
vx = -speed;
if(y+100>700)
vy = -speed;
InvalidateRect(hWnd,NULL,TRUE);
return 0;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
SetTextAlign(hdc, TA_CENTER);
TextOut(hdc,280,730,"움직이는 공입니다 - 제작자 : 성대현",35);
Rectangle(hdc,0,0,rt.right,705);
MemDC=CreateCompatibleDC(hdc);
MyBitmap=LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP1));
OldBitmap=(HBITMAP)SelectObject(MemDC, MyBitmap);
BitBlt(hdc, x,y,100,100,MemDC,0,0,SRCCOPY);
SelectObject(MemDC,OldBitmap);
DeleteObject(MyBitmap);
DeleteDC(MemDC);
EndPaint(hWnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}