Step 4.)

Add remaining functionality

Now we need to create a way for the user to register the application. The Nag display will also be the form used for registration. For this we will modify the stock About box, adding edit controls. Also a new function will be added that allows the OK button to be disabled for a specified time, creating the Nag effect. 

a) Add the following private member variables to the class CAboutDlg:

    CRedRegImpl I_RedReg; 
    bool m_bRegistered; 
    int iTimeLeft; 
    UINT u_TimerID;

b) Add the following member functions to the class CAboutDlg:

    protected: 
        virtual void OnCancel();

    public: 
        void IsRegistered( bool bReg ); 
        bool IsRegistered( void ); 
        void SetNagTimer( int NumSeconds );

    private: 
        void SetupRegView();

c) Initialize the variables as follows in the constructor CAboutDlg::CAboutDlg()

    u_TimerID = 0; // Init to inactive 
    iTimeLeft = 0; // Init to 0 seconds on nag 
    m_bRegistered = false;

d) Open the dialog resource IDD_ABOUTBOX, and resize to 235x95. 

e) Add three Edit controls, with ID's IDC_USERNAME, IDC_COMPANY, and IDC_REGCODE. Add the appropriate static labels. 

f) Add a Push Button, IDC_REGBUTTON and caption "&Register" 

g) Open the ClassWizard, and add the following message map handlers:

    "BN_CLICKED" for the IDC_REGBUTTON named "OnRegButtonClick" 
    "WM_INITDIALOG" for CAboutDlg named "OnInitDialog" 
    "WM_TIMER" for CAboutDlg name "OnTimer"

h) In the ClassWizard, add the following member variables:

    IDC_COMPANY = CString m_sCompany 
    IDC_REGCODE = CString m_sRegCode 
    IDC_USERNAME = CString m_sUserName 
    IDC_REGBUTTON = CButton m_RegButton 
    IDOK = CButton m_OKButton

i) Add the following function implementations:

    bool CAboutDlg::IsRegistered() { 
   
     return m_bRegistered; 
    }

    void CAboutDlg::IsRegistered(bool bReg) { 
        m_bRegistered = bReg; 
    }

    BOOL CAboutDlg::OnInitDialog() {
    _TCHAR sTemp[20]; 
        CDialog::OnInitDialog();
        // Set the screen 
        SetupRegView();
        // Test for Nag Screen view 
        if ( iTimeLeft != 0 ) { 
            u_TimerID = SetTimer( 1,1000,NULL); 
            // Init a timer, and set the timer value to Fire Every Second. 
            if ( u_TimerID != 0 ) {
                m_OKButton.EnableWindow(FALSE); 
                wsprintf(sTemp,"%ld",iTimeLeft); 
                m_OKButton.SetWindowText(sTemp); 
            } 
   
        
   
         return TRUE; 
    }

    void CAboutDlg::OnCancel() { 
        if ( iTimeLeft != 0 ) {}
        else 
            CDialog::OnCancel(); 
    }

    void CAboutDlg::OnRegButtonClick() { 
    bool bRetVal;
   
     UpdateData(TRUE); // Retrieve the data
   
     // Test if Registration was valid 
     bRetVal = i_RedReg.Registered(m_sCompany,m_sUserName,m_sRegCode);

    if ( bRetVal ) { // User is Registered, let's save it to the registry. 
        bRetVal = i_RedReg.Save(); 
        if ( !bRetVal ) { // Saved failed, but we are registered. 
            AfxMessageBox( _T("Registration Approved. Save Failed.") ); 
        } 
        else 
   
             AfxMessageBox( _T("Registration Approved. Thank you for Registering.") );
   
         EndDialog(true); 
    } 
   
else 
        AfxMessageBox( _T("Registration Invalid. Please Recheck your Registration Code.") ); 
    }

    void CAboutDlg::OnTimer(UINT nIDEvent) { 
    _TCHAR sTemp[20]; 
        if (nIDEvent == u_TimerID) { 
            if ( iTimeLeft ) { 
                iTimeLeft--; 
                wsprintf(sTemp,"%ld",iTimeLeft); 
                m_OKButton.SetWindowText(sTemp); 
            } 
            else
                KillTimer(u_TimerID); 
                m_OKButton.SetWindowText(_T("&OK") ); 
                m_OKButton.EnableWindow(TRUE); 
                u_TimerID = 0; 
                iTimeLeft = 0; 
            } 
        } 
        else 
            CDialog::OnTimer(nIDEvent); 
   
}

    void CAboutDlg::SetNagTimer(int NumSeconds) { 
        iTimeLeft = NumSeconds; // Init Timer to value passed. 
    }

    void CAboutDlg::SetupRegView() { 
        if ( !m_bRegistered ) { 
            bool bRetVal = i_RedReg.InitRedReg(); 
            if ( bRetVal ) 
                m_bRegistered = i_RedReg.Registered(); 
        } 
        if ( m_bRegistered ) { 
            m_RegButton.ShowWindow( SW_HIDE );
            m_sUserName = i_RedReg.GetUserName(); 
            m_sCompany = i_RedReg.GetCompanyName(); 
            m_sRegCode = _T("Thank you for Registering"); 
        } 
        else
            m_sUserName = _T("Unlicensed User"); 
            m_sCompany = _T("Unlicnesed"); 
            m_sRegCode = _T("Please Register."); 
        } 
        UpdateData(FALSE); 
    }


    Back             Download Project Files