2010年12月22日 星期三

Draw 9-patch執行時出現錯誤.....

在android下,如果在做Widget做Nine-Patch Images(可伸展圖像)時,執行Draw 9-patch (draw9patch.bat)出現如下圖所示的錯誤出現,這是因為SDK 2.3版中少了一個檔案。

2010年12月7日 星期二

Windows CE: 控制直流馬達應用程式

接續前一篇文章,我們利用下列幾個函式來控制馬達
1. 開啟驅動程式
m_hdcmotor = CreateFile(_T("DCM1:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
2. 啟動馬達
DeviceIoControl(m_hdcmotor, DC_SPEED_START, NULL, NULL, NULL, NULL, NULL, NULL);
3. 停止馬達
DeviceIoControl(m_hdcmotor, DC_SPEED_STOP, NULL, NULL, NULL, NULL, NULL, NULL);
4. 速度控制
DeviceIoControl(m_hdcmotor, DC_SPEED_INCREASE, &m_currdcmotor, NULL, NULL, NULL, NULL, NULL);

以上為馬達控制程式列表
// dcMotor.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "dcMotor.h"
#include
#include

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE g_hInst; // current instance
HWND g_hWndMenuBar; // menu bar handle

// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE, LPTSTR);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int dcStatus = 0; //0-5
HANDLE hBitmap[6];
HANDLE hStart;
HANDLE hStop;
int run=1;
HANDLE m_hdcmotor;
int m_currdcmotor = 80;
enum{
DC_SPEED_INCREASE = 0x18,
DC_SPEED_DECREASE ,
DC_SPEED_STOP = 0x20,
DC_SPEED_START
};

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}

HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_DCMOTOR));

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}

//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;

wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DCMOTOR));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;

return RegisterClass(&wc);
}

//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[MAX_LOADSTRING]; // title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // main window class name

g_hInst = hInstance; // Store instance handle in our global variable

// SHInitExtraControls should be called once during your application's initialization to initialize any
// of the device specific controls such as CAPEDIT and SIPPREF.
SHInitExtraControls();

LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_DCMOTOR, szWindowClass, MAX_LOADSTRING);

//If it is already running, then focus on the window, and exit
hWnd = FindWindow(szWindowClass, szTitle);
if (hWnd)
{
// set focus to foremost child window
// The "| 0x00000001" is used to bring any owned windows to the foreground and
// activate them.
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
return 0;
}

if (!MyRegisterClass(hInstance, szWindowClass))
{
return FALSE;
}

hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

// When the main window is created using CW_USEDEFAULT the height of the menubar (if one
// is created is not taken into account). So we resize the window after creating it
// if a menubar is present
if (g_hWndMenuBar)
{
RECT rc;
RECT rcMenuBar;

GetWindowRect(hWnd, &rc);
GetWindowRect(g_hWndMenuBar, &rcMenuBar);
rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);

MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);


return TRUE;
}

//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;

