CE监控程序

/********************************************************************
 file name :  g:/工具源码/WinCE/OvlManager/OvlManager/Listen.h
 author  : Clark
 created  : 27:1:2011   16:09
 purpose  : 
*********************************************************************/
#pragma once
#include "../../CE600/CE600.h"
#include "../../CE600/COMImage.h"

 


typedef void  RENDER_FUN(void* pThis, HDC hdc);
extern    const bool IS_TEST;


class CListen
{
public:
 CListen(HWND hWnd, RECT rtWindows, HINSTANCEUserEx* hResource);
 ~CListen(void);


 void    SetRenderFun(RENDER_FUN* pRender);
 void    Render(HDC hdc);
 void    Hit(POINT* ptMouse);


private:
 static void RenderUSBCONNECT( LPVOID pThis, HDC hdc);
 static void RenderLOWPOWER( LPVOID pThis, HDC hdc);
 static void RenderDESK( LPVOID pThis, HDC hdc);
 static void RenderDefault( LPVOID pThis, HDC hdc);
 friend static DWORD WINAPI ListenThread(LPVOID i_lparam);


 enum OS_EVENT_ID
 {
  BK_MAX = 3,
  LISTEN_EVENT_MAX = 2,
  RT_DESK_MAX = 4,
 };
 RENDER_FUN*    m_pRenderFun;
 ThreadEx*    m_pListenThread;
 HANDLE     m_hListenEvent[LISTEN_EVENT_MAX];
 RECT     m_rtImg;
 RECT     m_rtDesk[RT_DESK_MAX];
 IImageEx    m_imgBK[BK_MAX];
 HANDLE     m_hMutex;
 HWND     m_hWnd;
 CutTime     m_lTime;
 bool     m_bDesk;
};

 

////////////////////////////////////////////////////////

 

 

 

#include "StdAfx.h"
#include "Listen.h"
#include "Pkfuncs.h"
#include "../../CE600/HDCBuf.h"
#include "../../CE600/SystemAPI.h"

 

#define EVENT_OVL      _T("EVENT_OVL_LEADER")
#define BITMAP_LOWBAT_ID    1   
#define BITMAP_USBDISK_ID    2   
#define BITMPA_SLEEP_ID     4    
#define BITMAP_HIDE_ID      100
#define BITMPA_CLOSE_ID     5   
#define OVL_SLEEP_SHUTDOWN    200

const int CLOSE_TIME_LIMIT = 10;    
const bool IS_TEST = false;
extern RECT  MAX_RECT;


DWORD ListenThread(void* pPar)
{
 CListen* pListen = (CListen*)pPar;
 bool bListen = true;
 while( bListen)
 {
  DWORD dwRet = WaitForMultipleObjects(CListen::LISTEN_EVENT_MAX,pListen->m_hListenEvent,FALSE,500);
  switch( dwRet)
  {
  case WAIT_TIMEOUT:
  case WAIT_FAILED:
   break;
  default:
   {
    DWORD nIndex = dwRet - WAIT_OBJECT_0;
    if( 0 == nIndex)
    {
     bListen = false;
    }
    else
    {
     DWORD dwEvent = GetEventData(pListen->m_hListenEvent[1]);
     if( BITMAP_LOWBAT_ID == dwEvent)
     {
      pListen->SetRenderFun(pListen->RenderLOWPOWER);
      ShowWindow(pListen->m_hWnd, SW_SHOW);
      InvalidateRect(pListen->m_hWnd,&MAX_RECT,TRUE); 
     }
     else if( BITMAP_USBDISK_ID == dwEvent)
     {
      pListen->SetRenderFun(pListen->RenderUSBCONNECT);
      ShowWindow(pListen->m_hWnd, SW_SHOW);
      InvalidateRect(pListen->m_hWnd,&MAX_RECT,TRUE); 
     }
     else if( OVL_SLEEP_SHUTDOWN == dwEvent)
     {
      pListen->m_lTime.init();
      pListen->SetRenderFun(pListen->RenderDESK);
      ShowWindow(pListen->m_hWnd, SW_SHOW);
      pListen->m_bDesk = true;
      while(pListen->m_bDesk)
      {
       Sleep(500);
       InvalidateRect(pListen->m_hWnd,&(pListen->m_rtDesk[0]),TRUE); 
       long lT = pListen->m_lTime.overTime()/1000;
       if( CLOSE_TIME_LIMIT - lT<=0 )
       {
        SetEventData(pListen->m_hListenEvent[1],BITMPA_CLOSE_ID);
        SetEvent(pListen->m_hListenEvent[1]);
        break;
       }
      }
     }
     else if( BITMPA_SLEEP_ID == dwEvent)
     {
      pListen->SetRenderFun(pListen->RenderDefault);
      InvalidateRect(pListen->m_hWnd,&MAX_RECT,TRUE); 
      ShowWindow(pListen->m_hWnd, SW_HIDE);
      Sleep(500);
      //编译时这样写会报错,原因是睡眠让编程器断开了链接从而退出了程序
      g_pSysAPI->EquSleep();
     }
     else if( BITMPA_CLOSE_ID == dwEvent)
     {
      g_pSysAPI->ShutDown();
     }
     else if( BITMAP_HIDE_ID == dwEvent)
     {
      pListen->SetRenderFun(pListen->RenderDefault);
      if( IS_TEST)
       ShowWindow(pListen->m_hWnd, SW_SHOW);
      else
       ShowWindow(pListen->m_hWnd, SW_HIDE);
      InvalidateRect(pListen->m_hWnd,&MAX_RECT,TRUE); 
     }
    }
   }
   break;
  }
 }
 return 0;
}
CListen::CListen(HWND hWnd, RECT rtWindows, HINSTANCEUserEx* phResource)
{
 m_hWnd = hWnd;
 char strID[MAX_PATH];
 sprintf(strID,"MUTEX_FOR_COM_CDC_%d",__LINE__);
 a2w szID(strID);
 m_hMutex = CreateMutex(NULL,FALSE,szID);

 SetRect(&m_rtImg,rtWindows.left+60,rtWindows.top+20,rtWindows.left+356+60,rtWindows.left+188+20);
 SetRect(&m_rtDesk[0],m_rtImg.left+15+104,m_rtImg.top+20,m_rtImg.left+15+208,m_rtImg.top+112-20);
 SetRect(&m_rtDesk[1],m_rtImg.left+15,m_rtImg.top+112,m_rtImg.left+15+104,m_rtImg.top+176);
 SetRect(&m_rtDesk[2],m_rtDesk[1].right+8,m_rtImg.top+112,m_rtDesk[1].right+8+104,m_rtImg.top+176);
 SetRect(&m_rtDesk[3],m_rtDesk[2].right+8,m_rtImg.top+112,m_rtDesk[2].right+8+104,m_rtImg.top+176);

 
 IImageEx::Init();
 const int IDR_IMG_USBCONNECT = 100;
 const int IDR_IMG_LOWPOWER = 101;
 const int IDR_IMG_DESK = 102;
 m_imgBK[0].Load(*phResource,IDR_IMG_USBCONNECT,L"IMG");
 m_imgBK[1].Load(*phResource,IDR_IMG_LOWPOWER,L"IMG");
 m_imgBK[2].Load(*phResource,IDR_IMG_DESK,L"IMG");

 m_bDesk = false;
 SetRenderFun(RenderDefault);
 InvalidateRect(m_hWnd,&MAX_RECT,TRUE); 
 m_hListenEvent[1] = CreateEvent(NULL , FALSE , FALSE ,  EVENT_OVL);
 m_pListenThread = new ThreadEx(m_hListenEvent[0],ListenThread,this);
}
CListen::~CListen(void)
{
 delete m_pListenThread;
 m_pListenThread = NULL;
 IImageEx::UnInit();
 if( WAIT_OBJECT_0 == WaitForSingleObject(m_hMutex,5000))
 {
  ReleaseMutex(m_hMutex);
 }
 CloseHandle(m_hMutex);
 for(int i=0; i<LISTEN_EVENT_MAX; i++)
  CloseHandle(m_hListenEvent[i]); 
}
void CListen::RenderUSBCONNECT( LPVOID pThis,HDC hdc)
{
 CListen* pListen = (CListen*)pThis;
 HDCBuf hdcBuf(&hdc,&MAX_RECT);
 pListen->m_imgBK[0]->Draw(*hdcBuf.getHDC(), &MAX_RECT, NULL);
}
void CListen::RenderLOWPOWER( LPVOID pThis,HDC hdc)
{
 CListen* pListen = (CListen*)pThis;
 HDCBuf hdcBuf(&hdc,&MAX_RECT);
 pListen->m_imgBK[1]->Draw(*hdcBuf.getHDC(), &MAX_RECT, NULL);
}
void CListen::RenderDESK( LPVOID pThis,HDC hdc)
{
 CListen* pListen = (CListen*)pThis;

 char sTime[MAX_PATH];
 char sCancle[MAX_PATH];
 char sSleep[MAX_PATH];
 char sClose[MAX_PATH];
 long lT = pListen->m_lTime.overTime()/1000;

 DWORD dwLangLCID = GetUserDefaultLCID();;
 switch(dwLangLCID)
 {
 case 0x804://中简体
 case 0x404://中繁体
  sprintf(sTime,"%ld",CLOSE_TIME_LIMIT-lT);
  sprintf(sCancle,"取消");
  sprintf(sSleep,"睡眠");
  sprintf(sClose,"关机");
  break;
 case 0x409://默认英语
 default:
  sprintf(sTime,"%ld",CLOSE_TIME_LIMIT-lT);
  sprintf(sCancle,"Cancel");
  sprintf(sSleep,"Sleep");
  sprintf(sClose,"Shutdown");
  break;
 }


 HDCBuf hdcBuf(&hdc,&MAX_RECT);
 pListen->m_imgBK[2]->Draw(*hdcBuf.getHDC(), &(pListen->m_rtImg), NULL);
 
 a2w szTime(sTime);
 //hdcBuf.fillRect(RGB(0,255,0),&(pListen->m_rtDesk[0]));
 hdcBuf.drawText(szTime,pListen->m_rtDesk[0],40,RGB(0,0,0),DT_CENTER|DT_VCENTER);
 a2w szCancle(sCancle);
 //hdcBuf.fillRect(RGB(255,0,0),&(pListen->m_rtDesk[1]));
 hdcBuf.drawText(szCancle,pListen->m_rtDesk[1],20,RGB(0,0,0),DT_CENTER|DT_VCENTER);
 a2w szSleep(sSleep);
 //hdcBuf.fillRect(RGB(0,255,0),&(pListen->m_rtDesk[2]));
 hdcBuf.drawText(szSleep,pListen->m_rtDesk[2],20,RGB(0,0,0),DT_CENTER|DT_VCENTER);
 a2w szClose(sClose);
 //hdcBuf.fillRect(RGB(0,0,255),&(pListen->m_rtDesk[3]));
 hdcBuf.drawText(szClose,pListen->m_rtDesk[3],20,RGB(0,0,0),DT_CENTER|DT_VCENTER);
}
void CListen::RenderDefault( LPVOID pThis,HDC hdc)
{
 HDCBuf hdcBuf(&hdc,&MAX_RECT);
 hdcBuf.fillRect(RGB(255,0,0));
}

void CListen::Hit( POINT* ptMouse )
{
 if( RenderDESK == m_pRenderFun)
 {
  if(PtInRect(&m_rtDesk[1], *ptMouse) )
  {
   m_bDesk = false;
   SetEventData(m_hListenEvent[1],BITMAP_HIDE_ID);
   SetEvent(m_hListenEvent[1]);
  }
  else if(PtInRect(&m_rtDesk[2], *ptMouse))
  {
   m_bDesk = false;
   SetEventData(m_hListenEvent[1],BITMPA_SLEEP_ID);
   SetEvent(m_hListenEvent[1]);
  }
  else if(PtInRect(&m_rtDesk[3], *ptMouse))
  {
   m_bDesk = false;
   SetEventData(m_hListenEvent[1],BITMPA_CLOSE_ID);
   SetEvent(m_hListenEvent[1]);
  }
 }
 else
 {
  if( IS_TEST)
  {
   RECT rtLeft, rtRight;
   SetRect(&rtLeft,0,0,50,50);
   SetRect(&rtRight,400,0,480,50);
   if( PtInRect(&rtLeft, *ptMouse))
   {
    SetEventData(m_hListenEvent[1],BITMAP_LOWBAT_ID);
    SetEvent(m_hListenEvent[1]);
   }
   else if( PtInRect(&rtRight, *ptMouse))
   {
    SetEventData(m_hListenEvent[1],BITMAP_USBDISK_ID);
    SetEvent(m_hListenEvent[1]);
   }
   else
   {
    SetEventData(m_hListenEvent[1],BITMAP_HIDE_ID);
    SetEvent(m_hListenEvent[1]);
   }
  }
 }
}

void CListen::Render(HDC hdc)
{
 if( WAIT_OBJECT_0 == WaitForSingleObject(m_hMutex,INFINITE))
 {
  m_pRenderFun(this, hdc);
  ReleaseMutex(m_hMutex);
 }
}

void CListen::SetRenderFun( RENDER_FUN* pRender )
{
 if( WAIT_OBJECT_0 == WaitForSingleObject(m_hMutex,INFINITE))
 {
  m_pRenderFun = pRender;
  ReleaseMutex(m_hMutex);
 }
}

 

////////////////////////////////////////////////////////////////////////

 

 

 

