Compact 2013 Ebook

15.4 Time Button Handler
Created by djones on 6/18/2013 9:32:15 AM

Add some code to the Time Button Event Handler to display a Message Box:

1. In MainPage.cpp modify the TimeButton handler to include a MessageBox
(the code in bold):

//============================================================================
//  TimeButton_Click
// 
//  Description: Event handler implementation
//
//  Parameters:  pSender - The dependency object that raised the click event.
//               pArgs - Event specific arguments.
// //============================================================================
HRESULT MainPage::TimeButton_Click (IXRDependencyObject* pSender, XRMouseButtonEventArgs* pArgs)
{
    HRESULT hr = E_NOTIMPL;
    if ((NULL == pSender) || (NULL == pArgs))
    {
        hr = E_INVALIDARG;
    }
	MessageBox(NULL, _T("Time Clicked"), _T("MyFirstXAMLApp"),
                                            MB_OK|MB_TOPMOST);
    return hr;
}

Listing 15.4: Adding a MessageBox to the Time Button event handler

2. Rebuild the app and rerun it to test the Time Button functionality. Shut the app (not the OS) down before rebuilding, otherwise it won't be able to write the new .exe. Click on the Time Button.

clip_image002

Figure 15.26: Actioning an XAML MessageBox

Note the XAML MessageBox. That is a component in the Catalog:

clip_image004

Figure 15.27: The XAML Message Box Catalog Component

3. Click OK to close the MessageBox.

4. In the TimeButton Event Handler place a breakpoint on the first line of code.

5. Click on the Time Button and note that you can step through its code.
You must have built the subproject in Debug mode (as above) though.

Writing to a TextBox in XAML

We now need to write a string to the textbox.

6. Replace the MessageBox line with:

typedef struct _SYSTEMTIME {
    WORD wYear;
    WORD wMonth;
    WORD wDayOfWeek;
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;

Listing 15.5: Writing to a TextBox in XAML

The reference to the textbox was generated in the function InitializeComponent( )

7. Rebuild the subproject and test this functionality.
Remember to shut the app down before building it.

Accessing Date-Time in Win32

We now want to get access to the system time.

Because we are actually coding in native C++ the standard C++ libraries are available. We will use the structure SYSTEMTIME and the function:
GetSystemTime()
to read the system time into the structure:

typedef struct _SYSTEMTIME {
    WORD wYear;
    WORD wMonth;
    WORD wDayOfWeek;
    WORD wDay;
    WORD wHour;
    WORD wMinute;
    WORD wSecond;
    WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;

Listing 15.6: The SYSTEMTIME Win32 structure

These are defined in winbase.h

8. Replace that same line of code in the Time Button handler with:

//Get the local time
SYSTEMTIME st;
GetLocalTime(&st);
//Write it to the textbox
TCHAR dateTime[100];  //The buffer for the DateTime string
_stprintf(
dateTime,
_T("Time  %02d:%02d:%02d Date [DD/MM/YYYY] %02d/%02d/%04d")
,st.wHour,st.wMinute,st.wSecond, st.wDay, st.wMonth, st.wYear
);
m_pTextBox1->SetText(dateTime);

Listing 15.7: Win32 Native code to get local time and display it in an XAML textbox

9. Rebuild the app and rerun it to test the Time Button functionality:

clip_image006

Figure 15.27a: The running XAML app


NEXT: 15.5 XAML Application Exit Code

print

Click here to provide feedback and input

  Comments

There is no comment.

Turkish porno izle video site in rokettubeporno izle