在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,需要做如下配置:
2. Remote Executable设置为 "/Windows/ctlpnl.exe"; Command Arguments设置为 "myBackLight.cpl"。
(可以不设,但通过这个可以加深对cpl程序的理解)
上面的程序写注册表是在reg文件(比如platform.reg)中直接改,这里是写在程序里,我只试过前一种,下面这种做参考。
{
UINT uApp = lParam1;
LPWSTR pszName = (LPTSTR)lParam2;
_tcscpy(pszName, TEXT("myBacklight"));
swprintf(szKey, L"ControlPanel//%s", pszName);
DWORD dwSize = sizeof(DWORD);
HKEY hKey;
DWORD dwDisp;
DWORD dwGroup;
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程序出现的位置。
Device Information
[HKEY_LOCAL_MACHINE/ControlPanel/]
.
"Group" = dword:1
3. 刚开始的时候对cpl程序出现的位置怎么设置理解错误,以为只要设置注册表就可以了。
而实际上,设置注册表只是其中一步,还有一步便是处理CPL_IDNAME消息,
该消息下来设置控制面板小程序的名称,这个名称可以不跟程序本身的名称相同,
但必须跟注册表里设置的名字相同,这便是程序跟注册表里的设置联系的纽带。
4. 样例的cpl.h文件中没有定义CPL_IDNAME,在里面加上如下语句便可以了:
#define CPL_IDNAME 100