Compact 2013 Ebook

14.7 WinForm GetTime App
Created by djones on 6/18/2013 7:50:26 AM

Buttons & Local Time functionality on a Win32 Form Application

In this activity a label, a textbox and two buttons are added to the to the Winform UI. The label will display the text “Time/Date:” and be positioned to the left of the textbox. The first button, labelled “Get Name”, when pressed will get the local time, as in the console application and write it to the textbox. The second button when pressed will cause the application to close.

Starting with the HelloWinForm project (you might like to exit and make a backup copy of it now):

1. Open the project’s header file and add:

#define ID_TIMELABEL 500
#define ID_TIMETEXTBOX 501
#define ID_TIMEBUTTON 502
#define ID_EXITBUTTON 503

Listing 14.7 UI message defines for controls for Winform application in header file.

Windows at its base level is a messaging system. Messages are passed downwards until a handler accepts them. These values identify the controls and are to identify messages that are targeting them.

The project, as generated may not contain a reference in the source file to its header file. This reference needs to be added.

2. In the source file, near the top, just below
#include “resource.h”
add the following if it hasn’t been included:

#include “HelloWinForm.h”

Listing 14.8 Reference to application header file in for Winform application code source file

3. In the app’s source file find the #define near the top for MAX_LOADSTRING and insert the following just below it:

static HWND hwndTimeLabel, hwndTimeText, hwndTimeButton, hwndExitButton;

Listing 1.9 UI control instance declarations for Winform in application code source file.

These are the Windows handles for those controls, which are created when the controls are generated.

4. In the InitInstance function, find the LOC

    ShowWindow(hwnd, nCmdShow);

and insert the following just above it:
//Time label
int x, w, y, h;
y = 50; h = 20;			//Location of label on top left
x = 10; w = 100;			//Dimensions of time label
hwndTimeLabel = CreateWindow(
	_T("static"),					// GUI ''class'' to create
	_T("Time/Date :"),				// GUI caption
	WS_CHILD | WS_VISIBLE | WS_TABSTOP,		// Control styles separated by| 
	x, y, w, h,					// Location and dimesnsions
	hWnd,						// Parent window handle
	(HMENU)ID_TIMELABEL,				// Control's ID for WM_COMMAND
	hInst,						// Application instance 
	NULL);						// Other function parameters
//Time textbox
x+=w + 10; w=400;			//Place time textbox to right of time label
hwndTimeText = CreateWindow(
	_T("edit"),
	_T( ""),
	WS_CHILD | WS_VISIBLE | WS_TABSTOP| ES_LEFT | WS_BORDER, 
	x, y, w, h,
	hWnd,	
	(HMENU)ID_TIMETEXTBOX, 
	hInst,	
	NULL);	
	
SetWindowText(hwndTimeText, _T("Now"));  //How to change the textbox contents

Listing 14.11 The code to create the two buttons in the Winform in application code source file.

Note the use of CreateWindowEx in the last function call. This gives more options, none of which is used here though.

7. Again build and test the application to check that the controls display correctly:

clip_image002

Figure 14.16: Hello WinForm application user interface controls displayed on target device.

Windows messages for the application are caught in the Main Message Loop in the application’s WinMain function. These are handled by the app’s WndProc callback function. We need to add some handlers for the buttons in the switch statement in that function. We are looking WM_Command messages in the lower word of wParam (wmId) and commands that match our two buttons (ID_TimeButton and ID__ExitButton) in the upper word of wPararm (wmEvent).

8. In WndProc function add the follow case branch to the message switch statement before WM_PAINT:

case WM_COMMAND:
	wmId    = LOWORD(wParam);
	wmEvent = HIWORD(wParam);
	// Parse the button presses:
	switch (wmId)
	{
		case ID_TIMEBUTTON:
			MessageBox(hWnd, _T("You just pushed time!"), 
                            T( "Get Time!"), MB_OK|MB_ICONEXCLAMATION);
			break;
		case ID_EXITBUTTON:
			MessageBox(hWnd, _T("You just pushed exit!"), 
                                    _T( "Get Time!"), MB_OK|MB_ICONEXCLAMATION);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
	}
	break;
	}
break;

Listing 14.12 The code capture the button press messages in the Winform in application code source file.

9. Build and test the button presses. Message boxes should show for each:

clip_image004

Figure 14.17: Hello WinForm application displaying a MessageBox.

10. Replace the Exit Button MessageBox LOC with
DestroyWindow(hWnd);
And then build and test that the Exit button now closes the app.

11. Finally replace the Time Button MessageBox LOC with the following:

//Get the system 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
	);
SetWindowText(hwndTimeText, dateTime);

Listing 14.13 The code to get and display the local time in the Winform in application code source file.

Note: There is no linefeed in this version of the time code after %02d/%02d/%04d.

12. Build and test the app:

clip_image006

Figure 14.18: Hello WinForm application displaying a the Local Time.

 


NEXT: Using a Dynamic Link Library (.DLL)

print

Click here to provide feedback and input

  Comments

There is no comment.

Turkish porno izle video site in rokettubeporno izle