Code:
//Steve Mack
//Air Hockey
#include <windows.h>
#include <conio.h>
HWND ghWnd_Main;
HINSTANCE ghInst;
MSG msg;
HWND Clear;
int puckx = 100;
int pucky = 100;
HANDLE HThread;
unsigned long ThreadH;
unsigned long _stdcall getchthread();
char test = 'x';
//function prototypes
void SetFont(HWND hWnd, int iPointSize, const char *pcFontName); //(obviously) sets fonts of controls
void Draw();
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg) {
case WM_CLOSE:
TerminateThread(HThread,0);
DestroyWindow(ghWnd_Main); // Destroy the main window
return 0;
case WM_DESTROY:
TerminateThread(HThread,0);
PostQuitMessage(0); // Quit the application
return 0;
case WM_COMMAND:
//if(LOWORD(wParam) == BN_CLICKED && (HWND)lParam == OPENABOUT) {
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam); // Not handled - let the system deal with it
}
//==================WINDOW CODE=========================================================
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
ghInst = hInstance;
WNDCLASSEX wcxMyClass;
wcxMyClass.cbSize = sizeof(WNDCLASSEX); // Size of structure in bytes
wcxMyClass.style = 0; // Extra style information
wcxMyClass.lpfnWndProc = WndProc; // Pointer to window procedure
wcxMyClass.cbClsExtra = 0; // Extra bytes in class definition
wcxMyClass.cbWndExtra = 0; // Extra bytes for each window
wcxMyClass.hInstance = hInstance; // Instance handle for the class
wcxMyClass.hIcon = NULL; // Default icon - use system
wcxMyClass.hCursor = LoadCursor(NULL, IDC_ARROW); // Load the system arrow
wcxMyClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); // Background brush
wcxMyClass.lpszMenuName = NULL; // Menu name [MAKEINTRESOURCE(IDM_MAINMENU)] - none here
wcxMyClass.lpszClassName = "TESTCLASS"; // Class name
wcxMyClass.hIconSm = NULL; // Small icon
RegisterClassEx(&wcxMyClass); // Register the class
ghWnd_Main = CreateWindow("TESTCLASS", "Air Hockey - Steve Mack", \
WS_SYSMENU | WS_MINIMIZEBOX, 100, 100, 550, 350, \
NULL, NULL, hInstance, NULL);
if(ghWnd_Main) {
Clear = CreateWindow("Static", "", WS_CHILD | /*WS_DLGFRAME |*/ WS_VISIBLE, 0, 0, 500, 500, ghWnd_Main, NULL, hInstance, NULL);
ShowWindow(Clear,0);
ShowWindow(ghWnd_Main, nCmdShow); // Show using the specific show style
}
getchthread();
test = getch();
TerminateThread(HThread,0);
//=================== Handle message loop ==================================================
while(GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg); // Translate keycode messages
DispatchMessage(&msg); // Send to the window
}
UnregisterClass("TESTCLASS", ghInst); // Unregister our window class
return msg.wParam; // msg.wParam contains the code passed to PostQuitMessage()
}
//=========================FUNCTIONS===============================
void SetFont(HWND hWnd, int iPointSize, const char *pcFontName) { //(obviously) sets fonts of controls
HFONT hTheFont;
HDC hDC = GetDC(hWnd);
int nHeight = -MulDiv(iPointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
hTheFont = CreateFont(nHeight, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, pcFontName);
ReleaseDC(hWnd, hDC);
SendMessage(hWnd, WM_SETFONT, (WPARAM)hTheFont, TRUE);
}
void Draw()
{
HDC myDC = GetDC(ghWnd_Main);
Ellipse(myDC, puckx-10,pucky-10,puckx+10,pucky+10);
}
unsigned long _stdcall getchthread()
{
int i = 1;
char buffer[10];
while(test=='x')
{
SetWindowText(Clear, itoa(i, buffer, 10));
i++;
}
return 0;
}