Windows Program for Analog Clock, Study notes of Windows Programming

This is complete windows program for Analog Clock.

Typology: Study notes

Pre 2010

Available from 04/08/2023

MnGhani
MnGhani 🇵🇰

6 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
// Header Files
#include <windows.h>
#include <math.h>
#define TwoPi (2*3.14159) // Define Directives
// Prototype and Global Variables
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) ;
HINSTANCE hInst;
HWND handle;
// WinMain Procedure
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow)
{
HWND hwnd;
MSG msg;
hInst = hInstance;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = 0;
wndclass.hCursor = 0;
wndclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "MyClass";
// Check for error handling
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
"Error", MB_ICONERROR) ;
return 0 ;
}
// Creationof window and handle
hwnd = CreateWindow ("MyClass", TEXT ("Clock"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
pf3
pf4
pf5

Partial preview of the text

Download Windows Program for Analog Clock and more Study notes Windows Programming in PDF only on Docsity!

// Header Files #include <windows.h> #include <math.h> #define TwoPi (2*3.14159) // Define Directives // Prototype and Global Variables LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM) ; HINSTANCE hInst; HWND handle; // WinMain Procedure int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { HWND hwnd; MSG msg; hInst = hInstance; WNDCLASS wndclass; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = 0; wndclass.hCursor = 0; wndclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = "MyClass"; // Check for error handling if (!RegisterClass (&wndclass)) { MessageBox (NULL, TEXT ("This program requires Windows NT!"), "Error", MB_ICONERROR) ; return 0 ; } // Creationof window and handle hwnd = CreateWindow ("MyClass", TEXT ("Clock"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

NULL, NULL, hInstance, NULL); // function for showing window ShowWindow (hwnd, iCmdShow); UpdateWindow (hwnd); // loop for processing windows messages while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } return msg.wParam; } // endof win min /* Declaration and Definition of Local functions / // function for rotating point void RotatePoint (POINT &p, int rad, int deg) { p.x = (long)(rad * cos(TwoPideg/360)); // rotation for x_coordinate p.y = (long)(rad * sin(TwoPi*deg/360)); //rotation for y_cordinate } // end of function // function for drawing clock void DrawClock (HWND hwnd, HDC h, int radius) { RECT rect; POINT pt; // structure for containing x & y int diff; // for width of ellipse long angle, x, y;//for angle // code for drawing clock for (angle=0; angle<361; angle+=6)// for making ellipses { GetClientRect (hwnd, &rect); // get clientwindow coordinates //rotate point and check for size of circle RotatePoint (pt, radius, angle); x = rect.right/2 + pt.x; // x_coordinate of centre y = rect.bottom/2 + pt.y; // y_coordinate of centre diff = angle%5? 2:8;

HDC hdc; int df, diff; RECT rect; BOOL fChange; static SYSTEMTIME st ; // static bcz time retained in function calLs //initialization diff = 3; fChange = FALSE; // Strat of different Cases switch (message) { case WM_CREATE: st.wHour = (WORD)0; st.wMinute = (WORD)0; st.wSecond = (WORD)0; break; // case for WM_TIMER case WM_TIMER : st.wSecond = st.wSecond+1; if (st.wSecond == 60) { st.wMinute = st.wMinute+1; st.wSecond = 0; } if (st.wMinute == 12) { st.wHour = st.wHour+1; st.wMinute = 0; } if (st.wHour == 120) st.wHour = 0; // Invalidate client area to be repainted GetClientRect (hwnd, &rect); InvalidateRect (hwnd, &rect, TRUE); break;

// case for WM_PAINT case WM_PAINT: GetClientRect (hwnd, &rect); df = rect.bottom/2-13; // creation of child windows CreateWindow ("BUTTON","Start",BS_DEFPUSHBUTTON|WS_VISIBLE| WS_CHILD,rect.left+20,rect.top+20,df/3,df/3,hwnd,(HMENU)3,hInst,NULL); CreateWindow ("BUTTON","Stop",BS_DEFPUSHBUTTON|WS_VISIBLE|WS_CHILD,rect.left +20,rect.top+20+df/3,df/3,df/3,hwnd,(HMENU)4,hInst,NULL); CreateWindow ("BUTTON","Reset",BS_DEFPUSHBUTTON|WS_VISIBLE|WS_CHILD,rect.left +20,rect.top+20+2*df/3,df/3,df/3,hwnd,(HMENU)5,hInst,NULL); // handle for device context hdc = BeginPaint(hwnd,&ps); // drawing of objects on screen SelectObject (hdc, GetStockObject (WHITE_PEN)) ; Ellipse (hdc, rect.right/2, rect.bottom/2,rect.right/ +2, rect.bottom/2+2 ); DrawClock (hwnd, hdc, df); DrawHands (hwnd,hdc, &st); EndPaint(hwnd,&ps); break; // case for handling inputs case WM_COMMAND: // handling of button for start if(LOWORD(wParam)==3) { if(HIWORD(wParam)==BN_CLICKED) { // set timer SetTimer (hwnd, 1, 1000, NULL) ; // set timer values st.wHour = (WORD)0; st.wMinute = (WORD)0; st.wSecond = (WORD)0; } } // handling of button for stop