在WinCE下绘制半透的效果要注意的问题+WINCE 下创建动态显示图片窗口

  

http://blog.csdn.net/lanyzh0909/archive/2010/08/20/5827354.aspx

    在Windows XP 中实现半透明效果并不是件难事,仅仅只需要调SetLayeredWindowAttributes函数即可。如果同样的效果也想在Windows CE 中重现呢?因为Windows CE没有SetLayeredWindowAttributes 函数,不过在Windows CE 5.0开始支持AlphaBlend,该函数的作用是将两个HDC根据一定的比例混合,即有了类似半透明的效果。

知道了有这个函数,我们现在就应该来看看这个函数怎么用。查阅MSDN,该函数定义为:

BOOL AlphaBlend(

  HDC           hdcDest,// 指向目标设备环境的句柄

  int           nXOriginDest,// 指定目目标矩形区域左上角的X轴坐标

  int           nYOriginDest,// 指定目标矩形区域左上角的Y轴坐标

  int           nWidthDest,// 指定目标矩形区域的宽度

  int           nHeightDest,// 指向目标矩形区域高度的句柄

  HDC           hdcSrc, //指向源设备环境的句柄

  int           nXOriginSrc,// 指定源矩形区域左上角的X轴坐标

  int           nYOriginSrc,// 指定源矩形区域左上角的Y轴坐标

  int           nWidthSrc,// 指定源矩形区域的宽度

  int           nHeightSrc,// 指定源矩形区域的高度

  BLENDFUNCTION blendFunction //指定用于源位图和目标位图使用的alpha混合功能

);

要能想正常的使用这个函数还必须做一件事件将Windows.hCoredll.lib加载到你的工程里去。

 

    做好了这些事情钱准备了,可以编译你工程的了。幸运的话能够顺利通过编写,不幸的话将出现下列提示错误:

    error C3861: 'AlphaBlend': identifier not found”函数没有定义,是不是很奇怪我们明明把头文件和Library都包含到我们的工程里怎么会没有定义呢?是不是很纳闷?

    不要郁闷,那不是你的工程的问题而是系统的问题!在定制WINCE系统DE 时候有一个选项需要注意:如下图:

如果你想AlphaBlend来绘制透明效果就必须将这一项选上

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

WINCE 下创建动态显示图片窗口

WINCE下创建一个动态显示图片的窗口并非难事,大概思路为:

1、  创建一个窗口。

2、  创一个内存DC,用存放要显示的图片。

3、  创建一个线程相隔一段时间将图片将绘制到屏幕DC上。

  但是要将其封装在一个类中却并非一件容易的事情,为了以后方便使用我在这里封装一下。

介绍一下这个类的使用方法:

//定义一个窗口属性:

namespace AnimateWnd

{

struct Property

  {

       TSTRING strBkImage;    //要显示图片的路径

       RECT rcImagePosition;   //要显示图片中区域

       RECT rcDrawPosition;    //窗口的位置

       DWORD    dwCount;      //显示的帧数

       DWORD    dwTimeOut;    //每帧的间隔时间

       BOOL    bVertical;     // 图片是否为垂直的切割

  };

}

//定义一个窗口对象

CAnimateWnd g_AnimateWnd;

 

//初始化窗口属性

AnimateWnd::Property pro;

RECT Drawrect = {80,27,401,200};

RECT Imagerect={0,0,1920,177};

pro.dwCount = 6;

pro.dwTimeOut = 300;

pro.rcDrawPosition = Drawrect;

pro.rcImagePosition = Imagerect;

pro.bVertical = FALSE;

pro.strBkImage = TEXT("//NAND//WinCeTemplate//1.bmp");

 

// 调用成员函数设置窗口属性

g_AnimateWnd.SetProperty(pro);

 

//调用成员函数创建窗口.其中hWnd是父窗口的句柄,如果没有父窗口的话直接设置为NULL

//TEXT("ANIMATE_CLS")为窗口类名,TEXT("ANIMATE_NAME")为窗口名.

g_AnimateWnd.Create(hWnd,TEXT("ANIMATE_CLS"),TEXT("ANIMATE_NAME"));

 

//调用成员函数显示窗口

g_AnimateWnd.ShowWindow(TRUE);

 

//调用成员函数动态显示图片

g_AnimateWnd.Start();

 

经过以上几步工作,我们的动态窗口显示的工作就完成了!

 

//Header.h

  1. #include<windows.h>   
  2. #include<string>   
  3. #ifdef UNICODE   
  4.     #ifndef TSTRING   
  5.         #define TSTRING std::wstring   
  6.     #endif   
  7.   
  8.     #ifndef TSTRINGSTREAM   
  9.         #define TSTRINGSTREAM std::wstringstream   
  10.     #endif   
  11. #else   
  12.     #ifndef TSTRING   
  13.         #define TSTRING std::string   
  14.     #endif   
  15.   
  16.     #ifndef TSTRINGSTREAM   
  17.         #define TSTRINGSTREAM std::stringstream   
  18.     #endif   
  19. #endif //#ifdef UNICODE  

 

