WinCE 控制面板的创建
//=====================================================================================================
//AUTHOR:
// norains
//DATE:
// Saturday 25-Feb-2006
//=====================================================================================================
控制面板的组件,其实就是一个地地道道的DLL文件,所不同的是其后缀名为.CPL而已.控制面板组件对外必须要实现一个CPlApplet接口,其原型为:LONG CALLBACK CPlApplet(HWND hwndCPL,UINT message, LPARAM lParam1, LPARAM lParam2).而在此函数之中,为了使组件正常运作,我们必须要处理如下消息:CPL_INIT(初始化,可以在这分配内存等等),CPL_GETCOUNT(显示的组件数目),CPL_NEWINQUIRE(获取组件的信息,才能正常显示),CPL_DBLCLK(双击图标时执行).
为方便观看,将此函数体列出:
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// The entry point to the Control Panel application.
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
extern "C" LONG CALLBACK CPlApplet(HWND hwndCPL,UINT message, LPARAM lParam1, LPARAM lParam2)
{
switch (message)
{
case CPL_INIT:
// Perform global initializations, especially memory
// allocations, here.
// Return 1 for success or 0 for failure.
// Control Panel does not load if failure is returned.
return 1;
case CPL_GETCOUNT:
// The number of actions supported by this Control
// Panel application.
return 1;
case CPL_NEWINQUIRE:
{
// This message is sent once for each dialog box, as
// determined by the value returned from CPL_GETCOUNT.
// lParam1 is the 0-based index of the dialog box.
// lParam2 is a pointer to the NEWCPLINFO structure.
return 0; //means CPLApplet succeed
return 1; // Nonzero value means CPlApplet failed.
}
case CPL_DBLCLK:
{
// The user has double-clicked the icon for the
// dialog box in lParam1 (zero-based).
return 0; // CPlApplet succeed.
return 1; // CPlApplet failed.
}
case CPL_STOP:
// Called once for each dialog box. Used for cleanup.
case CPL_EXIT:
// Called only once for the application. Used for cleanup.
default:
return 0;
}
return 1; // CPlApplet failed.
} // CPlApplet
组件要进行相关操作,一般在.cpl文件内部进行操作;但我们完全可以把.CPL文件当成一个外壳,其作用只是在"控制面板"中显示一个图标,真正的处理是调用另外一个EXE文件.这样的好处是,此.CPL代码通用性强,如果想再添加别的组件,只要更改.CPL少量代码;并且,功能模块的分离,使得我们如果要更改相关功能,也只要修改相应的EXE文件,调试也更加方便.
现在就让我们看一下如何在控制面板中调用EXE文件.
我们需要的是,在控制面板中双击我们显示的图标就调用我们相应的EXE程序;很显然,我们只需要在CPL_DBLCLK消息中添加调用代码即可.
extern "C" LONG CALLBACK CPlApplet(HWND hwndCPL,UINT message, LPARAM lParam1, LPARAM lParam2)
{
switch (message)
....
case CPL_DBLCLK:
{
//-----------------------------------------------------------------
//关于FindWindow函数的说明:
//原型:HWND FindWindow(LPCTSTR lpClassName,LPCTSTR lpWindowName);
//lpClassName:要赋的值是类的字符串名称,在ce下可以运行Remote Spy++进行查看;在本例子中采用此工具看到的是"Dialog".
//此参数也可以为NULL,前提是所有在运行的窗口标题不同
//lpWindowName:窗口的标题字符串名称,也即是我们可以看到的窗口标题
//------------------------------------------------------------------
//由于我们的这个exe文件只有一个窗口,下面这个函数也可以这样写FindWindow (NULL,TEXT("背光调节"))
HWND hWnd = FindWindow (L"Dialog",TEXT("背光调节"));
if (hWnd)
{
//如果已经运行过一个实例,则把它提到窗口前
SetForegroundWindow (hWnd);
CloseHandle(hWnd);
return 0;
}
else
{
//调用exe文件.
if (CreateProcess(_T("//Windows//Backlight.exe"), NULL, NULL,NULL, FALSE, 0, NULL, NULL, NULL, &pi))
{
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
return 0;
}
}
return 1; // CPlApplet failed.
}
....
}