Win32 Sample Applications: Wake Me Up

 

Introduction

A clock is an electronic device used to wake a person up by playing a pre-specified radio station at a particular time. Instead of using one, you can develop an application that would cause a computer's music file or a computer CD to start playing a song of your choice at a certain time. In this exercise, we will create such a program.

Prerequisites:

  • Timer
  • MCI (we haven't had time to cover MCI enough but you don't need all its details to perform this exercise)

Practical Learning: Creating the Application

  1. Start Borland C++BuilderX and, on the main menu, click File -> New...
     
  2. In the Object Gallery dialog box, click New GUI Application and click OK
  3. In the New GUI Application Project Wizard - Step 1 of 3, in the Directory edit box of the Project Settings section, type the path you want. Otherwise, type
    C:\Programs\Win32 Programming
  4. In the Name edit box, type WakeMeUp
  5. Click Next
  6. In the New GUI Application Project Wizard - Step 2 of 3, accept the defaults and click Next
  7. In the New GUI Application Project Wizard - Step 3 of 3, click the check box under Create
  8. Select Untitled under the Name column header. Type Exercise to replace the name and press Tab
  9. Click Finish
  10. To create a resource header file, on the main menu, click File -> New File...
  11. In the Create New File dialog box, in the Name, type resource
  12. In the Type combo box, select h
  13. Click OK
  14. In the file, type:
     
    #define IDD_WAKEMEUP_DLG 101
  15. To create a resource script, on the main menu, click File -> New File...
  16. In the Create New File dialog box, in the Name, type WakeMeUp
  17. In the Type combo box, select rc
     
  18. Click OK
  19. In the file, type:
     
    #include "resource.h"
    
    IDD_WAKEMEUP_DLG DIALOG DISCARDABLE  200, 100, 200, 100
    STYLE WS_POPUP | WS_CAPTION | WS_VISIBLE | WS_SYSMENU
    CAPTION "Wake Me Up"
    FONT 8, "MS Sans Serif"
    BEGIN
        
    END
  20. Display the Exercise.cpp file and change it as follows:
     
    #include <windows.h>
    #ifdef __BORLANDC__
      #pragma argsused
      #endif
    
      #include "resource.h"
    
    //---------------------------------------------------------------------------
      HWND hWnd;
      HINSTANCE hInst;
      LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    //---------------------------------------------------------------------------
    int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
    {
        hInst = hInstance;
    
        DialogBox(hInst, MAKEINTRESOURCE(IDD_WAKEMEUP_DLG),
                         hWnd, (DLGPROC)DlgProc);
    
      return 0;
      }
    //---------------------------------------------------------------------------
      LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg,
                             WPARAM wParam, LPARAM lParam)
      {
              switch(Msg)
              {
              case WM_INITDIALOG:
                      return TRUE;
    
              case WM_COMMAND:
                      switch(wParam)
                      {
                        case IDCANCEL:
                            EndDialog(hWndDlg, 0);
                            return TRUE;
                      }
                      break;
    
              case WM_CLOSE:
                      PostQuitMessage(WM_QUIT);
                      break;
              }
    
              return FALSE;
      }
    //---------------------------------------------------------------------------
  21. To include the MCI and the common control libraries to your project, on the main menu of C++BuilderX, click Project -> Add Files
  22. Navigate to Drive:\C++BuilderX\lib\psdk folder and display its content
  23. In the right frame, click winmm.lib
     
  24. Click OK
  25. Display the Add To dialog box again but this time, select comctl32.lib
  26. Test the application then close the dialog box
  27. To add another button to the dialog box, access the resource.h header file and add the following:
     
    #define IDD_WAKEMEUP_DLG 	101
    #define IDC_SET_BTN             103
    #define IDC_TIMETOWAKEUP        104
    #define IDC_CHECKTIME           105
  28. Access the WakeMeUp.rc resource file and change it as follows:
     
    #include <commctrl.h>
    #include "resource.h"
    
    IDD_WAKEMEUP_DLG DIALOG DISCARDABLE  200, 100, 190, 65
    STYLE WS_POPUP | WS_CAPTION | WS_VISIBLE | WS_SYSMENU
    CAPTION "Wake Me Up"
    FONT 8, "MS Sans Serif"
    BEGIN
        DEFPUSHBUTTON      "Snooze", IDCANCEL, 10, 32, 80, 23
        PUSHBUTTON         "Set", IDC_SET_BTN, 95, 32, 80, 23
        LTEXT              "Wake me up at:", IDC_STATIC, 10, 10, 52, 8
        CONTROL            "DTP1", IDC_TIMETOWAKEUP, "SysDateTimePick32",
                           DTS_RIGHTALIGN  | DTS_UPDOWN | WS_TABSTOP | 0x8,
                           70, 10, 65, 15
    END
  29. Access the Exercise.cpp file and change it as follows:
    #include <windows.h>
    #include <commctrl.h>
    #include <Mmsystem.h>
    #include <stdio.h>
    #ifdef __BORLANDC__
      #pragma argsused
      #endif
    
      #include "resource.h"
    
    //---------------------------------------------------------------------------
      HWND hWnd;
      HINSTANCE hInst;
      int DialogWidth;
      int DialogHeight;
      SYSTEMTIME tmeSet;
      LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    //---------------------------------------------------------------------------
    int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
    {
        hInst = hInstance;
    
        DialogBox(hInst, MAKEINTRESOURCE(IDD_WAKEMEUP_DLG),
                  hWnd, (DLGPROC)DlgProc);
    
        INITCOMMONCONTROLSEX InitCtrlEx;
    
        InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
        InitCtrlEx.dwICC  = ICC_DATE_CLASSES;
        InitCommonControlsEx(&InitCtrlEx);
    
      return 0;
      }
    //---------------------------------------------------------------------------
      LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg,
                             WPARAM wParam, LPARAM /*lParam*/)
      {
          HDC hdc;
          HWND hWndDateTimePicker, hWndOK;
    
          LONG ptrStyles = GetWindowLong(hWndDlg, GWL_STYLE);
          // This will represent the dimensions of the whole screen
          RECT rctClient;
    
           // Create a black brush
           HBRUSH BlackBrush;
           // Select the black brush
           HBRUSH oldBrush;
    
          hWndDateTimePicker = GetDlgItem(hWndDlg, IDC_TIMETOWAKEUP);
          hWndOK = GetDlgItem(hWndDlg, IDOK);
    
          SYSTEMTIME tmeCurrent;
    
              switch(Msg)
              {
              case WM_INITDIALOG:
                      return TRUE;
    
              case WM_COMMAND:
                      switch(wParam)
                      {
                        case IDCANCEL:
                            EndDialog(hWndDlg, 0);
                            return TRUE;
    
                        case IDC_SET_BTN:
                            // Get the time that the user had set and store it
                            // in the tmeSet variable
                            DateTime_GetSystemtime(hWndDateTimePicker, &tmeSet);
    
                            hdc = GetDC(hWndDlg);
                            BlackBrush = CreateSolidBrush(RGB(0, 0, 0));
    
                            SelectObject(hdc, BlackBrush);
                            // TODO: Add your control notification handler code here
                            // Get the screen dimensions
                            DialogWidth  = GetSystemMetrics(SM_CXSCREEN);
                            DialogHeight = GetSystemMetrics(SM_CYSCREEN);
    
                            // When sets the clock CD, remove the title bar and the borders
    
                            ptrStyles &= ~WS_TILEDWINDOW;
                            SetWindowLong(hWndDlg, GWL_STYLE, ptrStyles);
    
                            // Occupy the whole screen
                            SetWindowPos(hWndDlg, HWND_TOPMOST, 0, 0, DialogWidth, DialogHeight, SWP_SHOWWINDOW);
    
                            // Get the dimension of the current dialog box
                            GetWindowRect(hWndDlg, &rctClient);
    
                            // Paint the dialog box in black
                            Rectangle(hdc, rctClient.left, rctClient.top, rctClient.right, rctClient.bottom);
    
                            // Restore the original brush
                            SelectObject(hdc, oldBrush);
                            // Start the timer control
    
                            SetTimer(hWndDlg, IDC_CHECKTIME, 2000, 0);
                            // We don't need to see the cursor
                            ShowCursor(FALSE);
                            ReleaseDC(hWndDlg, hdc);
    
    
                            return TRUE;
                      }
                      break;
    
              case WM_TIMER:
                    // Get the current time on the computer
                    GetLocalTime(&tmeCurrent);
    
                    // Compare the current time with the time the user had set
                    // If they are the same, then start playing the CD
                    if( (tmeCurrent.wYear == tmeSet.wYear) &&
                        (tmeCurrent.wMonth == tmeSet.wMonth) &&
                        //(tmeCurrent.wDayOfWeek == tmeSet.wDayOfWeek) &&
                        (tmeCurrent.wDay == tmeSet.wDay) &&
                        (tmeCurrent.wHour == tmeSet.wHour) &&
                        (tmeCurrent.wMinute == tmeSet.wMinute) /* &&
                        (tmeCurrent.wSecond == tmeSet.wSecond) &&
                        (tmeCurrent.wMilliseconds == tmeSet.wMilliseconds) */ )
                        {
                          mciSendString("play cdaudio", NULL, 0, NULL);
                        }
                    break;
    
                case WM_CLOSE:
                      PostQuitMessage(WM_QUIT);
                      break;
    
                    case WM_DESTROY:
                          // However the user decides to close the dialog box,
                          // stop playing the CD
                          mciSendString("stop cdaudio", NULL, 0, NULL);
                          KillTimer(hWndDlg, IDC_CHECKTIME);
                      break;
              }
    
              return FALSE;
      }
    //---------------------------------------------------------------------------
  30. Put a CD in the CD drive but don't play it
  31. Execute the application
  32. Set the time to 2 minutes from  the current time of the computer and click Set
  33. After less than two minutes, you should hear some music
  34. Press Esc to close the application 

Home