//WndBase.h

  1. // WndBase.h: interface for the CWndBase class.   
  2. //   
  3. //   
  4. #pragma once   
  5. #include "Header.h"   
  6. class CWndBase   
  7. {   
  8. public:   
  9.     //----------------------------------------------------------------------   
  10.     //Description:   
  11.     //    Show the window   
  12.     //----------------------------------------------------------------------   
  13.     virtual BOOL ShowWindow(BOOL bShow);   
  14.   
  15.     //----------------------------------------------------------------------   
  16.     //Description:   
  17.     //  Create the window   
  18.     //   
  19.     //Parameters:   
  20.     //  hWndParent : [in] The parent window   
  21.     //  strWndClass : [in] The class of the window   
  22.     //  strWndName : [in] The name of the windows   
  23.     //  bMsgThrdInside : [in] The message thread process is inside or not   
  24.     //----------------------------------------------------------------------   
  25.     virtual BOOL Create(HWND hWndParent,const TSTRING &strWndClass,const TSTRING &strWndName,BOOL bMsgThrdInside = FALSE);     
  26.   
  27.     //----------------------------------------------------------------------   
  28.     //Description:   
  29.     //  Create the window   
  30.     //   
  31.     //Parameters:   
  32.     //  hWndParent : [in] The parent window   
  33.     //  strWndClass : [in] The class of the window   
  34.     //  strWndName : [in] The name of the windows   
  35.     //  dwStyle : [in] The window type   
  36.     //  bMsgThrdInside : [in] The message thread process is inside or not   
  37.     //----------------------------------------------------------------------   
  38.     virtual BOOL CreateEx(HWND hWndParent,const TSTRING &strWndClass,const TSTRING &strWndName,DWORD dwStyle,BOOL bMsgThrdInside = FALSE);   
  39.   
  40.     //----------------------------------------------------------------------   
  41.     //Description:   
  42.     //    Set the parent window   
  43.     //----------------------------------------------------------------------   
  44.     BOOL SetParentWindow(HWND hWndParent);     
  45.   
  46.   
  47.     //----------------------------------------------------------------------   
  48.     //Description:   
  49.     //    Get the window handle   
  50.     //----------------------------------------------------------------------   
  51.     HWND GetWindow(voidconst;   
  52.   
  53.     //----------------------------------------------------------------------   
  54.     //Description:   
  55.     //    Get the window parent   
  56.     //----------------------------------------------------------------------   
  57.     HWND GetParentWindow(voidconst;   
  58.   
  59. public:   
  60.     CWndBase();   
  61.     virtual ~CWndBase();   
  62.   
  63. protected:   
  64.     //----------------------------------------------------------------------   
  65.     //Description:   
  66.     //    Actual WndProc   
  67.     //----------------------------------------------------------------------   
  68.     virtual LRESULT WndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam);       
  69.   
  70.     //----------------------------------------------------------------------   
  71.     //Description:   
  72.     //  Get the registry class data   
  73.     //Parameters:   
  74.     //  wc : [out] The buffer for storing the data.   
  75.     //----------------------------------------------------------------------   
  76.     virtual void GetDataForRegistryClass(WNDCLASS &wc);   
  77.   
  78. private:   
  79.     //----------------------------------------------------------------------   
  80.     //Description:   
  81.     //    Register window   
  82.     //----------------------------------------------------------------------   
  83.     BOOL RegisterClass();   
  84.   
  85.     //----------------------------------------------------------------------   
  86.     //Description:   
  87.     //    The process thread is for creating the window   
  88.     //----------------------------------------------------------------------   
  89.     static DWORD WINAPI CreateProc(PVOID pArg);   
  90.        
  91.   
  92.     //----------------------------------------------------------------------   
  93.     //Description:   
  94.     //    Create the window   
  95.     //----------------------------------------------------------------------   
  96.     BOOL CreateWnd(DWORD dwStyle,DWORD dwExStyle);   
  97.   
  98.     //----------------------------------------------------------------------   
  99.     //Description:   
  100.     //    On Message WM_DESTROY   
  101.     //----------------------------------------------------------------------   
  102.     void OnDestroy(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam);   
  103.   
  104.     //----------------------------------------------------------------------   
  105.     //Description:   
  106.     //    Static WndProc wrapper and actual WndProc   
  107.     //----------------------------------------------------------------------   
  108.     static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam);   
  109.        
  110. private:   
  111.     BOOL m_bCreated;   
  112.     BOOL m_bMsgThrdInside;   
  113.     HANDLE m_hEventCreated;   
  114.   
  115.     //The variable is for the common window use.       
  116.   
  117.     HWND m_hWnd;   
  118.     mutable HWND m_hWndParent;   
  119.     TSTRING m_strWndClass;   
  120.     TSTRING m_strWndName;   
  121.     DWORD m_dwStyle;   
  122.        
  123.        
  124.        
  125. };  

 

