为Compact 2013 创建应用程序可以有两种方式,其一是在Platform Builder 中把应用程序作为子项目添加到Compact 2013 OS映像文件中,其二是在Visual Studio 2012中不用Platform Builder 而用SDK 创建应用程序并部署到OS映像文件中。
在本次项目中使用第二种方式。另外Windows Embedded Compact 2013的应用程序可以使用本地代码C++,或托管代码C# 进行开发。本次采用C++代码进行。
首先增加一个子项目到OS Design
- 在Platform Builder,打开OS design 项目
- 菜单中Project〉Add NewSubproject 并开始子项目创建向导
- 在“Select name,location and templates ”界面中在“Available templates”中选择WCE Application
- 输入子项目名称如helloworld
- 在“Auto-generatedsubproject files ”选择“A simple application”点击Finish。
这样在Solution Explorer 中OS design的Subprojects节点下就有新的子项目出现。
其次修改代码
本次所添加的子项目程序就是显示Hello,World!
- 在“Solution Explorer”界面中,到“Subprojects”>Helloworld〉source files,然后打开helloworld.cpp文件
- 在helloworld.cpp文件中添加代码,并显示Hello,World!,或者你直接复制以下代码到helloworld.cpp文件中
#include "stdafx.h"
TCHAR szTitle[] = TEXT("GettingStarted");
TCHAR szWindowClass[] = TEXT("GettingStarted");
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT,WPARAM, LPARAM);
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow )
{
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
ATOM MyRegisterClass( HINSTANCE hInstance )
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = 0;
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
BOOL InitInstance(HINSTANCE hInstance, intnCmdShow)
{
HWND hWnd = CreateWindow ( szWindowClass, szTitle,
WS_VISIBLE |WS_SYSMENU | WS_CAPTION,
250, 190, 150,75, NULL, NULL, hInstance, NULL );
if(!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINTmessage, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, TEXT("\nHello, World!"), -1, &rt,DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
然后编译子项目
在上次实验中编译创建了OS design 映像文件后,本次子项目就是在此基础上进行的。该子项目编译后就OS design映像文件就包含了应用程序。
- 在“SolutionExplorer”中,展开Subprojects节点,并选择应用程序所在的子项目
- 在菜单栏中选择Build〉Build All Subprojects
下载并运行该应用程序
更新映像文件到测试设备上,当设备起动后运行了新的映像文件,就可以运行HelloWorld程序。
- 在Platform Builder中,选择Target〉Run Programs
- 选择Helloworld 的可执行文件,并点击“Run”
这时在测试设备的桌面上就可以显示Hello,World!字样。应用程序就这样在Windows Embedded Compact 2013的设备上运行起来了。