static SHACTIVATEINFO s_sai;

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_HELP_ABOUT:
DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
break;
case IDM_OK:
SendMessage (hWnd, WM_CLOSE, 0, 0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
SHMENUBARINFO mbi;

memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hWnd;
mbi.nToolBarId = IDR_MENU;
mbi.hInstRes = g_hInst;

if (!SHCreateMenuBar(&mbi))
{
g_hWndMenuBar = NULL;
}
else
{
g_hWndMenuBar = mbi.hwndMB;
}

// Initialize the shell activate info structure
memset(&s_sai, 0, sizeof (s_sai));
s_sai.cbSize = sizeof (s_sai);
for(int i=0; i<6; i++)
hBitmap[i] = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1+i));
hStart = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_START));
hStop = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_STOP));
SetTimer(hWnd, 1, 200, NULL);
m_hdcmotor = CreateFile(_T("DCM1:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
if(m_hdcmotor == INVALID_HANDLE_VALUE )
{
MessageBox(NULL,L"Open m_hdcmotor Driver error!",NULL,MB_OK);
}
DeviceIoControl(m_hdcmotor, DC_SPEED_START, NULL, NULL, NULL, NULL, NULL, NULL);

break;
case WM_TIMER:
dcStatus = (dcStatus+1) % 6;
DeviceIoControl(m_hdcmotor, DC_SPEED_START, NULL, NULL, NULL, NULL, NULL, NULL);
DeviceIoControl(m_hdcmotor, DC_SPEED_INCREASE, &m_currdcmotor, NULL, NULL, NULL, NULL, NULL);
InvalidateRect(hWnd, NULL, FALSE);
break;
case WM_LBUTTONDOWN:
int x,y;
x=LOWORD(lParam);
y=HIWORD(lParam);
if(x>=150 && x<=204 && y>=10 && y<=64)
run = 1-run;
if(run)
{
SetTimer(hWnd, 1, 200, NULL);
DeviceIoControl(m_hdcmotor, DC_SPEED_START, NULL, NULL, NULL, NULL, NULL, NULL); DeviceIoControl(m_hdcmotor, DC_SPEED_INCREASE, &m_currdcmotor, NULL, NULL, NULL, NULL, NULL);
}

else
{
KillTimer(hWnd, 1);
DeviceIoControl(m_hdcmotor, DC_SPEED_STOP, NULL, NULL, NULL, NULL, NULL, NULL); }
InvalidateRect(hWnd, NULL, FALSE);
break;
case WM_PAINT:
HDC hdcMem;
HANDLE hOldSel;
BITMAP bmp;
hdc = BeginPaint(hWnd, &ps);

// TODO: Add any drawing code here...
hdcMem = CreateCompatibleDC(hdc);
hOldSel = SelectObject(hdcMem, (HGDIOBJ) hBitmap[dcStatus]);
GetObject(hBitmap[dcStatus], sizeof(BITMAP), &bmp);
BitBlt(hdc, 10,10, bmp.bmWidth, bmp.bmHeight, hdcMem, 0,0, SRCCOPY);
if(run)
{
SelectObject(hdcMem, (HGDIOBJ) hStop);
GetObject(hStop, sizeof(BITMAP), &bmp);
}
else
{
SelectObject(hdcMem, (HGDIOBJ) hStart);
GetObject(hStart, sizeof(BITMAP), &bmp);
}

BitBlt(hdc, 150,10, bmp.bmWidth, bmp.bmHeight, hdcMem, 0,0, SRCCOPY);

SelectObject(hdcMem, hOldSel);
DeleteDC(hdcMem);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CommandBar_Destroy(g_hWndMenuBar);
PostQuitMessage(0);
break;

case WM_ACTIVATE:
// Notify shell of our activate message
SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
break;
case WM_SETTINGCHANGE:
SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
// Create a Done button and size it.
SHINITDLGINFO shidi;
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
shidi.hDlg = hDlg;
SHInitDialog(&shidi);
}
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;

case WM_CLOSE:
EndDialog(hDlg, message);
return TRUE;

}
return (INT_PTR)FALSE;
}

Windows CE: 增加按鈕來啟動計時器或關閉計時器

接續2010年12月1日星期三的文章,其步驟如下:
1. 再增加兩張圖分別設定其ID值為IDB_START和IDB_STOP。
2. 宣告三個變數值
HANDLE hStart; //儲存開始位元圖的代碼
HANDLE hStop; //儲存停止位元圖的代碼
int run=1; //記錄運轉或停止
3. 在WM_CREATE訊息下,載入位元圖
hStart = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_START));
hStop = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_STOP));
4. 增加WM_LBUTTONDOWN訊息處理程式
case WM_LBUTTONDOWN:
int x,y;
x=LOWORD(lParam);
y=HIWORD(lParam);
if(x>=150 && x<=204 && y>=10 && y<=64)
run = 1-run;
if(run)
SetTimer(hWnd, 1, 200, NULL);
else
KillTimer(hWnd, 1);
InvalidateRect(hWnd, NULL, FALSE);
break;

5.在WM_PAINT訊息下,顯示開始或停止的圖片
if(run)
{
SelectObject(hdcMem, (HGDIOBJ) hStop);
GetObject(hStop, sizeof(BITMAP), &bmp);
}
else
{
SelectObject(hdcMem, (HGDIOBJ) hStart);
GetObject(hStart, sizeof(BITMAP), &bmp);
}

BitBlt(hdc, 150,10, bmp.bmWidth, bmp.bmHeight, hdcMem, 0,0, SRCCOPY);
6. 完整程式列表
// dcMotor.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "dcMotor.h"
#include
#include

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE g_hInst; // current instance
HWND g_hWndMenuBar; // menu bar handle

// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE, LPTSTR);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int dcStatus = 0; //0-5
HANDLE hBitmap[6];
HANDLE hStart;
HANDLE hStop;
int run=1;

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}

HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_DCMOTOR));

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}

//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;

wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DCMOTOR));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;

return RegisterClass(&wc);
}

//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[MAX_LOADSTRING]; // title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // main window class name

g_hInst = hInstance; // Store instance handle in our global variable

// SHInitExtraControls should be called once during your application's initialization to initialize any
// of the device specific controls such as CAPEDIT and SIPPREF.
SHInitExtraControls();

LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_DCMOTOR, szWindowClass, MAX_LOADSTRING);

//If it is already running, then focus on the window, and exit
hWnd = FindWindow(szWindowClass, szTitle);
if (hWnd)
{
// set focus to foremost child window
// The "| 0x00000001" is used to bring any owned windows to the foreground and
// activate them.
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
return 0;
}

if (!MyRegisterClass(hInstance, szWindowClass))
{
return FALSE;
}

hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

// When the main window is created using CW_USEDEFAULT the height of the menubar (if one
// is created is not taken into account). So we resize the window after creating it
// if a menubar is present
if (g_hWndMenuBar)
{
RECT rc;
RECT rcMenuBar;

GetWindowRect(hWnd, &rc);
GetWindowRect(g_hWndMenuBar, &rcMenuBar);
rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);

MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);


return TRUE;
}

//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;

static SHACTIVATEINFO s_sai;

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_HELP_ABOUT:
DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
break;
case IDM_OK:
SendMessage (hWnd, WM_CLOSE, 0, 0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
SHMENUBARINFO mbi;

memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hWnd;
mbi.nToolBarId = IDR_MENU;
mbi.hInstRes = g_hInst;

if (!SHCreateMenuBar(&mbi))
{
g_hWndMenuBar = NULL;
}
else
{
g_hWndMenuBar = mbi.hwndMB;
}

// Initialize the shell activate info structure
memset(&s_sai, 0, sizeof (s_sai));
s_sai.cbSize = sizeof (s_sai);
for(int i=0; i<6; i++)
hBitmap[i] = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1+i));
hStart = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_START));
hStop = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_STOP));

SetTimer(hWnd, 1, 200, NULL);
break;
case WM_TIMER:
dcStatus = (dcStatus+1) % 6;
InvalidateRect(hWnd, NULL, FALSE);
break;
case WM_LBUTTONDOWN:
int x,y;
x=LOWORD(lParam);
y=HIWORD(lParam);
if(x>=150 && x<=204 && y>=10 && y<=64)
run = 1-run;
if(run)
SetTimer(hWnd, 1, 200, NULL);
else
KillTimer(hWnd, 1);
InvalidateRect(hWnd, NULL, FALSE);
break;

case WM_PAINT:
HDC hdcMem;
HANDLE hOldSel;
BITMAP bmp;
hdc = BeginPaint(hWnd, &ps);

// TODO: Add any drawing code here...
hdcMem = CreateCompatibleDC(hdc);
hOldSel = SelectObject(hdcMem, (HGDIOBJ) hBitmap[dcStatus]);
GetObject(hBitmap[dcStatus], sizeof(BITMAP), &bmp);
BitBlt(hdc, 10,10, bmp.bmWidth, bmp.bmHeight, hdcMem, 0,0, SRCCOPY);
if(run)
{
SelectObject(hdcMem, (HGDIOBJ) hStop);
GetObject(hStop, sizeof(BITMAP), &bmp);
}
else
{
SelectObject(hdcMem, (HGDIOBJ) hStart);
GetObject(hStart, sizeof(BITMAP), &bmp);
}

BitBlt(hdc, 150,10, bmp.bmWidth, bmp.bmHeight, hdcMem, 0,0, SRCCOPY);

SelectObject(hdcMem, hOldSel);
DeleteDC(hdcMem);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CommandBar_Destroy(g_hWndMenuBar);
PostQuitMessage(0);
break;

case WM_ACTIVATE:
// Notify shell of our activate message
SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
break;
case WM_SETTINGCHANGE:
SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
// Create a Done button and size it.
SHINITDLGINFO shidi;
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
shidi.hDlg = hDlg;
SHInitDialog(&shidi);
}
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;

case WM_CLOSE:
EndDialog(hDlg, message);
return TRUE;

}
return (INT_PTR)FALSE;
}

Google公布了Android 2.3 Platform

