March 24, 2016

Visual Calendar : Visual C++



       The following program is a Calendar in Visual C++. This program has been created in Visual Studio Express 2013 IDE. You can navigate around this Calendar's pages using the buttons.



The screenshots below describe its functionality :



Visual Calendar - Output

Visual Calendar - Output




Source Code :


#include <windows.h>
#include <math.h>
#include <tchar.h>
#include "resource.h"

#define ID_TIMER 1
#define TWOPI (2 * 3.14159)

HINSTANCE hInst;


int Left = -700, Top = 700, Width = 210, Height = 210;
long int  _YYYY, _MM, _DD, _tempyy, _tempmm;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
       static TCHAR szAppName[] = TEXT("Visual Calendar");
       HWND hwnd;
       MSG msg;
       WNDCLASS wndclass;
       wndclass.style = CS_HREDRAW | CS_VREDRAW;
       wndclass.lpfnWndProc = WndProc;
       wndclass.cbClsExtra = 0;
       wndclass.cbWndExtra = 0;
       wndclass.hInstance = hInstance;
       wndclass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_CALENDAR));
       wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
       wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
       wndclass.lpszMenuName = NULL;
       wndclass.lpszClassName = szAppName;

       if (!RegisterClass(&wndclass))
       {
              MessageBox(NULL, TEXT("Window Registration Failed!"),
                     szAppName, MB_ICONERROR);
              return 0;
       }

       hwnd = CreateWindow(szAppName, TEXT("Visual Calendar"),
              WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
              CW_USEDEFAULT, CW_USEDEFAULT,
              600, 600,
              NULL, NULL, hInstance, NULL);
       ShowWindow(hwnd, iCmdShow);
       UpdateWindow(hwnd);
       while (GetMessage(&msg, NULL, 0, 0))
       {
              TranslateMessage(&msg);
              DispatchMessage(&msg);
       }
       return msg.wParam;
}

/*-------------------------------------------------------------------------------------*/

int _NumberOfDays(int month, int year)
{

       int No_of_days = 0;
       switch (month)
       {
       case 1:
              No_of_days = 31;
              break;
       case 2:
              if (!((year % 4) || !(year % 100) && (year % 400)))
                     No_of_days = 29;
              else
                     No_of_days = 28;
              break;
       case 3:
              No_of_days = 31;
              break;
       case 4:
              No_of_days = 30;
              break;
       case 5:
              No_of_days = 31;
              break;
       case 6:
              No_of_days = 30;
              break;
       case 7:
              No_of_days = 31;
              break;
       case 8:
              No_of_days = 31;
              break;
       case 9:
              No_of_days = 30;
              break;
       case 10:
              No_of_days = 31;
              break;
       case 11:
              No_of_days = 30;
              break;
       case 12:
              No_of_days = 31;
              break;
       }

       return No_of_days;
}


int No_Of_Days()
{


       int d1, m1, y1;
       int d2, m2, y2;
       int D, M, Y;
       int Arr[12];
       int i, count;


       y1 =_YYYY;
       m1 = _MM;
       d1 = 01;


       y2 = 2015;
       m2 = 01;
       d2 = 01;

       if (y1 > y2 || (m1 > m2 && y1 == y2))
       {
              D = d1;
              M = m1;
              Y = y1;

              d1 = d2;
              m1 = m2;
              y1 = y2;

              d2 = D;
              m2 = M;
              y2 = Y;

       }

       count = 0;

       for (i = m1; i < (m2 + (y2 - y1) * 12); i++)

       {
              if (i > 12)
              {

                     i = 1;
                     y1 = y1 + 1;
              }

              Arr[0] = 31;
              if (!((y1 % 4) || !(y1 % 100) && (y1 % 400)))
              {
                     Arr[1] = 29;
              }
              else
              {
                     Arr[1] = 28;
              }
              Arr[2] = 31;
              Arr[3] = 30;
              Arr[4] = 31;
              Arr[5] = 30;
              Arr[6] = 31;
              Arr[7] = 31;
              Arr[8] = 30;
              Arr[9] = 31;
              Arr[10] = 30;
              Arr[11] = 31;

              count = count + (Arr[i - 1]);

       }

       count = count + d2 - d1;

       if (count < 0)
              count = count * -1;
  
       return count;
}


int Name_Of_Day()
{
       /* Assuming in 2015 on first january the day was thrusday. */
       /* Let S-1 M-2 T-3 W-4 T-5 F-6 S-7 */
        
       int _day = 5;
       int total_days = 0;

       total_days = No_Of_Days();

       if (_YYYY >= 2015)
       {

              for (int k = 1; k <= total_days; k++)
              {
                     _day++;
                     if (_day> 7)
                           _day = 1;
              }
       }
       else
       {
              for (int k = 1; k <= total_days; k++)
              {
                     _day--;
                     if (_day< 1)
                           _day = 7;
              }
       }

       return _day;

}

void WriteDays(HDC hdc, int left, int top)
{

       HFONT hfont;
       LOGFONT f = { 0 };
       HGDIOBJ holdfont;
       static HBRUSH hBrush;

       int No_of_days;
       int _left = left, _top = top;
       int _col = 0;
       int _i;

       hBrush = CreateSolidBrush(RGB(0,255,255));
      
       f.lfHeight = 110; /* font height */
       hfont = CreateFontIndirect(&f);
       holdfont = SelectObject(hdc, hfont);
       SetTextColor(hdc, RGB(0, 0, 0));
       SetBkMode(hdc, TRANSPARENT);


       No_of_days = _NumberOfDays((int)_MM, (int)_YYYY);

       left = left + (Name_Of_Day() - 1) * 200;

       for (int i = 0; i< No_of_days; i++)
       {

              if (left >= _left + 7*200)
              {
                     left = _left;
                     top -= 200;
              }


              /*****************************/
              if (_YYYY == _tempyy && _MM == _tempmm && _DD == i + 1)
              {
                     SelectObject(hdc, hBrush);
                     Ellipse(hdc, left-25, top+20, left+125, top-125);
                     _col = 1;

              }
              /*****************************/


              if (_col == 1)
              {
                     SetTextColor(hdc, RGB(255, 255, 255));
                     _col = 0;
              }
              else if (left == _left)
                     SetTextColor(hdc, RGB(255, 0, 0));
              else
                     SetTextColor(hdc, RGB(0, 0, 0));

              _i = i + 1;
              wchar_t buffer[13];
              _itow_s(_i, buffer, 10);
              TextOut(hdc, left, top, buffer, 2);

              left += 200;

       }

}


void Draw(HDC hdc)
{

       HFONT hfont;
       LOGFONT f = { 0 };
       HGDIOBJ holdfont;

       int left = Left, top = Top;
      
       int _left = -400;

       f.lfHeight = 110; /* font height */
       hfont = CreateFontIndirect(&f);
       holdfont = SelectObject(hdc, hfont);
       SetTextColor(hdc, RGB(255, 0, 0));
       SetBkMode(hdc, TRANSPARENT);


       /******************************************/

             
              TextOut(hdc, left+10, top+8, _T("S"), 2);
              left += 200;
              SetTextColor(hdc, RGB(0, 0, 0));
              SetBkMode(hdc, TRANSPARENT);
              TextOut(hdc, left + 10, top + 8, _T("M"), 2);
              left += 200;
              TextOut(hdc, left + 10, top + 8, _T("T"), 2);
              left += 200;
              TextOut(hdc, left + 10, top + 8, _T("W"), 2);
              left += 200;
              TextOut(hdc, left + 10, top + 8, _T("T"), 2);
              left += 200;
              TextOut(hdc, left + 10, top + 8, _T("F"), 2);
              left += 200;
              TextOut(hdc, left + 10, top + 8, _T("S"), 2);
              left += 200;
             
      

       /*******************************************/

              SetTextColor(hdc, RGB(0, 128, 255));
              SetBkMode(hdc, TRANSPARENT);

              switch (_MM)
              {
              case 1:
                     TextOut(hdc, _left, top + 200, _T("January"), 7);
                     break;
              case 2:
                     TextOut(hdc, _left, top + 200, _T("February"), 8);
                     break;
              case 3:
                     TextOut(hdc, _left, top + 200, _T("March"), 5);
                     break;
              case 4:
                     TextOut(hdc, _left, top + 200, _T("April"), 5);
                     break;
              case 5:
                     TextOut(hdc, _left, top + 200, _T("May"), 3);
                     break;
              case 6:
                     TextOut(hdc, _left, top + 200, _T("June"), 4);
                     break;
              case 7:
                     TextOut(hdc, _left, top + 200, _T("July"), 4);
                     break;
              case 8:
                     TextOut(hdc, _left, top + 200, _T("August"), 6);
                     break;
              case 9:
                     TextOut(hdc, _left, top + 200, _T("September"), 9);
                     break;
              case 10:
                     TextOut(hdc, _left, top + 200, _T("October"), 7);
                     break;
              case 11:
                     TextOut(hdc, _left, top + 200, _T("November"), 8);
                     break;
              case 12:
                     TextOut(hdc, _left, top + 200, _T("December"), 8);
                     break;
              }


              wchar_t buffer[13];
              _itow_s(_YYYY, buffer, 10);
              TextOut(hdc, _left+500, top+200, buffer, 4);
}

void Init()
{
      
       static SYSTEMTIME st;

       GetLocalTime(&st);

       _YYYY = st.wYear;
       _MM =  st.wMonth;
       _DD = st.wDay;

       _tempyy = _YYYY;
       _tempmm = _MM;

}

void ClearScreen(HDC hdc)
{

       SelectObject(hdc, GetStockObject(WHITE_PEN));
       Rectangle(hdc, -1000, 1000, 1000, -600);
             
}


void SetIsotropic(HDC hdc, int cxClient, int cyClient)
{
       SetMapMode(hdc, MM_ISOTROPIC);
       SetWindowExtEx(hdc, 1000, 1000, NULL);
       SetViewportExtEx(hdc, cxClient / 2, -cyClient / 2, NULL);
       SetViewportOrgEx(hdc, cxClient / 2, cyClient / 2, NULL);
}

/*-------------------------------------------------------------------------------------*/


LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
       static int cxClient, cyClient;
       HDC hdc;
       PAINTSTRUCT ps;
       static HWND hBtnMonPlus = NULL, hBtnMonMinus = NULL, hBtnYrPlus = NULL, hBtnYrMinus = NULL, hBtnToday = NULL;

       switch (message)
       {

       case WM_SIZE:
              cxClient = LOWORD(lParam);
              cyClient = HIWORD(lParam);
              return 0;

       case WM_COMMAND:

              hdc = GetDC(hwnd);

              switch (LOWORD(wParam))
              {
              case IDC_BTNMONPLUS:

                     // Month++

                    
                     if (_YYYY < 2070)
                           _MM++;

                     if (_MM>12 && _YYYY < 2070)
                     {
                           _MM = 1;
                           _YYYY++;
                     }

                    
                     break;
              case IDC_BTNMONMINUS:

                     // Month--


                     if (_YYYY>1500)
                           _MM--;
                     if (_MM < 1 && _YYYY > 1500)
                     {
                           _MM = 12;
                           _YYYY--;
                     }
                    

                     break;
              case IDC_BTNYRPLUS:

                     // Year++

                     if (_YYYY < 2070)
                           _YYYY++;


                     break;
              case IDC_BTNYRMINUS:

                     // Year--

                     if (_YYYY > 1500)
                           _YYYY--;

                     break;

              case IDC_BTNTODAY:

                     // Today

                     Init();

                     break;
              }

              ReleaseDC(hwnd, hdc);

              return 0;

       case WM_CREATE:
              hdc = GetDC(hwnd);

              //SetIsotropic(hdc, cxClient, cyClient);
              SetTimer(hwnd, ID_TIMER, 1000, NULL);

              // Month++
              hBtnMonPlus = CreateWindow(TEXT("BUTTON"), TEXT("Month++"),
                     WS_CHILD | WS_VISIBLE |
                     BS_PUSHBUTTON,
                     480, 500, 90, 40,
                     hwnd,
                     (HMENU)IDC_BTNMONPLUS,
                     hInst, NULL);

              // Month--
              hBtnMonPlus = CreateWindow(TEXT("BUTTON"), TEXT("Month- -"),
                     WS_CHILD | WS_VISIBLE |
                     BS_PUSHBUTTON,
                     370, 500, 90, 40,
                     hwnd,
                     (HMENU)IDC_BTNMONMINUS,
                     hInst, NULL);

              // Year++
              hBtnMonPlus = CreateWindow(TEXT("BUTTON"), TEXT("Year++"),
                     WS_CHILD | WS_VISIBLE |
                     BS_PUSHBUTTON,
                     250, 500, 100, 40,
                     hwnd,
                     (HMENU)IDC_BTNYRPLUS,
                     hInst, NULL);

              // Year--
              hBtnMonPlus = CreateWindow(TEXT("BUTTON"), TEXT("Year- -"),
                     WS_CHILD | WS_VISIBLE |
                     BS_PUSHBUTTON,
                     130, 500, 100, 40,
                     hwnd,
                     (HMENU)IDC_BTNYRMINUS,
                     hInst, NULL);


              // Today
              hBtnToday = CreateWindow(TEXT("BUTTON"), TEXT("Today"),
                     WS_CHILD | WS_VISIBLE |
                     BS_PUSHBUTTON,
                     10, 500, 100, 40,
                     hwnd,
                     (HMENU)IDC_BTNTODAY,
                     hInst, NULL);


              ReleaseDC(hwnd, hdc);

              return 0;

       case WM_TIMER:
              hdc = GetDC(hwnd);
              SetIsotropic(hdc, cxClient, cyClient);
              ClearScreen(hdc);
              Draw(hdc);
              WriteDays(hdc, Left, Top - 200);

              ReleaseDC(hwnd, hdc);
              return 0;
       case WM_PAINT:
              hdc = BeginPaint(hwnd, &ps);
              SetIsotropic(hdc, cxClient, cyClient);
             
              Init();
              Draw(hdc);
              WriteDays(hdc, Left, Top-200);

              EndPaint(hwnd, &ps);
              return 0;
       case WM_DESTROY:
              KillTimer(hwnd, ID_TIMER);
              PostQuitMessage(0);
              return 0;
       }
       return DefWindowProc(hwnd, message, wParam, lParam);
}




Downloads :

Click here to view and download the source code of Visual Calendar (1.86 MB).

Click here to download VisualCalendar.exe only (304 KB).







No comments:

Post a Comment