在Windows mobile系統中,使用者可以通過設定來存取控制面板的應用程式,軟體開發人員也可以通過Windows mobile提供的API函數來存取控制面板的一些資訊,例如可以向其中增加一個控制台的應用。
控制台應用程式實現為一個Dll中,但必須以cpl為尾碼,它匯出一個回呼函數:
LONG CPlApplet(HWND hwndCPl, UINT msg, LPARAM lParam1, LPARAM lParam2);
在使用者點擊設定時,ctlpnl.exe進程會通過調用CPlApplet來發送一些訊息,一個cpl可以支援多個控制台的小程式(applets):
hwndCPl:控制台視窗控制代碼,即是小程式的父視窗。
Msg:控制台程式(ctlpnl.exe)向我們的應用程式發送的訊息,這些訊息決定了應用程式初始化、啟動、停止、退出,主要包括:
CPL_INIT、CPL_GETCOUNT、CPL_NEWINQUIRE、CPL_IDNAME、CPL_DBLCLK、CPL_STOP、CPL_EXIT。
CPL_INIT:初始化訊息,即ctlpnl.exe通知控制台應用程式做一些全域的初始化工作,如記憶體配置。
CPL_GETCOUNT:擷取控制台應用程式支援的小程式個數。
CPL_NEWINQUIRE:查詢控制台應用程式的小程式的資訊。這些資訊包含在NEWCPLINFO結構中。
CPL_IDNAME:獲得控制台應用程式的名稱,通過設定下面的註冊表索引值可以改變應用位於控制台屬性頁面的位置。
[HKEY_LOCAL_MACHINE/ControlPanel/<ID name>]
.
"Group" = dword:1
Group value |
Settings tab where CPL exists
|
0 |
Personal |
1 (default value) |
System |
2 |
Connections |
CPL_DBLCLK:此訊息表明使用者點擊了控制台應用,可以在此啟動一個進程,進而完成相應的工作。
CPL_STOP、CPL_EXIT:分別是停止和退出訊息。
下面給出一個簡單的執行個體:
首先建立一個Smart Device的Win32 Smart Device Project DLL工程,
加入下面的代碼:
// TestCPL.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include <cpl.h>
#define NUM_APPLETS 1
HINSTANCE g_hInstance = NULL;
typedef struct tagApplets
{
int icon; // icon resource identifier
int namestring; // name-string resource identifier
int descstring; // description-string resource identifier
} APPLETS;
const APPLETS SystemApplets[] =
{
APPLET_ICON, APPLET_NAME, APPLET_DESC
// add more struct members here if supporting more than on applet
};
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
if (DLL_PROCESS_ATTACH == ul_reason_for_call)
g_hInstance = (HINSTANCE)hModule;
return TRUE;
}
BOOL InitApplet(HWND hwndParent)
{
return TRUE;
}
void TermApplet()
{
return;
}
////////////////////////////////////////////////////////
// This is the entry point called by ctlpnl.exe
////////////////////////////////////////////////////////
LONG CALLBACK CPlApplet (HWND hwndCPL, UINT uMsg, LONG lParam1, LONG lParam2)
{
int iApplet;
LPNEWCPLINFO lpNewCPlInfo;
static int iInitCount = 0;
switch (uMsg)
{
// First message sent. It is sent only once to
// allow the dll to initialize it's applet(s)
case CPL_INIT:
if (!iInitCount)
{
if (!InitApplet(hwndCPL))
return FALSE;
}
iInitCount++;
return TRUE;
// Second message sent. Return the count of applets supported
// by this dll
case CPL_GETCOUNT:
return (LONG)NUM_APPLETS;
// Third message sent. Sent once for each applet supported by this dll.
// The lParam1 contains the number that indicates which applet this is
// for, from 0 to 1 less than the count of applets.
// lParam2 is a NEWCPLINFO that should be filled with information about
// this applet before returning
case CPL_NEWINQUIRE:
lpNewCPlInfo = (LPNEWCPLINFO)lParam2;
iApplet = (int)lParam1;
lpNewCPlInfo->dwSize = (DWORD)sizeof(NEWCPLINFO);
lpNewCPlInfo->dwFlags = 0;
lpNewCPlInfo->dwHelpContext = 0;
lpNewCPlInfo->lData = SystemApplets[iApplet].icon;
lpNewCPlInfo->hIcon = LoadIcon(g_hInstance,(LPCTSTR)MAKEINTRESOURCE(SystemApplets[iApplet].icon));
lpNewCPlInfo->szHelpFile[0] = '/0';
LoadString(g_hInstance,SystemApplets[iApplet].namestring,lpNewCPlInfo->szName,32);
LoadString(g_hInstance,SystemApplets[iApplet].descstring,lpNewCPlInfo->szInfo,64);
break;
case CPL_IDNAME:
_tcscpy((LPWSTR)lParam2, _T("Device Information"));
break;
// This is sent whenever the user clicks an icon in Settings for one of
// the applets supported by this dll. lParam1 contains the number indicating
// which applet. Return 0 if applet successfully launched, non-zero otherwise
case CPL_DBLCLK:
iApplet = (UINT)lParam1;
//LoadDialog(g_hInstance, hwndCPL);
CreateProcess(_T("DeviceInfo.exe"),NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
break;
// Sent once per applet, before CPL_EXIT
case CPL_STOP:
break;
// Sent once before the dll is unloaded
case CPL_EXIT:
iInitCount--;
if (!iInitCount)
TermApplet();
break;
default:
break;
}
return 0;
}
//TestCPL.def
LIBRARY "TestCPL"
EXPORTS DllMain
CPlApplet
另外,添加註冊表索引值,如下:
[HKEY_LOCAL_MACHINE/ControlPanel/ Device Information].
"Group" = dword:1
下面是摘自另外一篇文章,作為補充:
預設工程產生的是dll,而希望產生的是Control Panel (.cpl) files,需要做如下配置:
1.
myBackLight (工程名)右鍵—〉
Properties—〉
Configuration Properties—〉
Debugging。
Linker—〉
General—〉
Output File設定為
$(OutDir)/myBacklight.cpl(即把尾碼dll改成cpl)
2.Remote Executable設定為 "/Windows/ctlpnl.exe";Command Arguments設定為 "myBackLight.cpl"。
(可以不設,但通過這個可以加深對cpl程式的理解) 配置完成,新增的AP表徵圖添加到Setting—〉System中,如果需要指定圖片到Personal或Connection中,需要在CPlApplet處理CPL_IDNAME訊息,主要是設定註冊表中的Group值,0 -- Personal, 1 -- System(預設),
2 -- Connection。
上面的程式寫註冊表是在reg檔案(比如platform.reg)中直接改,這裡是寫在程式裡,我只試過前一種,下面這種做參考。 case CPL_IDNAME:
{
UINT uApp = lParam1;
LPWSTR pszName = (LPTSTR)lParam2; TCHAR szKey[100];
_tcscpy(pszName, TEXT("myBacklight"));
swprintf(szKey, L"ControlPanel//%s", pszName);
DWORD dwSize = sizeof(DWORD);
HKEY hKey;
DWORD dwDisp;
DWORD dwGroup; dwGroup = 0;
RegCreateKeyExW(HKEY_LOCAL_MACHINE, szKey, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hKey, &dwDisp);
RegSetValueExW(hKey, L"Group", 0, REG_DWORD, (LPBYTE)&dwGroup, sizeof(DWORD));
return 0;
}
——————————————————————————————————————————————————
補充:
1. windows mobile 6 sdk 裡有個例子叫mybacklight,代碼可以參考那裡面的。
2. 絕大多數代碼是不用改的,只要更改有:
(1)在項目屬性裡將Output Files尾碼dll 改成cpl
(2)CPL_DBLCLK下的
(3)CPL_IDNAME下的
(4)登錄機碼。來確定你的cpl程式出現的位置。
[HKEY_LOCAL_MACHINE/ControlPanel/
Device Information]
.
"Group" = dword:1
3. 剛開始的時候對cpl程式出現的位置怎麼設定理解錯誤,以為只要設定註冊表就可以了。
而實際上,設定註冊表只是其中一步,還有一步便是處理CPL_IDNAME訊息,
該訊息下來設定控制台小程式的名稱,這個名稱可以不跟程式本身的名稱相同,
但必須跟註冊表裡設定的名字相同,這便是程式跟註冊表裡的設定聯絡的紐帶。
4. 範例的cpl.h檔案中沒有定義CPL_IDNAME,在裡面加上如下語句便可以了:
#define CPL_IDNAME 100