Google昨天公佈了Android SDK最新版2.3 (薑餅人),大家可以進行更新了:
http://developer.android.com/sdk/index.html


2010年12月1日 星期三

Windows CE: 利用計時器畫出馬達旋轉的圖

1. 建立dcMotor專案(Smart Device, Win 32)
2. 在資源檔中畫六張圖可呈現動畫, 您也可以將下列圖片下載, 放到dcMotor專案的res目錄下, 然後利用Import功能將圖片滙入。






3. 利用dcStatus來記綠馬達狀態, hBitmap可用來儲存六張圖片的資源代碼
int dcStatus = 0; //0-5
HANDLE hBitmap[6];
4. 程式列表(粗體字是新增部份)

// dcMotor.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "dcMotor.h"
#include
#include

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE g_hInst; // current instance
HWND g_hWndMenuBar; // menu bar handle

// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE, LPTSTR);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int dcStatus = 0; //0-5
HANDLE hBitmap[6];


int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}

HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_DCMOTOR));

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}

//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;

wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DCMOTOR));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;

return RegisterClass(&wc);
}

//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[MAX_LOADSTRING]; // title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // main window class name

g_hInst = hInstance; // Store instance handle in our global variable

// SHInitExtraControls should be called once during your application's initialization to initialize any
// of the device specific controls such as CAPEDIT and SIPPREF.
SHInitExtraControls();

LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_DCMOTOR, szWindowClass, MAX_LOADSTRING);

//If it is already running, then focus on the window, and exit
hWnd = FindWindow(szWindowClass, szTitle);
if (hWnd)
{
// set focus to foremost child window
// The "| 0x00000001" is used to bring any owned windows to the foreground and
// activate them.
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
return 0;
}

if (!MyRegisterClass(hInstance, szWindowClass))
{
return FALSE;
}

hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

// When the main window is created using CW_USEDEFAULT the height of the menubar (if one
// is created is not taken into account). So we resize the window after creating it
// if a menubar is present
if (g_hWndMenuBar)
{
RECT rc;
RECT rcMenuBar;

GetWindowRect(hWnd, &rc);
GetWindowRect(g_hWndMenuBar, &rcMenuBar);
rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);

MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);


return TRUE;
}

//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;

static SHACTIVATEINFO s_sai;

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_HELP_ABOUT:
DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
break;
case IDM_OK:
SendMessage (hWnd, WM_CLOSE, 0, 0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
SHMENUBARINFO mbi;

memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hWnd;
mbi.nToolBarId = IDR_MENU;
mbi.hInstRes = g_hInst;

if (!SHCreateMenuBar(&mbi))
{
g_hWndMenuBar = NULL;
}
else
{
g_hWndMenuBar = mbi.hwndMB;
}

// Initialize the shell activate info structure
memset(&s_sai, 0, sizeof (s_sai));
s_sai.cbSize = sizeof (s_sai);
for(int i=0; i<6; i++)
hBitmap[i] = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1+i));
SetTimer(hWnd, 1, 100, NULL);

break;
case WM_TIMER:
dcStatus = (dcStatus+1) % 6;
InvalidateRect(hWnd, NULL, FALSE);
break;

case WM_PAINT:
HDC hdcMem;
HANDLE hOldSel;
BITMAP bmp;

hdc = BeginPaint(hWnd, &ps);
hdcMem = CreateCompatibleDC(hdc);
hOldSel = SelectObject(hdcMem, (HGDIOBJ) hBitmap[dcStatus]);
GetObject(hBitmap[dcStatus], sizeof(BITMAP), &bmp);
BitBlt(hdc, 10,10, bmp.bmWidth, bmp.bmHeight, hdcMem, 0,0, SRCCOPY);
SelectObject(hdcMem, hOldSel);
DeleteDC(hdcMem);

EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CommandBar_Destroy(g_hWndMenuBar);
PostQuitMessage(0);
break;

case WM_ACTIVATE:
// Notify shell of our activate message
SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
break;
case WM_SETTINGCHANGE:
SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
// Create a Done button and size it.
SHINITDLGINFO shidi;
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
shidi.hDlg = hDlg;
SHInitDialog(&shidi);
}
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;

case WM_CLOSE:
EndDialog(hDlg, message);
return TRUE;

}
return (INT_PTR)FALSE;
}

2010年11月30日 星期二

Windows CE:四個LED燈分別控制(含硬體驅動)

// android.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "android.h"
#include
#include

