<p>What "Hello World" is to the console, the "Bouncing Ball" is to the Graphical User Interface. Nothing fancy, the ball is created via a call to the API function ellipse() and then bounced within the confines of the windows form. For the DEV C++ crowd: Let me know if you find these snippets somewhat helpful in your learning</p> <p><img src="https://steemitimages.com/DQmTMtrc537z4ryKcbhEEdLUzpQQYUf76oBvXpqQCuhNito/Bouncing%20ball.png" alt="Bouncing ball.png"></p> <p>// BCX Bounce by Kevin Diggins adopted from Charles Petzold<br> // BCX generated C code modified for the free Dev-C++ system<br> // from <a href="http://www.bloodshed.net/">http://www.bloodshed.net/</a><br> // or <a href="http://sourceforge.net/projects/dev-cpp/">http://sourceforge.net/projects/dev-cpp/</a><br> // (actually Dev-C++ is the IDE for the GNU GCC/G++ compiler)<br> // a Dev-C++ tested Windows Application by vegaseat 21nov2004<br> #include <windows.h><br> #define Show(Window) RedrawWindow(Window,0,0,0);ShowWindow(Window,SW_SHOW);<br> #define AppName "BouncingBall1"<br> #define Caption "Bouncing Ball ..."<br> char BCX_STR [1024<em>1024];<br> static int BCX_GetDiaUnit;<br> static int BCX_cxBaseUnit;<br> static int BCX_cyBaseUnit;<br> static int BCX_ScaleX;<br> static int BCX_ScaleY;<br> static HANDLE Form1;<br> double MIN (double,double);<br> double MAX (double,double);<br> int WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int);<br> void FormLoad (HANDLE);<br> LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);<br> double MAX (double a, double b)<br> {<br> if (a > b)<br> {<br> return a;<br> }<br> return b;<br> }<br> double MIN (double a, double b)<br> {<br> if (a < b)<br> {<br> return a;<br> }<br> return b;<br> }<br> // standard main for Windows GUI<br> int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR CmdLine, int CmdShow)<br> {<br> static WNDCLASS Wc;<br> memset(&Wc,0,sizeof(Wc));<br> static MSG Msg;<br> memset(&Msg,0,sizeof(Msg));<br> Wc.style=CS_HREDRAW | CS_VREDRAW;<br> Wc.lpfnWndProc=WndProc;<br> Wc.cbClsExtra=0;<br> Wc.cbWndExtra=0;<br> Wc.hInstance=hInst;<br> Wc.hIcon=LoadIcon(NULL,IDI_WINLOGO);<br> Wc.hCursor=LoadCursor(NULL,IDC_ARROW);<br> Wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);<br> Wc.lpszMenuName=NULL;<br> Wc.lpszClassName=AppName;<br> RegisterClass(&Wc);<br> FormLoad(hInst);<br> // 50ms here, lower value gives higher speed<br> SetTimer((HWND)Form1,1,50,NULL);<br> // ye olde event message loop<br> while(GetMessage(&Msg,NULL,0,0))<br> {<br> if (!IsWindow((HWND)Form1)||!IsDialogMessage((HWND)Form1,&Msg))<br> {<br> TranslateMessage(&Msg);<br> DispatchMessage(&Msg);<br> }<br> }<br> return Msg.wParam;<br> }<br> // create the form and show it (somewhat older style)<br> void FormLoad (HANDLE hInst)<br> {<br> // get the scale factors<br> BCX_GetDiaUnit = GetDialogBaseUnits();<br> BCX_cxBaseUnit = LOWORD(BCX_GetDiaUnit);<br> BCX_cyBaseUnit = HIWORD(BCX_GetDiaUnit);<br> BCX_ScaleX = BCX_cxBaseUnit/4;<br> BCX_ScaleY = BCX_cyBaseUnit/8;<br> // now the form<br> Form1=CreateWindow(AppName,Caption,<br> DS_MODALFRAME|WS_POPUP|WS_CAPTION|WS_SYSMENU,<br> 10</em>BCX_ScaleX,20<em>BCX_ScaleY,250</em>BCX_ScaleX,175*BCX_ScaleY,NULL,<br> (HMENU)NULL,(HINSTANCE)hInst,NULL);<br> Show((HWND)Form1);<br> }<br> // event message handler<br> LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)<br> {<br> static HANDLE hBitmap;<br> static HBRUSH hBrush;<br> static HDC hdc;<br> static HDC hdcMem;<br> static int cxClient;<br> static int cyClient;<br> static int xCenter;<br> static int yCenter;<br> static int cxTotal;<br> static int cyTotal;<br> static int cxRadius;<br> static int cyRadius;<br> static int cxMove;<br> static int cyMove;<br> static int xPixel;<br> static int yPixel;<br> static int nScale;</p> <p>while(1)<br> {<br> if (Msg == WM_CREATE)<br> {<br> hdc = GetDC(hWnd);<br> xPixel = GetDeviceCaps(hdc,ASPECTX);<br> yPixel = GetDeviceCaps(hdc,ASPECTY);<br> ReleaseDC(hWnd,hdc);<br> return 0;<br> break;<br> }<br> // draw the ball<br> if (Msg == WM_SIZE)<br> {<br> xCenter = (cxClient=LOWORD(lParam))/2;<br> yCenter = (cyClient=HIWORD(lParam))/2;<br> nScale = (int)MIN(cxClient<em>xPixel,cyClient</em>yPixel)/16;<br> cxRadius = nScale/xPixel;<br> cyRadius = nScale/yPixel;<br> cxMove = (int)MAX(1,cxRadius/4);<br> cyMove = (int)MAX(1,cyRadius/4);<br> cxTotal = 2<em>(cxRadius+cxMove);<br> cyTotal = 2</em>(cyRadius+cyMove);<br> if (hBitmap)<br> {<br> DeleteObject(hBitmap);<br> }<br> hdc = GetDC(hWnd);<br> hdcMem = CreateCompatibleDC(hdc);<br> hBitmap = CreateCompatibleBitmap(hdc,cxTotal,cyTotal);<br> ReleaseDC(hWnd,hdc);<br> SelectObject(hdcMem,hBitmap);<br> Rectangle(hdcMem,-1,-1,cxTotal+1,cyTotal+1);<br> hBrush = CreateHatchBrush(HS_DIAGCROSS,0);<br> SelectObject(hdcMem,hBrush);<br> SetBkColor(hdcMem,RGB(0,127,255));<br> Ellipse(hdcMem,cxMove,cyMove,cxTotal-cxMove,cyTotal-cyMove);<br> DeleteDC(hdcMem);<br> DeleteObject(hBrush);<br> return 0;<br> break;<br> }<br> // move the ball<br> if (Msg == WM_TIMER)<br> {<br> if (!hBitmap)<br> {<br> break;<br> }<br> hdc = GetDC(hWnd);<br> hdcMem = CreateCompatibleDC(hdc);<br> SelectObject(hdcMem,hBitmap);<br> BitBlt(hdc,xCenter-cxTotal/2,yCenter-cyTotal/2,cxTotal,cyTotal,hdcMem,0,0,SRCCOPY);<br> ReleaseDC(hWnd,hdc);<br> DeleteDC(hdcMem);<br> xCenter += cxMove;<br> yCenter += cyMove;<br> if (xCenter+cxRadius>=cxClient||xCenter-cxRadius<=0)<br> {<br> cxMove = -cxMove;<br> }<br> if (yCenter+cyRadius >= cyClient || yCenter-cyRadius <= 0)<br> {<br> cyMove = -cyMove;<br> }<br> return 0;<br> break;<br> }<br> // clean up and exit program<br> if (Msg == WM_DESTROY)<br> {<br> if (hBitmap)<br> {<br> DeleteObject(hBitmap);<br> }<br> KillTimer((HWND)Form1,1);<br> PostQuitMessage(0);<br> return 0;<br> }<br> break;<br> }<br> return DefWindowProc(hWnd, Msg, wParam, lParam);<br> }</p> <p>Semoga Bermamfaat ;)</p>