//WndBase.cpp

  1. #include "WndBase.h"   
  2.   
  3. //   
  4. // Construction/Destruction   
  5. //   
  6.   
  7. CWndBase::CWndBase():   
  8. m_hWnd(NULL),   
  9. m_hWndParent(NULL),   
  10. m_bCreated(FALSE),   
  11. m_hEventCreated(NULL),   
  12. m_bMsgThrdInside(FALSE),   
  13. m_dwStyle(WS_TABSTOP)   
  14. {   
  15.   
  16. }   
  17.   
  18. CWndBase::~CWndBase()   
  19. {      
  20. }   
  21.   
  22.   
  23. BOOL CWndBase::Create(HWND hWndParent,const TSTRING &strWndClass,const TSTRING &strWndName,BOOL bMsgThrdInside)   
  24. {   
  25.     return CreateEx(hWndParent,strWndClass,strWndName,WS_TABSTOP ,bMsgThrdInside);   
  26. }   
  27.   
  28.   
  29. BOOL CWndBase::RegisterClass()   
  30. {      
  31.     WNDCLASS wc;   
  32.     GetDataForRegistryClass(wc);   
  33.   
  34.     return ::RegisterClass(&wc);   
  35.   
  36.        
  37. }   
  38.   
  39.   
  40. LRESULT CALLBACK CWndBase::StaticWndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)   
  41. {   
  42.    CWndBase *pObject = reinterpret_cast<CWndBase*>(GetWindowLong(hWnd, GWL_USERDATA));   
  43.   
  44.     if(pObject)   
  45.     {   
  46.         return pObject->WndProc(hWnd,wMsg,wParam,lParam);   
  47.     }   
  48.     else  
  49.     {   
  50.         return DefWindowProc(hWnd,wMsg,wParam,lParam);   
  51.     }   
  52. }   
  53.   
  54.   
  55. LRESULT CWndBase::WndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)   
  56. {   
  57.     switch(wMsg)   
  58.     {   
  59.         case WM_DESTROY:   
  60.             OnDestroy(hWnd,wMsg,wParam,lParam);   
  61.             break;   
  62.     }   
  63.     return DefWindowProc(hWnd,wMsg,wParam,lParam);   
  64. }   
  65.   
  66.   
  67.   
  68. BOOL CWndBase::ShowWindow(BOOL bShow)   
  69. {   
  70.     if(m_hWnd == NULL)   
  71.     {   
  72.         return FALSE;   
  73.     }   
  74.   
  75.     if(bShow == TRUE)   
  76.     {   
  77.         SetForegroundWindow(m_hWnd);   
  78.         SetWindowPos(m_hWnd,HWND_TOP,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);            
  79.     }   
  80.     else  
  81.     {   
  82.         ::ShowWindow(m_hWnd,SW_HIDE);   
  83.     }     
  84.   
  85.   
  86.     return TRUE;   
  87. }   
  88.   
  89.   
  90.   
  91. DWORD WINAPI CWndBase::CreateProc(PVOID pArg)   
  92. {   
  93.     //Get the object instance   
  94.     CWndBase *pObject = reinterpret_cast<CWndBase *>(pArg);      
  95.   
  96.     #ifdef _WIN32_WCE   
  97.         pObject->m_bCreated = pObject->CreateWnd(pObject->m_dwStyle,0);   
  98.     #else   
  99.         //Create the window.You should set the WS_EX_NOPARENTNOTIFY here, or maybe the CreateWindowEx   
  100.         //never return because it is waiting for the parent window responding when the main message loop doesn't begin working.   
  101.         pObject->m_bCreated = pObject->CreateWnd(pObject->m_dwStyle,WS_EX_NOPARENTNOTIFY);   
  102.     #endif //#ifdef _WIN32_WCE   
  103.   
  104.     //Set the event   
  105.     if(pObject->m_hEventCreated != NULL)   
  106.     {   
  107.         SetEvent(pObject->m_hEventCreated);   
  108.     }   
  109.   
  110.     if(pObject->m_bCreated == FALSE)   
  111.     {   
  112.         //Failed in creating the window, so return and needn't the message loop   
  113.         return 0x01;   
  114.     }   
  115.   
  116.   
  117.     //The message loop     
  118.     MSG msg;   
  119.     while(GetMessage(&msg,NULL,0,0))   
  120.     {   
  121.         TranslateMessage(&msg);   
  122.         DispatchMessage(&msg);   
  123.     }   
  124.   
  125.     return 0;   
  126.   
  127. }   
  128.   
  129.   
  130. BOOL CWndBase::CreateWnd(DWORD dwStyle,DWORD dwExStyle)   
  131. {      
  132.     if(RegisterClass() == FALSE)   
  133.     {   
  134.         return FALSE;   
  135.     }   
  136.   
  137.     RECT rcArea = {0};   
  138.     SystemParametersInfo(SPI_GETWORKAREA, 0, &rcArea, 0);   
  139.   
  140.     m_hWnd = CreateWindowEx(dwExStyle,   
  141.                 m_strWndClass.c_str(),   
  142.                 m_strWndName.c_str(),   
  143.                 dwStyle,   
  144.                 rcArea.left,   
  145.                 rcArea.top,   
  146.                 rcArea.right - rcArea.left,   
  147.                 rcArea.bottom - rcArea.top,   
  148.                 m_hWndParent,    
  149.                 NULL,    
  150.                 GetModuleHandle(NULL),    
  151.                 0);   
  152.            
  153.     ASSERT(m_hWnd != FALSE);   
  154.   
  155.     if (IsWindow(m_hWnd) == FALSE)   
  156.     {   
  157.         return FALSE;   
  158.     }   
  159.   
  160.     // If the window is created successfully, store this object so the    
  161.     //static wrapper can pass calls to the real WndProc.   
  162.     SetWindowLong(m_hWnd, GWL_USERDATA, reinterpret_cast<DWORD>(this));   
  163.   
  164.     return TRUE;   
  165. }   
  166.   
  167.   
  168. void CWndBase::OnDestroy(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)   
  169. {   
  170.   
  171.     if(m_bMsgThrdInside == TRUE)   
  172.     {   
  173.         //Exit the inside thread   
  174.         PostQuitMessage(0x00);   
  175.     }   
  176.   
  177.   
  178. }   
  179.   
  180.   
  181.   
  182.   
  183. HWND CWndBase::GetWindow(voidconst  
  184. {   
  185.     return m_hWnd;   
  186. }   
  187.   
  188.   
  189. HWND CWndBase::GetParentWindow(voidconst  
  190. {   
  191.     if(m_hWnd != NULL)   
  192.     {   
  193.         m_hWndParent = ::GetParent(m_hWnd);   
  194.     }   
  195.   
  196.     return m_hWndParent;   
  197. }   
  198.   
  199.   
  200.   
  201. BOOL CWndBase::SetParentWindow(HWND hWndParent)   
  202. {   
  203.     if(m_hWnd == NULL)   
  204.     {   
  205.         m_hWndParent = hWndParent;   
  206.         return TRUE;   
  207.     }   
  208.   
  209.     LONG lStyle = GetWindowLong(m_hWnd,GWL_STYLE);   
  210.     lStyle &= ~WS_POPUP; //Remove the WS_POPUP flag   
  211.     lStyle |= WS_CHILD;   //Set the WS_CHILD flag   
  212.     SetWindowLong(m_hWnd,GWL_STYLE,lStyle);   
  213.   
  214.     ::SetParent(m_hWnd,hWndParent);   
  215.     m_hWndParent = GetParentWindow();   
  216.        
  217.     ASSERT(m_hWndParent != NULL);   
  218.        
  219.     return (m_hWndParent != NULL);   
  220.   
  221. }   
  222.   
  223.   
  224. BOOL CWndBase::CreateEx(HWND hWndParent,const TSTRING &strWndClass,const TSTRING &strWndName,DWORD dwStyle,BOOL bMsgThrdInside)   
  225. {   
  226.     m_hWndParent = hWndParent;   
  227.     m_dwStyle = dwStyle;   
  228.     m_strWndName = strWndName;   
  229.     m_strWndClass = strWndClass;   
  230.   
  231.   
  232.     //Create the window   
  233.     if(bMsgThrdInside == TRUE)   
  234.     {   
  235.   
  236.         HANDLE hdThrd = CreateThread(NULL,0,CreateProc,this,0,NULL);   
  237.         if(hdThrd == NULL )   
  238.         {   
  239.             return FALSE;   
  240.         }   
  241.         else  
  242.         {   
  243.             CloseHandle(hdThrd);   
  244.   
  245.             //Create the event and wait   
  246.             m_hEventCreated = CreateEvent(NULL,FALSE,FALSE,NULL);   
  247.             if(m_hEventCreated != NULL)   
  248.             {   
  249.                 WaitForSingleObject(m_hEventCreated,INFINITE);   
  250.                 CloseHandle(m_hEventCreated);   
  251.                 m_hEventCreated = NULL;   
  252.   
  253.                 return m_bCreated;   
  254.             }   
  255.             else  
  256.             {   
  257.                 return FALSE;   
  258.             }   
  259.                
  260.         }   
  261.     }   
  262.     else  
  263.     {   
  264.         return CreateWnd(dwStyle,0);   
  265.     }   
  266.   
  267. }   
  268.   
  269. void CWndBase::GetDataForRegistryClass(WNDCLASS &wc)   
  270. {   
  271.     wc.style         = 0;   
  272.     wc.lpfnWndProc   = CWndBase::StaticWndProc;   
  273.     wc.cbClsExtra    = 0;   
  274.     wc.cbWndExtra    = 0;   
  275.     wc.hInstance     = GetModuleHandle(NULL);   
  276.     wc.hIcon         = NULL;    
  277.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);    
  278.     wc.lpszMenuName  = NULL;   
  279.     wc.lpszClassName = m_strWndClass.c_str();      
  280.     wc.hbrBackground = NULL;//If you don't set NULL,it would erase the background when showing that causes flash.   
  281. }  

 

 

 

  1. #pragma once   
  2. #include"Header.h"   
  3. #include <vector>   
  4. #include <map>   
  5.   
  6. namespace EventThread   
  7. {   
  8.     enum Flag   
  9.     {          
  10.         FLAG_ASYNC,   
  11.         FLAG_SYNC,   
  12.     };     
  13. };   
  14.   
  15. class CEventThread   
  16. {   
  17. public:   
  18.     //------------------------------------------   
  19.     //Description:   
  20.     //  Create the thread.After succeeding in creating,you should   
  21.     //call Close to release resource.   
  22.     //   
  23.     //Parameters:   
  24.     //  vtEventName : [in] The event to trigger.       
  25.     //------------------------------------------   
  26.     BOOL Create(const std::vector<TSTRING> &vtEventName);   
  27.   
  28.     //-----------------------------------------------------------------------------------------------------------------   
  29.     //Description:   
  30.     //  Start the thread to run.   
  31.     //Parameters:   
  32.     //  dwTimeout : [in] The timeout for the waiting.   
  33.     //  flag : [in] FLAG_ASYNC -- The function would return immediately after notify the thread to start, which means   
  34.     //                  when the function return the thread may not be running yet.   
  35.     //              FLAG_SYNC -- It wouldn't return when the thread running.    
  36.     //-------------------------------------------------------------------------------------------------------------------   
  37.     BOOL Start(DWORD dwTimeout,EventThread::Flag flag = EventThread::FLAG_SYNC);   
  38.   
  39.     //-----------------------------------------------------------------------------------------------------------------------   
  40.     //Description:   
  41.     //  Stop the thread running.   
  42.     //Parameters:   
  43.     //  flag : [in] FLAG_ASYNC -- The function would return immediately after notify the thread to exit, which means   
  44.     //                  when the function return the thread would still running. You could call IsRunning to check   
  45.     //                  whether the thread really exit.   
  46.     //              FLAG_SYNC -- It wouldn't return when the thread doesn't running. You couldn't set the parameters   
  47.     //                  as the value in the callback function,or it would call dead locked!!   
  48.     //                     
  49.     //------------------------------------------------------------------------------------------------------------------------   
  50.     BOOL Stop(EventThread::Flag flag = EventThread::FLAG_ASYNC);   
  51.   
  52.     //-----------------------------------------------------------------------------------------------   
  53.     //Description:   
  54.     //  Close the thread.   
  55.     //Parameters:   
  56.     //  flag : [in] FLAG_ASYNC -- The function would return immediately after notify the thread to exit, which means   
  57.     //                  when the function return the thread would still running. You could call IsRunning to check   
  58.     //                  whether the thread really exit.   
  59.     //               FLAG_SYNC -- It wouldn't return when the thread doesn't running. You couldn't set the parameters   
  60.     //                  as the value in the callback function,or it would call dead locked!!   
  61.     //                     
  62.     //-----------------------------------------------------------------------------------------------   
  63.     void Close(EventThread::Flag flag);   
  64.   
  65.     //--------------------------------------------------------------------------------------------------------------   
  66.     //Description:   
  67.     //  If the monitor thread doesn't exit,it return TRUE. Be careful,if you succeed in calling Stop but not Close,the    
  68.     //function also return TRUE,because the Stop doesn't cause the monitor process to exit.If you want to check   
  69.     //whether the process is stop or not,you should call IsStop.   
  70.     //--------------------------------------------------------------------------------------------------------------   
  71.     BOOL IsRunning();   
  72.   
  73.     //-------------------------------------------------------------------------------------------------------   
  74.     //Description:   
  75.     //  Check the thread whether stop monitoring event or not. If return TRUE,the function which set by SetHandleEventFunc   
  76.     //or SetHandleTimeoutFunc never be called.   
  77.     //-------------------------------------------------------------------------------------------------------   
  78.     BOOL IsStop();   
  79.   
  80.     //----------------------------------------------------------------   
  81.     //Description:   
  82.     //  Set the callback funtion for the handle event   
  83.     //Parameters:   
  84.     //  pHandleEventFunc : [in] The callback function address   
  85.     //  pParam : [in] The parameter pass to the callback function when calling     
  86.     //---------------------------------------------------------------   
  87.     typedef void (*HANDLE_EVENT_FUNC)(const TSTRING& strEventName,DWORD dwEventData,PVOID pParam);   
  88.     void SetHandleEventFunc(HANDLE_EVENT_FUNC pHandleEventFunc,PVOID pParam);   
  89.   
  90.     //----------------------------------------------------------------   
  91.     //Description:   
  92.     //  Set the callback funtion for the handle timeout   
  93.     //Parameters:   
  94.     //  pHandleTimeoutFunc : [in] The callback function address   
  95.     //  pParam : [in] The parameter pass to the callback function when calling     
  96.     //---------------------------------------------------------------   
  97.     typedef void (*HANDLE_TIMEOUT_FUNC)(PVOID pParam);   
  98.     void SetHandleTimeoutFunc(HANDLE_TIMEOUT_FUNC pHandleTimeoutFunc,PVOID pParam);   
  99.   
  100.     //----------------------------------------------------------------   
  101.     //Description:   
  102.     //  This function sets the priority value for the specified thread.   
  103.     //Parameters:   
  104.     //  iPriority : [in] Specifies the priority value for the thread.   
  105.     //                  You should set the value as follows:   
  106.     //                      THREAD_PRIORITY_TIME_CRITICAL   
  107.     //                      THREAD_PRIORITY_HIGHEST   
  108.     //                      THREAD_PRIORITY_ABOVE_NORMAL   
  109.     //                      THREAD_PRIORITY_NORMAL   
  110.     //                      THREAD_PRIORITY_BELOW_NORMAL   
  111.     //                      THREAD_PRIORITY_LOWEST   
  112.     //                      THREAD_PRIORITY_ABOVE_IDLE   
  113.     //                      THREAD_PRIORITY_IDLE    
  114.     //----------------------------------------------------------------   
  115.     void SetPriority(int iPriority);   
  116.   
  117.     //----------------------------------------------------------------   
  118.     //Description:   
  119.     //  This function get the priority value.   
  120.     //----------------------------------------------------------------   
  121.     int GetPriority();   
  122.   
  123.     //----------------------------------------------------------------   
  124.     //Description:   
  125.     //  This function set the timeout for the event   
  126.     //Parameters:   
  127.     //  dwTimeout : [in] The timeout to set   
  128.     //----------------------------------------------------------------   
  129.     void SetTimeout(DWORD dwTimeout);   
  130.   
  131.     //----------------------------------------------------------------   
  132.     //Description:   
  133.     //  This function Get the timeout for the event   
  134.     //----------------------------------------------------------------   
  135.     DWORD GetTimeout();   
  136.   
  137. public:   
  138.     CEventThread();   
  139.     virtual ~CEventThread();   
  140.   
  141. protected:   
  142.     //---------------------------------------------------------------------   
  143.     //Description:   
  144.     //  Handle the event.It's for the derived class used   
  145.     //Parameters:   
  146.     //  strEventName : [in] The event name   
  147.     //  dwEventData : [in] The event data   
  148.     //----------------------------------------------------------------------   
  149.     virtual void HandleEvent(const TSTRING& strEventName,DWORD dwEventData) {};   
  150.   
  151.     //----------------------------------------------------------------------   
  152.     //Description:   
  153.     //  Handle the timeout.It's for the derived class used   
  154.     //----------------------------------------------------------------------   
  155.     virtual void HandleTimeout() {};   
  156.   
  157.     //----------------------------------------------------------------------   
  158.     //Description:   
  159.     //  Reset all the event   
  160.     //----------------------------------------------------------------------   
  161.     void ResetAllEvent();   
  162.   
  163. private:   
  164.     //---------------------------------------------------------------------------   
  165.     //Description:   
  166.     //  Add the event   
  167.     //Parameters:   
  168.     //  strEventName : [in] The event name to add   
  169.     //---------------------------------------------------------------------------   
  170.     void AddEvent(const TSTRING &strEventName);   
  171.   
  172.     //---------------------------------------------------------------------------   
  173.     //Description:   
  174.     //  The process for monitor the event   
  175.     //---------------------------------------------------------------------------   
  176.     static DWORD WINAPI MonitorProc(PVOID pParam);   
  177.   
  178.     //---------------------------------------------------------------------------   
  179.     //Description:   
  180.     //  Release the resource   
  181.     //---------------------------------------------------------------------------   
  182.     void ReleaserResource();   
  183.   
  184. private:   
  185.     std::vector<HANDLE> m_vtEvent;   
  186.     std::map<DWORD,TSTRING> m_mpEventIndex;   
  187.     HANDLE m_hEventAwake;   
  188.     BOOL m_bCreated;   
  189.     BOOL m_bProcRunning;   
  190.     BOOL m_bStop;   
  191.     DWORD m_dwTimeout;     
  192.     int m_iPriority;   
  193.     HANDLE m_hThrd;   
  194.   
  195.     struct HandleEventFuncData   
  196.     {   
  197.         HANDLE_EVENT_FUNC pHandleEventFunc;   
  198.         PVOID pParam;   
  199.     };   
  200.     HandleEventFuncData m_HandleEventFuncData;   
  201.   
  202.     struct HandleTimeoutFuncData   
  203.     {   
  204.         HANDLE_TIMEOUT_FUNC pHandleTimeoutFunc;   
  205.         PVOID pParam;   
  206.     };   
  207.     HandleTimeoutFuncData m_HandleTimeoutFuncData;   
  208.   
  209.     enum AwakeData   
  210.     {   
  211.         AWD_NONE,   
  212.         AWD_EXIT,   
  213.         AWD_RESTART,   
  214.         AWD_STOP,   
  215.     };   
  216.     AwakeData m_AwakeData;   
  217. };  

 

 

//EventThread.cpp

 

  1. #include "EventThread.h"   
  2. #include <algorithm>   
  3.   
  4. #ifdef _WIN32_WCE   
  5.     #include "Pkfuncs.h"   
  6. #endif //#ifndef _WIN32_WCE   
  7.   
  8. CEventThread::CEventThread():   
  9. m_hEventAwake(NULL),   
  10. m_bCreated(FALSE),   
  11. m_dwTimeout(0),   
  12. m_bProcRunning(FALSE),   
  13. m_AwakeData(AWD_NONE),   
  14. m_iPriority(THREAD_PRIORITY_NORMAL),   
  15. m_hThrd(NULL),   
  16. m_bStop(TRUE)   
  17. {   
  18.     memset(&m_HandleEventFuncData,0,sizeof(m_HandleEventFuncData));   
  19.     memset(&m_HandleTimeoutFuncData,0,sizeof(m_HandleTimeoutFuncData));   
  20.        
  21. }   
  22.   
  23. CEventThread::~CEventThread()   
  24. {   
  25. }   
  26.   
  27. BOOL CEventThread::Create(const std::vector<TSTRING> &vtEventName)   
  28. {   
  29.     BOOL bRes = FALSE;   
  30.   
  31.     if(m_bCreated != FALSE)   
  32.     {   
  33.         return FALSE;   
  34.     }   
  35.   
  36.     for(std::vector<TSTRING>::const_iterator iter = vtEventName.begin(); iter != vtEventName.end(); iter ++)   
  37.     {   
  38.         AddEvent(*iter);   
  39.     }   
  40.   
  41.     //The event for awake only   
  42.     m_hEventAwake = CreateEvent(NULL,TRUE,FALSE,NULL);   
  43.     m_vtEvent.push_back(m_hEventAwake);   
  44.     m_mpEventIndex.insert(std::make_pair(m_mpEventIndex.size(),TEXT("")));   
  45.        
  46.     m_bCreated = TRUE;   
  47.   
  48.     return TRUE;   
  49. }   
  50.   
  51. void CEventThread::Close(EventThread::Flag flag)   
  52. {      
  53.     if(m_bProcRunning != FALSE)   
  54.     {          
  55.         InterlockedExchange(reinterpret_cast<LONG *>(&m_AwakeData),AWD_EXIT);   
  56.         SetEvent(m_hEventAwake);   
  57.   
  58.         if(flag == EventThread::FLAG_ASYNC)   
  59.         {   
  60.             return;   
  61.         }   
  62.   
  63.         //Wait untill the process exit   
  64.         while(InterlockedExchange(reinterpret_cast<LONG *>(&m_bProcRunning),TRUE) == TRUE)   
  65.         {   
  66.             SetEvent(m_hEventAwake);   
  67.             Sleep(100);   
  68.         }   
  69.         InterlockedExchange(reinterpret_cast<LONG *>(&m_bProcRunning),FALSE);   
  70.     }      
  71.   
  72.     CloseHandle(m_hThrd);   
  73.     m_hThrd = NULL;   
  74. }   
  75.   
  76. BOOL CEventThread::Start(DWORD dwTimeout,EventThread::Flag flag)   
  77. {   
  78.     m_dwTimeout = dwTimeout;       
  79.   
  80.     if(m_bProcRunning == FALSE)   
  81.     {   
  82.         InterlockedExchange(reinterpret_cast<LONG *>(&m_AwakeData),AWD_NONE);   
  83.   
  84.         if(m_hThrd != NULL)   
  85.         {   
  86.             CloseHandle(m_hThrd);   
  87.             m_hThrd = NULL;   
  88.         }   
  89.   
  90.         m_hThrd = CreateThread(NULL,NULL,MonitorProc,this,NULL,NULL);   
  91.         SetThreadPriority(m_hThrd,m_iPriority);   
  92.   
  93.         if(flag == EventThread::FLAG_SYNC)   
  94.         {   
  95.             //Wait untill the thread process funtion running.   
  96.             while(InterlockedExchange(reinterpret_cast<LONG *>(&m_bProcRunning),FALSE) == FALSE)   
  97.             {   
  98.                 Sleep(100);   
  99.             }   
  100.             InterlockedExchange(reinterpret_cast<LONG *>(&m_bProcRunning),TRUE);   
  101.         }   
  102.     }      
  103.     else  
  104.     {   
  105.         InterlockedExchange(reinterpret_cast<LONG *>(&m_AwakeData),AWD_RESTART);   
  106.     }      
  107.   
  108.     SetEvent(m_hEventAwake);   
  109.   
  110.        
  111.   
  112.     return TRUE;   
  113. }   
  114.   
  115. BOOL CEventThread::Stop(EventThread::Flag flag)   
  116. {   
  117.     InterlockedExchange(reinterpret_cast<LONG *>(&m_AwakeData),AWD_STOP);   
  118.     SetEvent(m_hEventAwake);   
  119.   
  120.     if(flag == EventThread::FLAG_SYNC)   
  121.     {   
  122.         //Wait untill the flag set TRUE   
  123.         while(InterlockedExchange(reinterpret_cast<LONG *>(&m_bStop),FALSE) == FALSE)   
  124.         {   
  125.             InterlockedExchange(reinterpret_cast<LONG *>(&m_AwakeData),AWD_STOP);   
  126.             SetEvent(m_hEventAwake);   
  127.             Sleep(100);   
  128.         }   
  129.         InterlockedExchange(reinterpret_cast<LONG *>(&m_bStop),TRUE);   
  130.     }   
  131.   
  132.     return TRUE;   
  133. }   
  134.   
  135. void CEventThread::AddEvent(const TSTRING &strEventName)   
  136. {      
  137.     HANDLE hEvent = CreateEvent(NULL,TRUE,FALSE,strEventName.c_str());   
  138.     m_vtEvent.push_back(hEvent);   
  139.     m_mpEventIndex.insert(std::make_pair(m_vtEvent.size() - 1,strEventName.c_str()));   
  140.   
  141.     ResetEvent(hEvent);   
  142.        
  143. }   
  144.   
  145. DWORD WINAPI CEventThread::MonitorProc(PVOID pParam)   
  146. {   
  147.     CEventThread *pObj = reinterpret_cast<CEventThread *>(pParam);   
  148.     if(pObj == NULL)   
  149.     {   
  150.         return 0x10;   
  151.     }   
  152.   
  153.     if(InterlockedExchange(reinterpret_cast<LONG *>(&pObj->m_bProcRunning),TRUE) != FALSE)   
  154.     {   
  155.         //Another thread is running, so exit.   
  156.         return 0x20;   
  157.     }   
  158.        
  159.   
  160.     while(TRUE)   
  161.     {      
  162.         BOOL bStop = FALSE;   
  163.   
  164.         //Set the timeout for the waiting   
  165.         DWORD dwTimeout = 0;   
  166.         if(pObj->m_AwakeData == AWD_STOP)   
  167.         {   
  168.             dwTimeout = INFINITE;   
  169.             InterlockedExchange(reinterpret_cast<LONG *>(&pObj->m_bStop),TRUE);   
  170.                
  171.             bStop = TRUE;              
  172.         }   
  173.         else  
  174.         {   
  175.             dwTimeout = pObj->m_dwTimeout;   
  176.             InterlockedExchange(reinterpret_cast<LONG *>(&pObj->m_bStop),FALSE);   
  177.         }   
  178.   
  179.         if(pObj->m_AwakeData == AWD_EXIT)   
  180.         {   
  181.             //Exit the process   
  182.             break;   
  183.         }   
  184.   
  185.         //Reset the flag   
  186.         InterlockedExchange(reinterpret_cast<LONG *>(&pObj->m_AwakeData),AWD_NONE);   
  187.   
  188.         //Wait for the event   
  189.         DWORD dwObj = 0;           
  190.         if(bStop != FALSE)   
  191.         {   
  192.             //May be the awake event send by the call backfunction,so I need reset it here   
  193.             ResetEvent(pObj->m_hEventAwake);   
  194.   
  195.             dwObj = WaitForSingleObject(pObj->m_hEventAwake,dwTimeout);   
  196.         }   
  197.         else  
  198.         {   
  199.             dwObj = WaitForMultipleObjects(pObj->m_vtEvent.size(),&pObj->m_vtEvent[0],FALSE,dwTimeout);   
  200.         }   
  201.   
  202.         if(WAIT_TIMEOUT != dwObj)   
  203.         {   
  204.             if(pObj->m_AwakeData == AWD_EXIT)   
  205.             {   
  206.                 //Exit the process   
  207.                 break;   
  208.             }   
  209.   
  210.             if(pObj->m_AwakeData == AWD_RESTART || pObj->m_AwakeData == AWD_STOP)   
  211.             {                  
  212.                 ResetEvent(pObj->m_hEventAwake);   
  213.   
  214.                 //Go to the next waitin   
  215.                 continue;   
  216.             }   
  217.   
  218.                
  219.         }   
  220.         else  
  221.         {   
  222.             pObj->HandleTimeout();   
  223.   
  224.             if(pObj->m_HandleTimeoutFuncData.pHandleTimeoutFunc != NULL)   
  225.             {   
  226.                 pObj->m_HandleTimeoutFuncData.pHandleTimeoutFunc(pObj->m_HandleTimeoutFuncData.pParam);   
  227.             }   
  228.             continue;   
  229.         }   
  230.   
  231.         //Because I am afraid that other application notify the event by SetEvent,so I reset the event here.   
  232.         ResetEvent(pObj->m_vtEvent[dwObj - WAIT_OBJECT_0]);   
  233.   
  234.         DWORD dwIndex = dwObj - WAIT_OBJECT_0;   
  235.         if(dwIndex == pObj->m_vtEvent.size() - 1)   
  236.         {   
  237.             //It's the awake event,needn't do anything   
  238.             continue;   
  239.         }   
  240.   
  241.         //Handle the event   
  242.         std::map<DWORD,TSTRING>::iterator iter = pObj->m_mpEventIndex.find(dwObj - WAIT_OBJECT_0);   
  243.         if(iter != pObj->m_mpEventIndex.end())   
  244.         {   
  245.             pObj->HandleEvent(iter->second,GetEventData(pObj->m_vtEvent[dwObj - WAIT_OBJECT_0]));   
  246.   
  247.             if(pObj->m_HandleEventFuncData.pHandleEventFunc != NULL)   
  248.             {   
  249.                 pObj->m_HandleEventFuncData.pHandleEventFunc(iter->second,GetEventData(pObj->m_vtEvent[dwObj - WAIT_OBJECT_0]),pObj->m_HandleEventFuncData.pParam);   
  250.             }   
  251.         }   
  252.            
  253.            
  254.     }   
  255.   
  256.     pObj->ReleaserResource();   
  257.     InterlockedExchange(reinterpret_cast<LONG *>(&pObj->m_bProcRunning),FALSE);   
  258.   
  259.     return 0;   
  260. }   
  261.   
  262. void CEventThread::SetHandleEventFunc(HANDLE_EVENT_FUNC pHandleEventFunc,PVOID pParam)   
  263. {   
  264.     m_HandleEventFuncData.pHandleEventFunc = pHandleEventFunc;   
  265.     m_HandleEventFuncData.pParam = pParam;   
  266. }   
  267.   
  268. void CEventThread::SetHandleTimeoutFunc(HANDLE_TIMEOUT_FUNC pHandleTimeoutFunc,PVOID pParam)   
  269. {   
  270.     m_HandleTimeoutFuncData.pHandleTimeoutFunc = pHandleTimeoutFunc;   
  271.     m_HandleTimeoutFuncData.pParam = pParam;   
  272. }   
  273.   
  274. BOOL CEventThread::IsRunning()   
  275. {   
  276.     return m_bProcRunning;   
  277. }   
  278.   
  279. void CEventThread::ReleaserResource()   
  280. {   
  281.   
  282.     //Close the event   
  283.     std::for_each(m_vtEvent.begin(),m_vtEvent.end(),std::ptr_fun(CloseHandle));   
  284.     m_vtEvent.swap(std::vector<HANDLE>());   
  285.     m_mpEventIndex.clear();   
  286.   
  287.     m_hEventAwake = NULL;   
  288.        
  289.     m_bCreated = FALSE;   
  290. }   
  291.   
  292. void CEventThread::SetPriority(int iPriority)   
  293. {   
  294.     m_iPriority = iPriority;   
  295.   
  296.     if(m_hThrd != NULL)   
  297.     {   
  298.         SetThreadPriority(m_hThrd,m_iPriority);   
  299.     }   
  300. }   
  301.   
  302. void CEventThread::SetTimeout(DWORD dwTimeout)   
  303. {   
  304.     m_dwTimeout = dwTimeout;   
  305.     InterlockedExchange(reinterpret_cast<LONG *>(&m_AwakeData),AWD_RESTART);   
  306.     SetEvent(m_hEventAwake);   
  307. }   
  308.   
  309. DWORD CEventThread::GetTimeout()   
  310. {   
  311.     return m_dwTimeout;   
  312. }   
  313.   
  314. int CEventThread::GetPriority()   
  315. {   
  316.     return m_iPriority;   
  317. }   
  318.   
  319. void CEventThread::ResetAllEvent()   
  320. {   
  321.     for(std::vector<HANDLE>::iterator iter = m_vtEvent.begin(); iter != m_vtEvent.end(); iter ++)   
  322.     {   
  323.         ResetEvent(*iter);   
  324.     }   
  325. }   
  326.   
  327. BOOL CEventThread::IsStop()   
  328. {   
  329.     return m_bStop;   
  330. }  

 

 

 

//AnimateWnd.h

  1. #include "WndBase.h"   
  2. #include "MemDC.h"   
  3. #include "EventThread.h"   
  4. namespace AnimateWnd   
  5. {   
  6.     struct Property   
  7.     {   
  8.         TSTRING strBkImage;   
  9.         RECT    rcImagePosition;   
  10.         RECT    rcDrawPosition;   
  11.         DWORD   dwCount;   
  12.         DWORD   dwTimeOut;   
  13.         BOOL    bVertical;   
  14.     };   
  15. }   
  16.   
  17. class CAnimateWnd:   
  18.     public CWndBase   
  19. {   
  20. public:   
  21.     //----------------------------------------------------------------------   
  22.     //Description:   
  23.     //  Create the window   
  24.     //   
  25.     //Parameters:   
  26.     //  hWndParent : [in] The parent window   
  27.     //  strWndClass : [in] The class of the window   
  28.     //  strWndName : [in] The name of the windows   
  29.     //  bMsgThrdInside : [in] The message thread process is inside or not   
  30.     //----------------------------------------------------------------------   
  31.     virtual BOOL Create(HWND hWndParent,const TSTRING &strWndClass,const TSTRING &strWndName,BOOL bMsgThrdInside = FALSE);   
  32.   
  33.     //--------------------------------------------------------------------   
  34.     //Description:   
  35.     //  Set all the property for the AnimateWnd   
  36.     //---------------------------------------------------------------------   
  37.     BOOL SetProperty(const AnimateWnd::Property &Property);   
  38.   
  39.     //--------------------------------------------------------------------   
  40.     //Description:   
  41.     //  Set all the property for the AnimateWnd   
  42.     //---------------------------------------------------------------------   
  43.     BOOL GetProPerty(AnimateWnd::Property &Property);   
  44.   
  45.     //--------------------------------------------------------------------   
  46.     //Description:   
  47.     //  Start Play animateWnd   
  48.     //---------------------------------------------------------------------   
  49.     void Start();   
  50.   
  51.     //--------------------------------------------------------------------   
  52.     //Description:   
  53.     //  Start Play animateWnd   
  54.     //---------------------------------------------------------------------   
  55.     void Stop();   
  56.   
  57. public:   
  58.     CAnimateWnd();   
  59.     virtual ~CAnimateWnd();   
  60. protected:   
  61.     //----------------------------------------------------------------------   
  62.     //Description:   
  63.     //    You  should see the description in the base class   
  64.     //----------------------------------------------------------------------   
  65.     virtual LRESULT WndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam);   
  66.   
  67.     //----------------------------------------------------------------------   
  68.     //Description:   
  69.     //    Draw image to hdc   
  70.     //----------------------------------------------------------------------   
  71.     void DrawWnd();   
  72.   
  73.     //Draw image to m_MemImge   
  74.     void DrawMemDC();   
  75.   
  76.     //----------------------------------------------------------------------   
  77.     //Description:   
  78.     //    Refresh Thread    
  79.     //----------------------------------------------------------------------   
  80.     static void RefreshProc(LPVOID pArg);   
  81.   
  82. private:   
  83.     AnimateWnd::Property m_Property;   
  84.     CMemDC m_MemImge;   
  85.     DWORD m_dwIndex;   
  86.     CEventThread m_EventRefreshThrd;   
  87.   
  88. };  

 

