home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS - Coast to Coast
/
simteldosarchivecoasttocoast2.iso
/
wpj_mag
/
wpjv1n8.zip
/
GDI.ZIP
/
TBALL.ZIP
/
TBALL.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-11
|
8KB
|
372 lines
#include <windows.h> /* required for all Windows applications */
#include "tball.h" /* specific to this program */
int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg; /* message */
if (!hPrevInstance) /* Other instances of app running? */
if (!InitApplication(hInstance)) /* Initialize shared things */
return (FALSE); /* Exits if unable to initialize */
/* Perform initializations that apply to a specific instance */
if (!InitInstance(hInstance, nCmdShow))
return (FALSE);
/* Acquire and dispatch messages until a WM_QUIT message is received. */
while (GetMessage(&msg, /* message structure */
NULL, /* handle of window receiving the message */
NULL, /* lowest message to examine */
NULL)) /* highest message to examine */
{
TranslateMessage(&msg); /* Translates virtual key codes */
DispatchMessage(&msg); /* Dispatches message to window */
}
return (msg.wParam); /* Returns the value from PostQuitMessage */
}
BOOL InitApplication(HANDLE hInstance)
{
WNDCLASS wc;
/* Fill in window class structure with parameters that describe the
main window. */
wc.style = NULL; /* Class style(s) */
wc.lpfnWndProc = MainWndProc; /* Function to retrieve messages for */
/* windows of this class */
wc.cbClsExtra = 0; /* No per-class extra data */
wc.cbWndExtra = 0; /* No per-window extra data */
wc.hInstance = hInstance; /* Application that owns the class */
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = "BallMenu"; /* Name of menu resource in .RC file. */
wc.lpszClassName = "BallWClass"; /* Name used in call to CreateWindow. */
/* Register the window class and return success/failure code. */
return (RegisterClass(&wc));
}
BOOL InitInstance(HANDLE hInstance, int nCmdShow)
{
/* Save the instance handle in static variable, which will be used in
many subsequent calls from this application to Windows. */
hInst = hInstance;
/* Create a main window for this application instance. */
hWnd = CreateWindow(
"BallWClass", /* See RegisterClass() call */
"Timer Message Ball", /* Text for window title bar */
WS_OVERLAPPEDWINDOW, /* Window style */
CW_USEDEFAULT, /* Default horizontal position */
CW_USEDEFAULT, /* Default vertical position */
CW_USEDEFAULT, /* Default width */
CW_USEDEFAULT, /* Default height */
NULL, /* Overlapped windows have no parent */
NULL, /* Use the window class menu */
hInstance, /* This instance owns this window */
NULL /* Pointer not needed */
);
/* If window could not be created, return "failure" */
if (!hWnd)
return (FALSE);
/* Make the window visible; update its client area; and return "success" */
ShowWindow(hWnd, nCmdShow); /* Show the window */
UpdateWindow(hWnd); /* Sends WM_PAINT message */
return (TRUE); /* Returns the value from PostQuitMessage */
}
LONG FAR PASCAL MainWndProc(HWND hWnd, unsigned message, WORD wParam, LONG lParam)
{
FARPROC lpProcAbout; /* pointer to the "About" function */
TEXTMETRIC tm;
HMENU hMenu;
switch (message)
{
case WM_CREATE:
SetTimer (hWnd, 1, 1, NULL);
hbBall=LoadBitmap(hInst, "ball");
GetObject(hbBall,16, (LPSTR) &bmBall);
nWidth=bmBall.bmWidth;
nHeight=bmBall.bmHeight;
break;
case WM_COMMAND: /* message: command from application menu */
switch (wParam)
{
case IDM_ABOUT:
lpProcAbout = MakeProcInstance(About, hInst);
DialogBox(hInst, /* current instance */
"AboutBox", /* resource to use */
hWnd, /* parent handle */
lpProcAbout); /* About() instance address */
FreeProcInstance(lpProcAbout);
break;
case IDM_SPEED1:
wPrevItem=wPrevSpeed;
wPrevSpeed=wParam;
AnimeStep=1;
break;
case IDM_SPEED2:
wPrevItem=wPrevSpeed;
wPrevSpeed=wParam;
AnimeStep=5;
break;
case IDM_SPEED3:
wPrevItem=wPrevSpeed;
wPrevSpeed=wParam;
AnimeStep=10;
break;
case IDM_SPEED4:
wPrevItem=wPrevSpeed;
wPrevSpeed=wParam;
AnimeStep=50;
break;
}
if (wParam!=IDM_ABOUT){
CheckMenuItem(GetMenu(hWnd), wPrevItem, MF_UNCHECKED);
CheckMenuItem(GetMenu(hWnd), wParam, MF_CHECKED);
}
break;
case WM_TIMER:
DrawBall();
break;
case WM_PAINT:
{
hDC = BeginPaint (hWnd, &ps);
hMemoryDC = CreateCompatibleDC(hDC);
SelectObject(hMemoryDC, hbBall);
BitBlt(hDC, x, y,
bmBall.bmWidth, bmBall.bmHeight, hMemoryDC, 0, 0, SRCCOPY);
DeleteDC(hMemoryDC);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY: /* message: window being destroyed */
KillTimer(hWnd, 1);
DeleteObject(hbBall);
PostQuitMessage(0);
break;
default: /* Passes it on if unproccessed */
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return (NULL);
}
BOOL FAR PASCAL About(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
{
switch (message)
{
case WM_INITDIALOG: /* message: initialize dialog box */
return (TRUE);
case WM_COMMAND: /* message: received a command */
if (wParam == IDOK || wParam == IDCANCEL)
{
EndDialog(hDlg, TRUE); /* Exits the dialog box */
return (TRUE);
}
break;
}
return (FALSE); /* Didn't process a message */
}
void DrawBall(void)
{
MoveBall();
DrawBitmap (hbBall, x, y);
EraseOldBall();
}
void MoveBall(void)
{
GetClientRect(hWnd, &r);
rect1.left = x;
rect1.top = y;
rect1.right = x+nWidth;
rect1.bottom= y+nHeight;
if (x<=r.left) {
x=r.left;
xdirection=RIGHT;
}
if (x>=(r.right-nWidth)) {
x=(r.right-nWidth);
xdirection=LEFT;
}
if (y<=r.top){
y=r.top;
ydirection=DOWN;
}
if (y>=(r.bottom-nHeight)) {
y=r.bottom-nHeight;
ydirection=UP;
}
if (xdirection==RIGHT){
x+=AnimeStep;
}
else {
x-=AnimeStep;
}
if (ydirection==DOWN) {
y+=AnimeStep;
}
else {
y-=AnimeStep;
}
rect2.left=x;
rect2.top=y;
rect2.right=x+nWidth;
rect2.bottom=y+nHeight;
return;
}
void EraseOldBall(void)
{
HDC hdc;
HBRUSH hbrush;
hdc = GetDC(hWnd);
hbrush=GetStockObject(WHITE_BRUSH);
if (IntersectRect(&rect3,&rect1,&rect2))
{
if (xdirection==RIGHT)
{
if (ydirection==DOWN)
{
rect4.top=rect1.top;
rect4.left=rect1.left;
rect4.bottom=rect3.bottom;
rect4.right=rect3.left;
rect5.top=rect1.top;
rect5.left=rect3.left;
rect5.bottom=rect3.top;
rect5.right=rect3.right;
}
else
{
rect4.top=rect1.top;
rect4.left=rect1.left;
rect4.bottom=rect1.bottom;
rect4.right=rect3.left;
rect5.top=rect3.bottom;
rect5.left=rect3.left;
rect5.bottom=rect1.bottom;
rect5.right=rect1.right;
}
}
else
{
if (ydirection==DOWN)
{
rect4.top=rect1.top;
rect4.left=rect1.left;
rect4.bottom=rect3.top;
rect4.right=rect1.right;
rect5.top=rect3.top;
rect5.left=rect3.right;
rect5.bottom=rect1.bottom;
rect5.right=rect1.right;
}
else
{
rect4.top=rect3.top;
rect4.left=rect3.right;
rect4.bottom=rect1.bottom;
rect4.right=rect1.right;
rect5.top=rect3.bottom;
rect5.left=rect3.left;
rect5.bottom=rect1.bottom;
rect5.right=rect3.right;
}
}
FillRect(hdc, &rect4, hbrush);
FillRect(hdc, &rect5, hbrush);
}
else
{
rect4.top=rect1.top;
rect4.left=rect1.left;
rect4.bottom=rect1.bottom;
rect4.right=rect1.right;
rect5.top=0;
rect5.left=0;
rect5.bottom=0;
rect5.right=0;
FillRect(hdc, &rect4, hbrush);
}
DeleteObject(hbrush);
ReleaseDC(hWnd, hdc);
}
void DrawBitmap (HBITMAP hBitmap, int xStart, int yStart)
{
HDC hdc;
BITMAP bm ;
HDC hdcMem ;
hdc = GetDC(hWnd);
hdcMem = CreateCompatibleDC (hdc) ;
SelectObject (hdcMem, hBitmap) ;
GetObject (hBitmap, sizeof (BITMAP), (LPSTR)&bm) ;
BitBlt (hdc, xStart, yStart, bm.bmWidth, bm.bmHeight,
hdcMem, 0, 0, SRCCOPY) ;
DeleteDC (hdcMem) ;
ReleaseDC (hWnd, hdc);
}