#define MAX_LOADSTRING 100

#define LED_ON 0x10
#define LED_OFF 0x11

// Global Variables:
HINSTANCE g_hInst; // current instance
HWND g_hWndMenuBar; // menu bar handle
int LedStatus[4] = {0,0,0,0};
HANDLE m_hled;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE, LPTSTR);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}

HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_ANDROID));

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}

//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;

wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ANDROID));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;

return RegisterClass(&wc);
}

//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[MAX_LOADSTRING]; // title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // main window class name

g_hInst = hInstance; // Store instance handle in our global variable

// SHInitExtraControls should be called once during your application's initialization to initialize any
// of the device specific controls such as CAPEDIT and SIPPREF.
SHInitExtraControls();

LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_ANDROID, szWindowClass, MAX_LOADSTRING);

//If it is already running, then focus on the window, and exit
hWnd = FindWindow(szWindowClass, szTitle);
if (hWnd)
{
// set focus to foremost child window
// The "| 0x00000001" is used to bring any owned windows to the foreground and
// activate them.
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
return 0;
}

if (!MyRegisterClass(hInstance, szWindowClass))
{
return FALSE;
}

hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

// When the main window is created using CW_USEDEFAULT the height of the menubar (if one
// is created is not taken into account). So we resize the window after creating it
// if a menubar is present
if (g_hWndMenuBar)
{
RECT rc;
RECT rcMenuBar;

GetWindowRect(hWnd, &rc);
GetWindowRect(g_hWndMenuBar, &rcMenuBar);
rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);

MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);


return TRUE;
}

//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
int m_control1=0;

static SHACTIVATEINFO s_sai;

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_HELP_ABOUT:
DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
break;
case IDM_OK:
SendMessage (hWnd, WM_CLOSE, 0, 0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
SHMENUBARINFO mbi;

memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hWnd;
mbi.nToolBarId = IDR_MENU;
mbi.hInstRes = g_hInst;

if (!SHCreateMenuBar(&mbi))
{
g_hWndMenuBar = NULL;
}
else
{
g_hWndMenuBar = mbi.hwndMB;
}

// Initialize the shell activate info structure
memset(&s_sai, 0, sizeof (s_sai));
s_sai.cbSize = sizeof (s_sai);
m_hled = CreateFile(TEXT("LED1:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
if(m_hled == INVALID_HANDLE_VALUE ) {
MessageBox(NULL,L"Open LED Driver error!",NULL,MB_OK);
}
break;
case WM_LBUTTONDOWN :
int x,y;
x=LOWORD(lParam);
y=HIWORD(lParam);
if(x<=200)
{
LedStatus[x/50] = 1-LedStatus[x/50];
if(LedStatus[x/50])
{
m_control1=x/50;
DeviceIoControl(m_hled, LED_ON, &m_control1, NULL, NULL, NULL, NULL, NULL);
}
else
{
m_control1=x/50;
DeviceIoControl(m_hled, LED_OFF, &m_control1, NULL, NULL, NULL, NULL, NULL);

}
InvalidateRect(hWnd, NULL, TRUE);
}
break;
case WM_PAINT:
HDC hdcMem;
HANDLE hOldSel, hBitmap;
BITMAP bmp;
hdc = BeginPaint(hWnd, &ps);
hdcMem = CreateCompatibleDC(hdc);
hOldSel = SelectObject(hdcMem, (HGDIOBJ) hBitmap);

// TODO: Add any drawing code here...
for(int i=0; i<4; i++)
{
if(LedStatus[i])
hBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1));
else
hBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP2));
SelectObject(hdcMem, (HGDIOBJ) hBitmap);
GetObject(hBitmap, sizeof(BITMAP), &bmp);
BitBlt(hdc, i*50, 10, bmp.bmWidth, bmp.bmHeight, hdcMem, 0,0, SRCCOPY);
}
SelectObject(hdcMem, hOldSel);
DeleteDC(hdcMem); EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CommandBar_Destroy(g_hWndMenuBar);
PostQuitMessage(0);
break;

case WM_ACTIVATE:
// Notify shell of our activate message
SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
break;
case WM_SETTINGCHANGE:
SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
// Create a Done button and size it.
SHINITDLGINFO shidi;
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
shidi.hDlg = hDlg;
SHInitDialog(&shidi);
}
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;

case WM_CLOSE:
EndDialog(hDlg, message);
return TRUE;

}
return (INT_PTR)FALSE;
}