//AnimateWnd.cpp

  1. #include "AnimateWnd.h"   
  2.   
  3.   
  4. CAnimateWnd::CAnimateWnd():   
  5. m_dwIndex(0)   
  6. {   
  7.     std::vector<TSTRING> vtEvent;   
  8.     m_EventRefreshThrd.Create(vtEvent);   
  9.     m_EventRefreshThrd.SetHandleTimeoutFunc(RefreshProc,this);   
  10. }   
  11.   
  12. CAnimateWnd::~CAnimateWnd()   
  13. {   
  14.     if(m_MemImge.IsOK())   
  15.     {   
  16.         m_MemImge.Delete();   
  17.     }   
  18.   
  19.     m_EventRefreshThrd.Close(EventThread::FLAG_SYNC);   
  20. }   
  21.   
  22. BOOL CAnimateWnd::Create(HWND hWndParent,const TSTRING &strWndClass,const TSTRING &strWndName,BOOL bMsgThrdInside)   
  23. {   
  24.   
  25.     BOOL bReturn = CWndBase::CreateEx(hWndParent,strWndClass,strWndName,WS_TABSTOP,bMsgThrdInside);   
  26.     if(bReturn)   
  27.     {   
  28.         RECT rcParent ={0};   
  29.         ::GetWindowRect(hWndParent,&rcParent);   
  30.         ::MoveWindow(GetWindow(),   
  31.                     m_Property.rcDrawPosition.left + rcParent.left,    
  32.                     m_Property.rcDrawPosition.top + rcParent.top,   
  33.                     m_Property.rcDrawPosition.right - m_Property.rcDrawPosition.left,   
  34.                     m_Property.rcDrawPosition.bottom- m_Property.rcDrawPosition.top,   
  35.                     TRUE);   
  36.   
  37.     }   
  38.   
  39.     return bReturn;   
  40. }   
  41.   
  42.   
  43. LRESULT CAnimateWnd::WndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)   
  44. {   
  45.        
  46.     switch(wMsg)   
  47.     {   
  48.     case WM_PAINT:   
  49.         {   
  50.             PAINTSTRUCT ps;   
  51.             HDC hdc;   
  52.             hdc = BeginPaint(hWnd,&ps);   
  53.             DrawWnd();   
  54.             EndPaint(hWnd,&ps);   
  55.             return 0;   
  56.         }   
  57.     case WM_DESTROY:   
  58.         PostQuitMessage(0);   
  59.         return 0;   
  60.     }   
  61.     return DefWindowProc(hWnd,wMsg,wParam,lParam);   
  62. }   
  63.   
  64. void CAnimateWnd::Start()   
  65. {   
  66.     m_EventRefreshThrd.Start(m_Property.dwTimeOut);   
  67. }   
  68.   
  69. void CAnimateWnd::Stop()   
  70. {   
  71.     m_EventRefreshThrd.Stop();   
  72. }   
  73.   
  74. void CAnimateWnd::RefreshProc(LPVOID pArg)   
  75. {   
  76.     CAnimateWnd *pObj= reinterpret_cast<CAnimateWnd *>(pArg);   
  77.   
  78.     //只有在父窗口和自身窗口可见的时候才需要刷新   
  79.     if( ::IsWindowVisible(pObj->GetParentWindow()) && ::IsWindowVisible(pObj->GetWindow()))   
  80.     {   
  81.         pObj->DrawWnd();   
  82.         pObj->m_dwIndex++;   
  83.   
  84.         if(pObj->m_dwIndex >= pObj->m_Property.dwCount )   
  85.         {   
  86.             pObj->m_dwIndex = 0;   
  87.         }   
  88.   
  89.     }   
  90. }   
  91.   
  92. BOOL CAnimateWnd::SetProperty(const AnimateWnd::Property &Property)   
  93. {   
  94.     m_Property.dwCount        = Property.dwCount;   
  95.     m_Property.dwTimeOut      = Property.dwTimeOut;   
  96.     m_Property.rcDrawPosition = Property.rcDrawPosition;   
  97.     m_Property.rcImagePosition= Property.rcImagePosition;   
  98.     m_Property.strBkImage     = Property.strBkImage;   
  99.     m_Property.bVertical      = Property.bVertical;   
  100.   
  101.     DrawMemDC();   
  102.   
  103.     return TRUE;   
  104. }   
  105.   
  106. BOOL CAnimateWnd::GetProPerty(AnimateWnd::Property &Property)   
  107. {   
  108.     Property.dwCount        = m_Property.dwCount;   
  109.     Property.dwTimeOut      = m_Property.dwTimeOut;   
  110.     Property.rcDrawPosition = m_Property.rcDrawPosition;   
  111.     Property.rcImagePosition= m_Property.rcImagePosition;   
  112.     Property.strBkImage     = m_Property.strBkImage;   
  113.     Property.bVertical      = m_Property.bVertical;   
  114.   
  115.     return TRUE;   
  116. }   
  117.   
  118. void CAnimateWnd::DrawWnd()   
  119. {   
  120.   
  121.     if(!m_MemImge.IsOK())   
  122.     {   
  123.         DrawMemDC();   
  124.     }   
  125.   
  126.     HDC hdc = GetDC(GetWindow());   
  127.   
  128.     HBITMAP hBmp = SHLoadDIBitmap(m_Property.strBkImage.c_str());    
  129.   
  130.     if(m_Property.bVertical == TRUE)   
  131.     {   
  132.         int iHeight = m_MemImge.GetHeight()/ m_Property.dwCount;   
  133.   
  134.         StretchBlt( hdc,   
  135.             0,   
  136.             0,   
  137.             m_Property.rcDrawPosition.right - m_Property.rcDrawPosition.left,   
  138.             m_Property.rcDrawPosition.bottom - m_Property.rcDrawPosition.top,   
  139.             m_MemImge.GetDC(),   
  140.             0,   
  141.             iHeight * m_dwIndex,   
  142.             m_MemImge.GetWidth(),   
  143.             iHeight,   
  144.             SRCCOPY   
  145.             );   
  146.     }   
  147.     else  
  148.     {   
  149.         int iWidth = m_MemImge.GetWidth() / m_Property.dwCount;   
  150.   
  151.         RECT rc;   
  152.         GetClientRect(GetWindow(),&rc);   
  153.         StretchBlt( hdc,   
  154.             0,   
  155.             0,   
  156.             m_Property.rcDrawPosition.right - m_Property.rcDrawPosition.left,   
  157.             m_Property.rcDrawPosition.bottom - m_Property.rcDrawPosition.top,   
  158.             m_MemImge.GetDC(),   
  159.             iWidth * m_dwIndex,   
  160.             0,   
  161.             iWidth,   
  162.             m_MemImge.GetHeight(),   
  163.             SRCCOPY   
  164.             );   
  165.   
  166.     }   
  167.   
  168.     ReleaseDC(GetWindow(),hdc);   
  169.   
  170. }   
  171.   
  172. void CAnimateWnd::DrawMemDC()   
  173. {   
  174.     if(m_MemImge.IsOK())   
  175.     {   
  176.         m_MemImge.Delete();   
  177.     }   
  178.   
  179.     HDC hdc = GetDC(GetWindow());   
  180.   
  181.     SIZE size ={m_Property.rcImagePosition.right - m_Property.rcImagePosition.left,m_Property.rcImagePosition.bottom - m_Property.rcImagePosition.top};   
  182.     m_MemImge.Create(hdc,&size);   
  183.   
  184.     ReleaseDC(GetWindow(),hdc);   
  185.   
  186. #ifdef _WIN32_WCE   
  187.     HBITMAP hBmp = SHLoadDIBitmap(m_Property.strBkImage.c_str());    
  188. #else   
  189.     HBITMAP hBmp = reinterpret_cast<HBITMAP>(LoadImage(NULL,m_Property.strBkImage.c_str(),IMAGE_BITMAP,0,0,LR_LOADFROMFILE));   
  190. #endif   
  191.   
  192.     if(hBmp == NULL)   
  193.     {   
  194.         return ;   
  195.     }   
  196.   
  197.     //Crete the bitmap DC   
  198.     HDC hdcBmp = CreateCompatibleDC(m_MemImge.GetDC());   
  199.     HGDIOBJ hOldSel = SelectObject(hdcBmp,hBmp);   
  200.   
  201.     StretchBlt(   
  202.         m_MemImge.GetDC(),   
  203.         0,   
  204.         0,   
  205.         m_MemImge.GetWidth(),   
  206.         m_MemImge.GetHeight(),   
  207.         hdcBmp,   
  208.         m_Property.rcImagePosition.left,   
  209.         m_Property.rcImagePosition.top,   
  210.         size.cx,   
  211.         size.cy,   
  212.         SRCCOPY   
  213.         );   
  214.   
  215.     //Delete the bitmap DC   
  216.     SelectObject(hdcBmp,hOldSel);   
  217.     DeleteDC(hdcBmp);   
  218.   
  219.     //Delete the bmp   
  220.     DeleteObject(hBmp);   
  221. }  

 

