The only difference between this code and the code from the native code chapter is the use of g_hInst instead of hInst in ShowWindows( ).
1. Start the OS and run the CoreCon files, if stopped
2. Refocus on the second instance of Visual Studio 2012.
3. Select the Solution in Solution Explorer
4. File—>Add—>New Project
5. Navigate to Visual C++/Windows Embedded Compact/SDK1
6. Choose Win32 Windows Application and call it TimeWinFormSDKApp.
7. Generate the project
8. Set the project to be built in Debug mode (default).
9. Build and run the application: Right-click on the project, Debug—>Start New Instance
10. Close the app: Debug—>Stop Debugging
11. Open the project’s header file and add:
#define ID_TIMELABEL 500 #define ID_TIMETEXTBOX 501 #define ID_TIMEBUTTON 502 #define ID_EXITBUTTON 503
#define ID_TIMELABEL 500
#define ID_TIMETEXTBOX 501
#define ID_TIMEBUTTON 502
#define ID_EXITBUTTON 503
Listing 16.10 UI message defines for controls for Winform application in header file (TimeWinformSDK.h).
12. 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 16.11 UI control instance declarations for Winform in application code source file (TimeWinformSDK.cpp).
These are the Windows handles for those controls, which are created when the controls are generated.
13. In the InitInstance function, find the line of code: 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 g_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, g_hInst, NULL); SetWindowText(hwndTimeText, _T("Now")); //How to change the textbox contents
//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
g_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,
g_hInst,
NULL);
SetWindowText(hwndTimeText, _T("Now")); //How to change the textbox contents
Listing 16.12 The code to create the label and textbox in the Winform in application code source file.
This will create the label and textbox as part of the application’s main form
14. Build and test run application the application to check that these controls do display.
15. Add the two textboxes below the code above:
//The Time Button x = 10; y +=h+20; h= 30; w = 200; //Place time button below label hwndTimeButton = CreateWindow( _T("BUTTON"), _T("Get Time"), WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON, x, y, w, h, hWnd, (HMENU)ID_TIMEBUTTON, g_hInst, NULL); //The Exit Button x = 10; y +=h+20; h= 30; w = 200; //Place exit button below time button hwndExitButton = CreateWindowEx( 0, _T("BUTTON"), _T("Exit"), WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON, x, y, w, h, hWnd, (HMENU)ID_EXITBUTTON, g_hInst, NULL);
//The Time Button
x = 10; y +=h+20; h= 30; w = 200; //Place time button below label
hwndTimeButton = CreateWindow(
_T("BUTTON"),
_T("Get Time"),
WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
(HMENU)ID_TIMEBUTTON,
g_hInst, NULL);
//The Exit Button
x = 10; y +=h+20; h= 30; w = 200; //Place exit button below time button
hwndExitButton = CreateWindowEx( 0,
_T("Exit"),
(HMENU)ID_EXITBUTTON,
Listing 16.13 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.
16. Again build and test the application to check that the controls display correctly:
Figure 16.12: Hello WinForm application user interface controls displayed on target device.
17. In WndProc function add the TimeButton and ExitButton case branches to the message switch (wmId) statement in WM_COMMAND as in Listing 16.14 .
case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // Parse the button presses: switch (wmId) case ID_TIMEBUTTON: //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/YYY] %02d/%02d/%04d"), st.wHour,st.wMinute,st.wSecond, st.wDay, st.wMonth, st.wYear ); SetWindowText(hwndTimeText, dateTime); break; case ID_EXITBUTTON: DestroyWindow(hWnd); break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the button presses:
switch (wmId)
case ID_TIMEBUTTON:
//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/YYY] %02d/%02d/%04d"),
st.wHour,st.wMinute,st.wSecond, st.wDay,
st.wMonth, st.wYear
);
SetWindowText(hwndTimeText, dateTime);
break;
case ID_EXITBUTTON:
DestroyWindow(hWnd);
Listing 16.14 The code capture the button press messages in the Winform in application code source file.
18. Build and test the app:
Figure 16.13: TimeWinformSDKApp application displaying the Local Time.
NEXT: 16.9 Using the SDK within the OS Solution
Click here to provide feedback and input