Windows CE:四個LED燈分別控制



// LED.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "LED.h"
#include
#include

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE g_hInst; // current instance
HWND g_hWndMenuBar; // menu bar handle
int LedStatus[4] = {0,0,0,0};

// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE, LPTSTR);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}

HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LED));

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}

//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;

wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LED));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;

return RegisterClass(&wc);
}

//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[MAX_LOADSTRING]; // title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // main window class name

g_hInst = hInstance; // Store instance handle in our global variable

// SHInitExtraControls should be called once during your application's initialization to initialize any
// of the device specific controls such as CAPEDIT and SIPPREF.
SHInitExtraControls();

LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_LED, szWindowClass, MAX_LOADSTRING);

//If it is already running, then focus on the window, and exit
hWnd = FindWindow(szWindowClass, szTitle);
if (hWnd)
{
// set focus to foremost child window
// The "| 0x00000001" is used to bring any owned windows to the foreground and
// activate them.
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
return 0;
}

if (!MyRegisterClass(hInstance, szWindowClass))
{
return FALSE;
}

hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

// When the main window is created using CW_USEDEFAULT the height of the menubar (if one
// is created is not taken into account). So we resize the window after creating it
// if a menubar is present
if (g_hWndMenuBar)
{
RECT rc;
RECT rcMenuBar;

GetWindowRect(hWnd, &rc);
GetWindowRect(g_hWndMenuBar, &rcMenuBar);
rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);

MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);


return TRUE;
}

//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;

static SHACTIVATEINFO s_sai;

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_HELP_ABOUT:
DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
break;
case IDM_OK:
SendMessage (hWnd, WM_CLOSE, 0, 0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
SHMENUBARINFO mbi;

memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hWnd;
mbi.nToolBarId = IDR_MENU;
mbi.hInstRes = g_hInst;

if (!SHCreateMenuBar(&mbi))
{
g_hWndMenuBar = NULL;
}
else
{
g_hWndMenuBar = mbi.hwndMB;
}

// Initialize the shell activate info structure
memset(&s_sai, 0, sizeof (s_sai));
s_sai.cbSize = sizeof (s_sai);
break;
case WM_LBUTTONDOWN :
int x,y;
x=LOWORD(lParam);
y=HIWORD(lParam);
if(x<=200)
{
LedStatus[x/50] = 1-LedStatus[x/50];
InvalidateRect(hWnd, NULL, TRUE);
}
break;

case WM_PAINT:
HDC hdcMem;
HANDLE hOldSel, hBitmap;
BITMAP bmp;

hdc = BeginPaint(hWnd, &ps);

hdcMem = CreateCompatibleDC(hdc);
hOldSel = SelectObject(hdcMem, (HGDIOBJ) hBitmap);

// TODO: Add any drawing code here...
for(int i=0; i<4; i++)
{
if(LedStatus[i])
hBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1));
else
hBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP2));
SelectObject(hdcMem, (HGDIOBJ) hBitmap);
GetObject(hBitmap, sizeof(BITMAP), &bmp);
BitBlt(hdc, i*50, 10, bmp.bmWidth, bmp.bmHeight, hdcMem, 0,0, SRCCOPY);
}
SelectObject(hdcMem, hOldSel);
DeleteDC(hdcMem);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CommandBar_Destroy(g_hWndMenuBar);
PostQuitMessage(0);
break;

case WM_ACTIVATE:
// Notify shell of our activate message
SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
break;
case WM_SETTINGCHANGE:
SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
// Create a Done button and size it.
SHINITDLGINFO shidi;
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
shidi.hDlg = hDlg;
SHInitDialog(&shidi);
}
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;

case WM_CLOSE:
EndDialog(hDlg, message);
return TRUE;

}
return (INT_PTR)FALSE;
}

2010年11月24日 星期三

使用eclipse撰寫第一支Java程式的步驟

1.選擇File->New->Java Project
2. 為專案命名,按下Finish鍵。
3.在src目錄下按下右鍵,選擇New->Class
4. 為class命名,並選擇產生main函式再按Finish鍵
5.增加程式System.out.print
6.按下執行鈕
7.選擇OK鈕
8. 察看結果

WinCE 位元圖切換

完成的功能如下面這兩張圖。


其步驟如下:
1. 請利用Visual C++的Win 32 Smart Device專案。

2. 新增兩個位元圖。