其中代码中CMemDC类的实现在http://blog.csdn.net/lanyzh0909/archive/2010/08/07/5795590.aspx有实现代码.

测试代码:

  1. #include"AnimateWnd.h"   
  2. CAnimateWnd g_AnimateWnd;   
  3. int WINAPI WinMain( HINSTANCE hInstance,   
  4.                    HINSTANCE hPrevInstance,   
  5.                    LPTSTR    lpCmdLine,   
  6.                    int       nCmdShow)   
  7. {   
  8.     MSG msg;   
  9.     CWndBase MainWnd;   
  10.     MainWnd.Create(NULL,TEXT("MAIN_CLS"),TEXT("窗口显示"));   
  11.     MainWnd.ShowWindow(TRUE);   
  12.   
  13.     //初始化窗口属性   
  14.     AnimateWnd::Property pro;   
  15.     RECT Drawrect = {80,27,401,200};   
  16.     RECT Imagerect={0,0,1920,177};   
  17.     pro.dwCount = 6;   
  18.     pro.dwTimeOut = 300;   
  19.     pro.rcDrawPosition = Drawrect;   
  20.     pro.rcImagePosition = Imagerect;   
  21.     pro.bVertical = FALSE;   
  22.     pro.strBkImage = TEXT("//NAND//WinCeTemplate//1.bmp");   
  23.   
  24.     // 调用成员函数设置窗口属性   
  25.     g_AnimateWnd.SetProperty(pro);   
  26.   
  27.     //调用成员函数创建窗口,其中hWnd是父窗口的句柄,如果没有父窗口的话直接设置为NULL   
  28.     g_AnimateWnd.Create(MainWnd.GetWindow(),TEXT("ANIMATE_CLS"),TEXT("ANIMATE_NAME"));   
  29.   
  30.     //调用成员函数显示窗口   
  31.     g_AnimateWnd.ShowWindow(TRUE);   
  32.   
  33.     //调用成员函数动态显示图片   
  34.     g_AnimateWnd.Start();   
  35.   
  36.   
  37.     while(GetMessage(&msg,NULL,0,0))   
  38.     {   
  39.         TranslateMessage(&msg);   
  40.         DispatchMessage(&msg);   
  41.     }   
  42.     return 0;   
  43. }  

//EventThread.h

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值