// OvlManager.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "OvlManager.h"
#include <windows.h>
#include <commctrl.h>
#include "../../CE600/SystemAPI.h"
#include "Listen.h"
#include "Pkfuncs.h"

 

#define MAX_LOADSTRING 100


// Global Variables:
// Forward declarations of functions included in this code module:
ATOM    MyRegisterClass(HINSTANCE, LPTSTR);
BOOL    InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
#define APP_NAME _T("OvlManager")
HINSTANCE   g_hInst;   // current instance
HWND    g_hWnd;
RECT    MAX_RECT;


HINSTANCEUserEx  g_hResource;
CListen*   g_pListen = NULL;


//--------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance,
       HINSTANCE hPrevInstance,
       LPTSTR    lpCmdLine,
                   int       nCmdShow)
{
 //单例进程
 if( AppExist(APP_NAME))
 {
  return TRUE;
 }

 //窗口大小
 SetRect(&MAX_RECT,0,0,480,272);

 //资源文件
 TCHAR szPath[MAX_PATH];
 getCurDirPath(szPath);
 _tcscat(szPath,L"/OMResource.dll");
 if( !g_hResource.Load(szPath))
  return 0;

 //系统接口
 _tcscpy(szPath,L"");
 getCurDirPath(szPath);
 _tcscat(szPath,L"/AppInterface.dll");
 try
 {
  g_pSysAPI = new SystemAPI(szPath);
 }
 catch (std::exception* e)
 {
  g_pSysAPI = NULL;
  MessageBox(NULL,L"Failed to load AppInterface.dll",L"Error",0);
  goto AppExit;
 }
 
 
//-----------------------------------------------------------
 // Perform application initialization:
 g_hWnd = NULL;
 if( IS_TEST)
 {
  if (!InitInstance(hInstance, SW_SHOW))
  {
   goto AppExit;
  }
 }
 else
 {
  if (!InitInstance(hInstance, SW_HIDE))
  {
   goto AppExit;
  }
 }

 //控制模块
 g_pListen = new CListen(g_hWnd, MAX_RECT, &g_hResource);
 
 
 HACCEL hAccelTable;
 hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_OVLMANAGER));

 // Main message loop:
 MSG msg;
 while (GetMessage(&msg, NULL, 0, 0))
 {
  if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }
 }
//-----------------------------------------------------------


AppExit:
 if( NULL != g_pListen)
  delete g_pListen;
 g_pListen = NULL;
 g_hWnd = NULL;
 return (int)msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
 WNDCLASS wc;

 wc.style         = CS_HREDRAW | CS_VREDRAW;
 wc.lpfnWndProc   = WndProc;
 wc.cbClsExtra    = 0;
 wc.cbWndExtra    = 0;
 wc.hInstance     = hInstance;
 wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_OVLMANAGER));
 wc.hCursor       = 0;
 wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
 wc.lpszMenuName  = 0;
 wc.lpszClassName = szWindowClass;

 return RegisterClass(&wc);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    HWND hWnd;
    TCHAR szWindowClass[MAX_LOADSTRING]; // main window class name

    g_hInst = hInstance; // Store instance handle in our global variable
    LoadString(hInstance, IDC_OVLMANAGER, szWindowClass, MAX_LOADSTRING);

 UINT width = GetSystemMetrics(SM_CXSCREEN);
 UINT height = GetSystemMetrics(SM_CYSCREEN);
    if (!MyRegisterClass(hInstance, szWindowClass))
    {
     return FALSE;
    }
 hWnd = CreateWindowEx(WS_EX_TOPMOST,szWindowClass,APP_NAME,WS_POPUP|WS_MAXIMIZEBOX,
  0, 0, width, height,
  NULL,NULL,hInstance,NULL);
 GetWindowRect(hWnd,&MAX_RECT);

    if (!hWnd)
    {
        return FALSE;
    }


    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);
 g_hWnd = hWnd;
    return TRUE;
}


//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND - process the application menu
//  WM_PAINT - Paint the main window
//  WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
 POINT ptMouse;
    switch (message)
    {
        case WM_COMMAND:
            break;
        case WM_CREATE:
            break;
        case WM_PAINT:
            hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code here...
            EndPaint(hWnd, &ps);
            break;
  case WM_ERASEBKGND:
   if( NULL != g_pListen)
   {
    g_pListen->Render((HDC) wParam);
   }
   break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
  case WM_LBUTTONUP:
   ptMouse.x = LOWORD(lParam);
   ptMouse.y = HIWORD(lParam);
   if( NULL != g_pListen)
   {
    g_pListen->Hit(&ptMouse);
   }
   break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

你可能感兴趣的:(CE监控程序)