3. 撰寫下列程式(粗體字部份)

// LED.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "LED.h"
#include
#include

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE g_hInst; // current instance
HWND g_hWndMenuBar; // menu bar handle
int LedStatus = 0;

// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE, LPTSTR);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}

HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LED));

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}

//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;

wc.style = CS_HREDRAW CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LED));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;

return RegisterClass(&wc);
}

//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[MAX_LOADSTRING]; // title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // main window class name

g_hInst = hInstance; // Store instance handle in our global variable

// SHInitExtraControls should be called once during your application's initialization to initialize any
// of the device specific controls such as CAPEDIT and SIPPREF.
SHInitExtraControls();

LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_LED, szWindowClass, MAX_LOADSTRING);

//If it is already running, then focus on the window, and exit
hWnd = FindWindow(szWindowClass, szTitle);
if (hWnd)
{
// set focus to foremost child window
// The " 0x00000001" is used to bring any owned windows to the foreground and
// activate them.
SetForegroundWindow((HWND)((ULONG) hWnd 0x00000001));
return 0;
}

if (!MyRegisterClass(hInstance, szWindowClass))
{
return FALSE;
}

hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

// When the main window is created using CW_USEDEFAULT the height of the menubar (if one
// is created is not taken into account). So we resize the window after creating it
// if a menubar is present
if (g_hWndMenuBar)
{
RECT rc;
RECT rcMenuBar;

GetWindowRect(hWnd, &rc);
GetWindowRect(g_hWndMenuBar, &rcMenuBar);
rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);

MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);


return TRUE;
}

//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;

static SHACTIVATEINFO s_sai;

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_HELP_ABOUT:
DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
break;
case IDM_OK:
SendMessage (hWnd, WM_CLOSE, 0, 0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
SHMENUBARINFO mbi;

memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hWnd;
mbi.nToolBarId = IDR_MENU;
mbi.hInstRes = g_hInst;

if (!SHCreateMenuBar(&mbi))
{
g_hWndMenuBar = NULL;
}
else
{
g_hWndMenuBar = mbi.hwndMB;
}

// Initialize the shell activate info structure
memset(&s_sai, 0, sizeof (s_sai));
s_sai.cbSize = sizeof (s_sai);
break;
case WM_LBUTTONDOWN :
LedStatus = 1-LedStatus;
InvalidateRect(hWnd, NULL, TRUE);
break;

case WM_PAINT:
HDC hdcMem;
HANDLE hOldSel, hBitmap;
BITMAP bmp;


hdc = BeginPaint(hWnd, &ps);if(LedStatus)
hBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1));
else
hBitmap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP2));
hdcMem = CreateCompatibleDC(hdc);
hOldSel = SelectObject(hdcMem, (HGDIOBJ) hBitmap);
GetObject(hBitmap, sizeof(BITMAP), &bmp);
BitBlt(hdc, 10,10, bmp.bmWidth, bmp.bmHeight, hdcMem, 0,0, SRCCOPY);
SelectObject(hdcMem, hOldSel);
DeleteDC(hdcMem);

EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CommandBar_Destroy(g_hWndMenuBar);
PostQuitMessage(0);
break;

case WM_ACTIVATE:
// Notify shell of our activate message
SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
break;
case WM_SETTINGCHANGE:
SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
// Create a Done button and size it.
SHINITDLGINFO shidi;
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON SHIDIF_SIPDOWN SHIDIF_SIZEDLGFULLSCREEN SHIDIF_EMPTYMENU;
shidi.hDlg = hDlg;
SHInitDialog(&shidi);
}
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;

case WM_CLOSE:
EndDialog(hDlg, message);
return TRUE;

}
return (INT_PTR)FALSE;
}

2010年11月23日 星期二

DMA2440平台Win CE LED控制程式


先利用Visual Studio建立Visual C++的Smart Device之LED專案,其功能是按下mouse左鍵關燈放開則點亮,程式碼如下「新增部份以粗體字表示」:

// LED.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "LED.h"
#include
#include

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE g_hInst; // current instance
HWND g_hWndMenuBar; // menu bar handle
HANDLE m_hled;
#define LED_ON 0x10
#define LED_OFF 0x11


// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE, LPTSTR);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

// Perform application initialization:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}

HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LED));

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}

//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;

wc.style = CS_HREDRAW CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LED));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;

return RegisterClass(&wc);
}

//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[MAX_LOADSTRING]; // title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // main window class name

g_hInst = hInstance; // Store instance handle in our global variable

// SHInitExtraControls should be called once during your application's initialization to initialize any
// of the device specific controls such as CAPEDIT and SIPPREF.
SHInitExtraControls();

LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_LED, szWindowClass, MAX_LOADSTRING);

//If it is already running, then focus on the window, and exit
hWnd = FindWindow(szWindowClass, szTitle);
if (hWnd)
{
// set focus to foremost child window
// The " 0x00000001" is used to bring any owned windows to the foreground and
// activate them.
SetForegroundWindow((HWND)((ULONG) hWnd 0x00000001));
return 0;
}

if (!MyRegisterClass(hInstance, szWindowClass))
{
return FALSE;
}

hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

// When the main window is created using CW_USEDEFAULT the height of the menubar (if one
// is created is not taken into account). So we resize the window after creating it
// if a menubar is present
if (g_hWndMenuBar)
{
RECT rc;
RECT rcMenuBar;

GetWindowRect(hWnd, &rc);
GetWindowRect(g_hWndMenuBar, &rcMenuBar);
rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);

MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);


return TRUE;
}

//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
int m_control1=0;

static SHACTIVATEINFO s_sai;

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_HELP_ABOUT:
DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
break;
case IDM_OK:
SendMessage (hWnd, WM_CLOSE, 0, 0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
SHMENUBARINFO mbi;

memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hWnd;
mbi.nToolBarId = IDR_MENU;
mbi.hInstRes = g_hInst;

if (!SHCreateMenuBar(&mbi))
{
g_hWndMenuBar = NULL;
}
else
{
g_hWndMenuBar = mbi.hwndMB;
}

// Initialize the shell activate info structure
memset(&s_sai, 0, sizeof (s_sai));
s_sai.cbSize = sizeof (s_sai);

m_hled = CreateFile(TEXT("LED1:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
if(m_hled == INVALID_HANDLE_VALUE )
{
MessageBox(NULL,L"Open LED Driver error!",NULL,MB_OK);
}


break;
case WM_LBUTTONDOWN:
DeviceIoControl(m_hled, LED_OFF, &m_control1, NULL, NULL, NULL, NULL, NULL);
break;
case WM_LBUTTONUP:
DeviceIoControl(m_hled, LED_ON, &m_control1, NULL, NULL, NULL, NULL, NULL);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);

// TODO: Add any drawing code here...

EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
CommandBar_Destroy(g_hWndMenuBar);
PostQuitMessage(0);
break;

case WM_ACTIVATE:
// Notify shell of our activate message
SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
break;
case WM_SETTINGCHANGE:
SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
// Create a Done button and size it.
SHINITDLGINFO shidi;
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON SHIDIF_SIPDOWN SHIDIF_SIZEDLGFULLSCREEN SHIDIF_EMPTYMENU;
shidi.hDlg = hDlg;
SHInitDialog(&shidi);
}
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;

case WM_CLOSE:
EndDialog(hDlg, message);
return TRUE;

}
return (INT_PTR)FALSE;
}

2010年11月22日 星期一

下載USB HID測試程式及測試USB設備上的LED

USB Central網站上提供許多USB學習資源

點選HID選項

將下載到的generic_hid_vb_46.zip解開

開啟Visual Studio 2005,選擇File選單中的Open之Project/Solution選項,開啟GenericHid。



然後利用USB線將USB發展工具與電腦連接在一起,再打開Cypress USB Control Panel,下載led.hex到發展工具。

按下Visual Studio的執行鈕後,設定Vendor ID及Product ID為1234及5678。

然後按下Find My Device按鈕及One按鈕,就可以觀察USB設備上LED的變化。

2010年11月17日 星期三

產生第一個Win CE程式並上載到DMA2440

1. 啟動Visual studio 2005開始撰寫程式
2.選擇File功能表建立新專案

3.選擇Smart Device 及 Win32 Smart Device Project





4.按下執行鈕




5. 選擇yes




6. 找到執行檔 (注意路徑), 並複製






7.開啟瀏覽裝置















































8. 複製到Temp目錄































9. 最後您可以打開平台上的檔案管理程式去執行程式












安裝ActiveSync驅動程式

1.把DMA2440上的USB熱插拔一下, 安裝ActiveSync USB驅動程式(注意和DNW驅動不同)























2. 安裝ActiveSync軟體