Skip to content

Instantly share code, notes, and snippets.

@Soulstorm50
Created November 5, 2016 17:55
Show Gist options
  • Save Soulstorm50/865561ff184bda4974852970daec5d14 to your computer and use it in GitHub Desktop.
Save Soulstorm50/865561ff184bda4974852970daec5d14 to your computer and use it in GitHub Desktop.
Clicker
#include <windows.h>
#include <tchar.h>
#include <time.h>
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
void SetWindowCaption(HWND hwnd);
int clickCount = 0;
int timerCount = 0;
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nCmdShow)
{
TCHAR szClassWindow[] = L"Win32Application";
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
HWND hWnd;
MSG lpMsg;
WNDCLASSEX wcl;
wcl.cbSize = sizeof(wcl);
wcl.style = CS_HREDRAW | CS_VREDRAW;
wcl.lpfnWndProc = WindowProc;
wcl.cbClsExtra = 0;
wcl.cbWndExtra = 0;
wcl.hInstance = hInst;
wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
wcl.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
wcl.lpszMenuName = NULL;
wcl.lpszClassName = szClassWindow;
wcl.hIconSm = NULL;
if (!RegisterClassEx(&wcl))
return 0;
hWnd = CreateWindowEx(0, szClassWindow, L"Clicker", WS_POPUPWINDOW | WS_CAPTION,
screenWidth / 2 - 250, screenHeight / 2 - 250, 500, 500, NULL, NULL, hInst, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MessageBox(NULL, L"You have to click with the left mouse button somewhere on the gray area, and you have 20 seconds.\n\nClick OK to start.", L"Rules", MB_OK);
SetWindowCaption(hWnd);
SetTimer(hWnd, 1, 1000, TimerProc);
while (GetMessage(&lpMsg, NULL, 0, 0))
{
TranslateMessage(&lpMsg);
DispatchMessage(&lpMsg);
}
return lpMsg.wParam;
}
VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
timerCount++;
SetWindowCaption(hwnd);
if (timerCount == 20)
{
KillTimer(hwnd, idEvent);
TCHAR szResultMessage[100] = {};
wsprintf(szResultMessage, L"Your result is %d click.\n\nDo you want to paly once again?", clickCount);
int answer = MessageBox(NULL, szResultMessage, L"Result", MB_YESNO | MB_ICONQUESTION);
if (answer == IDYES)
{
SetTimer(hwnd, idEvent, 1000, TimerProc);
clickCount = 0;
timerCount = 0;
SetWindowCaption(hwnd);
}
else
{
SendMessage(hwnd, WM_DESTROY, 0, 0);
}
}
}
void SetWindowCaption(HWND hwnd)
{
TCHAR szWindowCaption[50] = {};
wsprintf(szWindowCaption, L"%d seonds left", 20 - timerCount);
SetWindowText(hwnd, szWindowCaption);
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
clickCount++;
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment