一个简单的带消息循环的控制面板选项例程

//========================================================================
//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);
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值