一个简单的带消息循环的控制面板选项例程
2007-05-17 23:13
288 查看
//========================================================================
//TITLE:
// 一个简单的带消息循环的控制面板选项例程
//AUTHOR:
// norains
//DATE:
// Thursday 16-May-2007
//Environment:
// EVC4.0 + Standard SDK 4.2
// EVC4.0 + Standard SDK 5.0
//========================================================================
普通的应用程序可以在WinMain()中进行消息循环,当然CPL也可以,不过不是在DllMain(),而是在CPlApplet接收CPL_DBLCLK时.本文给出了一个很简单的在控制面板创建对话框的例子.
关于控制面板的功能和架构在我的其它文章里已经有介绍,这里就不再陈诉,而直接罗列代码.对本章所列代码有所不明白的朋友可以参考以下这几篇文章:
WinCE 控制面板的创建:http://blog.csdn.net/norains/archive/2006/02/25/609216.aspx
WinCE 控制面板和驱动通信:http://blog.csdn.net/norains/archive/2006/02/25/609217.aspx
详解WINCE的控制面板:http://blog.csdn.net/norains/archive/2006/09/07/1189143.aspx
/**/////////////////////////////////////////////////////////////////////////
// DisplayOutput.cpp : Defines the entry point for the DLL application.
//
/**////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "D:/ProgramFiles/WINCE500/PUBLIC/COMMON/OAK/INC/Cpl.h"
#include "resource.h"
#include "Maindlg.h"
//---------------------------------------------------------------------
//Macro define
#define CPL_TITLE TEXT("简单例程")
#define CPL_INFO TEXT("这是一个很简单例程的信息")
// Returns the number of characters in an expression.
#define LENGTHOF(exp) ((sizeof((exp)))/sizeof((*(exp))))
//--------------------------------------------------------------------------------
//Global data
HMODULE g_hModule = NULL; // Handle to the DLL.
//--------------------------------------------------------------------------------
//Description:
// Main entry point for the Control Panel DLL.
//--------------------------------------------------------------------------------------------
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
...{
switch(ul_reason_for_call)
...{
case DLL_PROCESS_ATTACH:
...{
g_hModule = (HMODULE) hModule;
break;
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
//--------------------------------------------------------------------------------------------
//Description:
// 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.
...{
ASSERT(0 == lParam1);
ASSERT(lParam2);
NEWCPLINFO* lpNewCplInfo = (NEWCPLINFO *) lParam2;
if (lpNewCplInfo)
...{
lpNewCplInfo->dwSize = sizeof(NEWCPLINFO);
lpNewCplInfo->dwFlags = 0; //Ignored
lpNewCplInfo->dwHelpContext = 0; //Ignored
lpNewCplInfo->lData = IDI_ICON;
// The large icon for this application. Do not free this
// HICON; it is freed by the Control Panel infrastructure.
lpNewCplInfo->hIcon = LoadIcon(g_hModule,MAKEINTRESOURCE(IDI_ICON));
if(_tcslen(CPL_TITLE) < LENGTHOF(lpNewCplInfo->szName))
...{
_tcscpy(lpNewCplInfo->szName,CPL_TITLE);
}
if(_tcslen(CPL_INFO) < LENGTHOF(lpNewCplInfo->szInfo))
...{
_tcscpy(lpNewCplInfo->szInfo,CPL_INFO);
}
_tcscpy(lpNewCplInfo->szHelpFile, _T(""));
return 0;
}
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).
HWND hWnd = FindWindow(NULL,WND_TITLE);
if(hWnd != NULL)
...{
SetForegroundWindow(hWnd);
return 1;
}
CMainDlg *pWnd = CMainDlg::GetInstance();
if(pWnd != NULL)
...{
pWnd->ShowWindow(g_hModule,TRUE);
}
MSG msg;
while(GetMessage(&msg,NULL,0,0))
...{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
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
/**///////////////////////////////////////////////////////////////////////
// MainDlg.h: interface for the CMainDlg class.
//
/**///////////////////////////////////////////////////////////////////////
#ifndef MAINDLG_H
#define MAINDLG_H
//---------------------------------------------------------------------
//Macro define
#define WND_TITLE TEXT("Hello World")
//---------------------------------------------------------------------
class CMainDlg
...{
public:
static CMainDlg * GetInstance();
BOOL ShowWindow(HINSTANCE hInst,BOOL bShow);
virtual ~CMainDlg();
protected:
void OnDestroy(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam);
static BOOL WinProc(HWND hWnd, UINT wMsg, WPARAM wParam,LPARAM lParam);
static CMainDlg *m_pInstance;
HWND m_hWnd;
HINSTANCE m_hInst;
private:
CMainDlg();
};
#endif // #ifndef MAINDLG_H
/**////////////////////////////////////////////////////////////////////////
// MainDlg.cpp: implementation of the CMainDlg class.
//
/**///////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MainDlg.h"
#include "resource.h"
//---------------------------------------------------------------------
//Initialize the data
CMainDlg *CMainDlg::m_pInstance = NULL;
//-----------------------------------------------------------------------
/**///////////////////////////////////////////////////////////////////////
// Construction/Destruction
/**///////////////////////////////////////////////////////////////////////
CMainDlg::CMainDlg()
...{
m_hWnd = NULL;
m_hInst = NULL;
}
CMainDlg::~CMainDlg()
...{
if(m_pInstance != NULL)
...{
delete m_pInstance;
m_pInstance = NULL;
}
}
//---------------------------------------------------------------------
//Description:
// Show the dialog
//---------------------------------------------------------------------
BOOL CMainDlg::ShowWindow(HINSTANCE hInst, BOOL bShow)
...{
m_hInst = hInst;
m_hWnd = CreateDialog(hInst,MAKEINTRESOURCE(IDD_MAIN),NULL,WinProc);
if(m_hWnd == NULL)
...{
return FALSE;
}
if(bShow == TRUE)
...{
::ShowWindow(m_hWnd,SW_SHOW);
}
else
...{
::ShowWindow(m_hWnd,SW_HIDE);
}
SetForegroundWindow(m_hWnd);
return TRUE;
}
//---------------------------------------------------------------------
//Description:
// The window process
//---------------------------------------------------------------------
BOOL CMainDlg::WinProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
...{
switch(wMsg)
...{
case WM_COMMAND:
...{
switch(LOWORD(wParam))
...{
case IDCANCEL:
DestroyWindow(hWnd);
return TRUE;
}
}
case WM_DESTROY:
m_pInstance->OnDestroy(hWnd,wMsg,wParam,lParam);
return TRUE;
}
return FALSE;
}
//---------------------------------------------------------------------
//Description:
// Get the instance
//---------------------------------------------------------------------
CMainDlg * CMainDlg::GetInstance()
...{
if(m_pInstance == NULL)
...{
m_pInstance = new CMainDlg();
}
return m_pInstance;
}
//---------------------------------------------------------------------
//Description:
// On the message WM_DESTROY
//---------------------------------------------------------------------
void CMainDlg::OnDestroy(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
...{
PostQuitMessage(0x0);
}
//TITLE:
// 一个简单的带消息循环的控制面板选项例程
//AUTHOR:
// norains
//DATE:
// Thursday 16-May-2007
//Environment:
// EVC4.0 + Standard SDK 4.2
// EVC4.0 + Standard SDK 5.0
//========================================================================
普通的应用程序可以在WinMain()中进行消息循环,当然CPL也可以,不过不是在DllMain(),而是在CPlApplet接收CPL_DBLCLK时.本文给出了一个很简单的在控制面板创建对话框的例子.
关于控制面板的功能和架构在我的其它文章里已经有介绍,这里就不再陈诉,而直接罗列代码.对本章所列代码有所不明白的朋友可以参考以下这几篇文章:
WinCE 控制面板的创建:http://blog.csdn.net/norains/archive/2006/02/25/609216.aspx
WinCE 控制面板和驱动通信:http://blog.csdn.net/norains/archive/2006/02/25/609217.aspx
详解WINCE的控制面板:http://blog.csdn.net/norains/archive/2006/09/07/1189143.aspx
/**/////////////////////////////////////////////////////////////////////////
// DisplayOutput.cpp : Defines the entry point for the DLL application.
//
/**////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "D:/ProgramFiles/WINCE500/PUBLIC/COMMON/OAK/INC/Cpl.h"
#include "resource.h"
#include "Maindlg.h"
//---------------------------------------------------------------------
//Macro define
#define CPL_TITLE TEXT("简单例程")
#define CPL_INFO TEXT("这是一个很简单例程的信息")
// Returns the number of characters in an expression.
#define LENGTHOF(exp) ((sizeof((exp)))/sizeof((*(exp))))
//--------------------------------------------------------------------------------
//Global data
HMODULE g_hModule = NULL; // Handle to the DLL.
//--------------------------------------------------------------------------------
//Description:
// Main entry point for the Control Panel DLL.
//--------------------------------------------------------------------------------------------
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
...{
switch(ul_reason_for_call)
...{
case DLL_PROCESS_ATTACH:
...{
g_hModule = (HMODULE) hModule;
break;
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
//--------------------------------------------------------------------------------------------
//Description:
// 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.
...{
ASSERT(0 == lParam1);
ASSERT(lParam2);
NEWCPLINFO* lpNewCplInfo = (NEWCPLINFO *) lParam2;
if (lpNewCplInfo)
...{
lpNewCplInfo->dwSize = sizeof(NEWCPLINFO);
lpNewCplInfo->dwFlags = 0; //Ignored
lpNewCplInfo->dwHelpContext = 0; //Ignored
lpNewCplInfo->lData = IDI_ICON;
// The large icon for this application. Do not free this
// HICON; it is freed by the Control Panel infrastructure.
lpNewCplInfo->hIcon = LoadIcon(g_hModule,MAKEINTRESOURCE(IDI_ICON));
if(_tcslen(CPL_TITLE) < LENGTHOF(lpNewCplInfo->szName))
...{
_tcscpy(lpNewCplInfo->szName,CPL_TITLE);
}
if(_tcslen(CPL_INFO) < LENGTHOF(lpNewCplInfo->szInfo))
...{
_tcscpy(lpNewCplInfo->szInfo,CPL_INFO);
}
_tcscpy(lpNewCplInfo->szHelpFile, _T(""));
return 0;
}
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).
HWND hWnd = FindWindow(NULL,WND_TITLE);
if(hWnd != NULL)
...{
SetForegroundWindow(hWnd);
return 1;
}
CMainDlg *pWnd = CMainDlg::GetInstance();
if(pWnd != NULL)
...{
pWnd->ShowWindow(g_hModule,TRUE);
}
MSG msg;
while(GetMessage(&msg,NULL,0,0))
...{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
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
/**///////////////////////////////////////////////////////////////////////
// MainDlg.h: interface for the CMainDlg class.
//
/**///////////////////////////////////////////////////////////////////////
#ifndef MAINDLG_H
#define MAINDLG_H
//---------------------------------------------------------------------
//Macro define
#define WND_TITLE TEXT("Hello World")
//---------------------------------------------------------------------
class CMainDlg
...{
public:
static CMainDlg * GetInstance();
BOOL ShowWindow(HINSTANCE hInst,BOOL bShow);
virtual ~CMainDlg();
protected:
void OnDestroy(HWND hWnd,UINT wMsg,WPARAM wParam,LPARAM lParam);
static BOOL WinProc(HWND hWnd, UINT wMsg, WPARAM wParam,LPARAM lParam);
static CMainDlg *m_pInstance;
HWND m_hWnd;
HINSTANCE m_hInst;
private:
CMainDlg();
};
#endif // #ifndef MAINDLG_H
/**////////////////////////////////////////////////////////////////////////
// MainDlg.cpp: implementation of the CMainDlg class.
//
/**///////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MainDlg.h"
#include "resource.h"
//---------------------------------------------------------------------
//Initialize the data
CMainDlg *CMainDlg::m_pInstance = NULL;
//-----------------------------------------------------------------------
/**///////////////////////////////////////////////////////////////////////
// Construction/Destruction
/**///////////////////////////////////////////////////////////////////////
CMainDlg::CMainDlg()
...{
m_hWnd = NULL;
m_hInst = NULL;
}
CMainDlg::~CMainDlg()
...{
if(m_pInstance != NULL)
...{
delete m_pInstance;
m_pInstance = NULL;
}
}
//---------------------------------------------------------------------
//Description:
// Show the dialog
//---------------------------------------------------------------------
BOOL CMainDlg::ShowWindow(HINSTANCE hInst, BOOL bShow)
...{
m_hInst = hInst;
m_hWnd = CreateDialog(hInst,MAKEINTRESOURCE(IDD_MAIN),NULL,WinProc);
if(m_hWnd == NULL)
...{
return FALSE;
}
if(bShow == TRUE)
...{
::ShowWindow(m_hWnd,SW_SHOW);
}
else
...{
::ShowWindow(m_hWnd,SW_HIDE);
}
SetForegroundWindow(m_hWnd);
return TRUE;
}
//---------------------------------------------------------------------
//Description:
// The window process
//---------------------------------------------------------------------
BOOL CMainDlg::WinProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
...{
switch(wMsg)
...{
case WM_COMMAND:
...{
switch(LOWORD(wParam))
...{
case IDCANCEL:
DestroyWindow(hWnd);
return TRUE;
}
}
case WM_DESTROY:
m_pInstance->OnDestroy(hWnd,wMsg,wParam,lParam);
return TRUE;
}
return FALSE;
}
//---------------------------------------------------------------------
//Description:
// Get the instance
//---------------------------------------------------------------------
CMainDlg * CMainDlg::GetInstance()
...{
if(m_pInstance == NULL)
...{
m_pInstance = new CMainDlg();
}
return m_pInstance;
}
//---------------------------------------------------------------------
//Description:
// On the message WM_DESTROY
//---------------------------------------------------------------------
void CMainDlg::OnDestroy(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
...{
PostQuitMessage(0x0);
}
相关文章推荐
- 一个简单的带消息循环的控制面板选项例程
- 一个简单的Windows程序及消息循环机制
- C/C++做的一个简单界面程序扩充,向指定窗口发送消息
- 一个简单的PopupWindow做的选项菜单点击按钮会在按钮下弹出几个下拉控件
- 一个简单实用的模拟选项夹的代码
- QObject::deleteLater()并没有将对象立即销毁,而是向主消息循环发送了一个event,下一次主消息循环收到这个event之后才会销毁对象 good
- 一个简单的Windows驱动例程
- mysql 存储过程一个简单的循环遍历操作数据
- 适合C# Actor的消息执行方式(5):一个简单的网络爬虫
- 一个最简单的sdl显示图片例子sdl example sdl 例子 例程SDL贴图bmp
- 【cocos2d-x 3.x】02-简单看一下cocos的消息循环的步骤
- mDNS原理的简单理解——每个进入局域网的主机,如果开启了mDNS服务的话,都会向局域网内的所有主机组播一个消息,我是谁,和我的IP地址是多少。然后其他也有该服务的主机就会响应,也会告诉你,它是谁,它的IP地址是多少
- C#TCPClient应用-一个简单的消息发送和接收
- 在执行一个 CLR 例程或 SQL Server 2005 中使用程序集时的错误消息:"在主机存储区中的组件在 GAC 中有一个不同的签名比程序集。
- C#TCPClient应用-一个简单的消息发送和接收
- 客户关系管理的迭代特质:一个简单的CRM循环
- MFC 一个简单的消息示例
- js,jquery的一个简单按时间循环调用方法
- 一个简单的顶部菜单消息提醒功能
- C# 编写一个简单的快速排序例程