home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / AutoPC / apcsdk10.exe / data1.cab / Win32_Samples / win32 / simpleclock / simpleclockapp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-13  |  7.4 KB  |  305 lines

  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2.  
  3. Copyright (c) 1998 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     SimpleClockApp.cpp
  8.  
  9. Abstract:
  10.  
  11.     Main Application class.  Interface pointers, init functions, etc.
  12.  
  13. Environment:
  14.  
  15.     AutoPC
  16.  
  17. -------------------------------------------------------------------*/
  18.  
  19. //Non-application header files
  20. #include <windows.h>
  21. #include <olectl.h>
  22. #include <asfc.h>        //Header for AS Forms Manager
  23. #include <ascmnctl.h>    //Header for AS Common Controls
  24. #include <keypad.h>
  25.  
  26. //Application header files
  27. #include "resource.h"
  28. #include "SimpleClockSink.h"
  29. #include "SimpleClockApp.h"
  30.  
  31. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  32. Function:
  33.     CSimpleClockApp
  34.  
  35. Description:
  36.     App constructor, Nulls all member variables.
  37.  
  38. Parameters:
  39.     [IN] DWORD - Current process thread Id
  40. -------------------------------------------------------------------*/
  41.  
  42. CSimpleClockApp::CSimpleClockApp(DWORD dwThreadID)
  43. {
  44.     //save the threadID
  45.     m_dwThreadID = dwThreadID;
  46.  
  47.     //Null other variables
  48.     m_hFC = NULL;
  49.     m_pFmMsgSink = NULL;
  50.     m_pClock = NULL;
  51.     m_pForm = NULL;
  52.     m_pFormEventSink = NULL;
  53.     m_pLabel = NULL;    
  54.     m_pManager = NULL;
  55. }
  56.  
  57. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  58. Function:
  59.     ~CSimpleClockApp
  60.  
  61. Description:
  62.     Destructor, does nothing in this case
  63. -------------------------------------------------------------------*/
  64.  
  65. CSimpleClockApp::~CSimpleClockApp()
  66. {
  67.  
  68. }
  69.  
  70. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  71. Function:
  72.     Init
  73.  
  74. Description:
  75.     Creates sinks, forms manager, form, and adds controls
  76.  
  77. Parameters:
  78.     returns TRUE/FALSE success
  79. -------------------------------------------------------------------*/
  80.  
  81. BOOL CSimpleClockApp::Init()
  82. {
  83.     HRESULT hr;
  84.  
  85.     InitASFormsManager();   // Initialize the forms manager
  86.  
  87.     if(!LoadBSTR(STR_APP_SHELL_NAME, &m_bstrAppName)) return FALSE;
  88.     if(!LoadBSTR(IDS_EXITTEXT, &m_bstrExitText)) return FALSE;
  89.     if(!CreateSinks()) return FALSE;
  90.     if(!CreateFormsManager()) return FALSE;
  91.     if(!CreateMainForm()) return FALSE;
  92.     if(!AddControls()) return FALSE;
  93.  
  94.     //Bring app to the foreground (only necessary for applications not launched from shell)
  95.     hr = m_pManager->MoveAppToForeground(GetCurrentProcessId(), 0, ASFC_APPSTYLEFLG_POPUP);
  96.     if(FAILED(hr)) return FALSE;
  97.  
  98.     return TRUE;
  99. }
  100.  
  101. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  102. Function:
  103.     CreateSinks
  104.  
  105. Description:
  106.     Creates form event sink (m_pFormEventSink) and form message sink (m_pFormMsmSink)
  107.  
  108. Parameters:
  109.     returns TRUE/FALSE success
  110. -------------------------------------------------------------------*/
  111.  
  112.  
  113. BOOL CSimpleClockApp::CreateSinks()
  114. {
  115.     CSimpleClockSink*    pAppSink;
  116.     HRESULT             hr;
  117.  
  118.     pAppSink = new CSimpleClockSink(m_dwThreadID);  //Create our event sink object
  119.  
  120.     if (!pAppSink) return FALSE;
  121.  
  122.     hr = pAppSink->QueryInterface(IID_ASEVENTSINK, (PVOID *) &m_pFormEventSink);
  123.     if(FAILED(hr)) {
  124.         delete pAppSink;
  125.         return FALSE;
  126.         }
  127.     
  128.     hr = pAppSink->QueryInterface(IID_ASCLASSMSGSINK, (PVOID *) &m_pFmMsgSink);
  129.     if(FAILED(hr)) {
  130.         delete pAppSink;
  131.         return FALSE;
  132.         }
  133.     
  134.     return TRUE;
  135. }
  136.  
  137. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  138. Function:
  139.     CreateFormsManager
  140.  
  141. Description:
  142.     Gets form manager interface and Forms Context Handle
  143.  
  144. Parameters:
  145.     returns TRUE/FALSE success
  146. -------------------------------------------------------------------*/
  147.  
  148.  
  149. BOOL CSimpleClockApp::CreateFormsManager()
  150. {
  151.        HRESULT hr;
  152.  
  153.     // Create the forms manager
  154.     hr = CoCreateInstance(CLSID_FMMANAGE,    NULL, CLSCTX_INPROC_SERVER,
  155.         IID_FMMANAGE, (PVOID *) &m_pManager);
  156.  
  157.     if(FAILED(hr)) return FALSE;
  158.  
  159.     hr = m_pManager->GetFormsContextHandle(&m_hFC);
  160.     if(FAILED(hr)) return FALSE;
  161.  
  162.     return TRUE;
  163. }
  164.  
  165. BOOL CSimpleClockApp::CreateMainForm()
  166. {
  167.     HRESULT hr;
  168.  
  169.     // Create the form
  170.     hr = CoCreateInstance(CLSID_ASFORM, NULL, CLSCTX_INPROC_SERVER, 
  171.         IID_ASFORM, (PVOID *) &m_pForm);
  172.     if(FAILED(hr)) goto Error;
  173.     
  174.     // initialize the newly created form
  175.     hr = m_pForm->Init(ID_MAINFORM, NULL, m_pFormEventSink); 
  176.     if(FAILED(hr)) goto Error;
  177.     
  178.     IASUserNavArrows * pNavArrows;    //Get nav arrow interface and hide them
  179.     m_pForm->get_NavArrows(&pNavArrows);
  180.     pNavArrows->put_Visible(FALSE);
  181.  
  182.     hr = m_pManager->Start(m_pForm); //Start the form up
  183.     if(FAILED(hr)) goto Error;
  184.  
  185.     //Set up message and event sinks
  186.     hr = m_pManager->put_ClassMsgSink(m_pFmMsgSink);
  187.     if(FAILED(hr)) goto Error;
  188.  
  189.     hr = m_pManager->put_EventSink(m_pFormEventSink);
  190.     if(FAILED(hr)) goto Error;
  191.  
  192.     m_pForm->put_Visible(TRUE); //Show it
  193.     m_pForm->put_Caption(m_bstrAppName);
  194.  
  195.     // Register the application with the forms manager.
  196.     hr = m_pManager->RegisterStartedApplication(m_hFC, m_bstrAppName, 0, 0);
  197.     if(FAILED(hr)) goto Error;
  198.  
  199.     return TRUE;
  200.  
  201. Error:
  202.     return FALSE;
  203.  
  204. }
  205.  
  206. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  207. Function:
  208.     AddControls
  209.  
  210. Description:
  211.     Adds the clock and label controls
  212.  
  213. Parameters:
  214.     returns TRUE/FALSE success
  215. -------------------------------------------------------------------*/
  216.  
  217. BOOL CSimpleClockApp::AddControls()
  218. {
  219.     HRESULT hr;
  220.  
  221.     //Create clock and add it to form
  222.     hr = CoCreateInstance(CLSID_ASCLOCK, NULL, CLSCTX_INPROC_SERVER, 
  223.         IID_ASCLOCK, (PVOID *) &m_pClock);
  224.     if(FAILED(hr)) return FALSE;
  225.     m_pClock->SetBounds(70, 20, 98, 20);
  226.     m_pForm->Add(m_pClock, IDC_CLOCK);
  227.  
  228.     //Create the label and add it to form
  229.     hr = CoCreateInstance(CLSID_ASLABEL, NULL, CLSCTX_INPROC_SERVER, 
  230.         IID_ASLABEL, (PVOID *) &m_pLabel);
  231.     if(FAILED(hr)) return FALSE;
  232.     m_pLabel->SetBounds(0, 40, 256, 20);
  233.     m_pLabel->put_ForeColor(RGB(255,0,0));
  234.     m_pLabel->put_Caption(m_bstrExitText);
  235.     m_pLabel->put_Format(ASFC_ALIGN_VCENTER | ASFC_ALIGN_CENTER);
  236.     m_pForm->Add(m_pLabel, ASFC_ID_NONE);
  237.  
  238.     return TRUE;
  239. }
  240.  
  241. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  242. Function:
  243.     ProcessMessages
  244.  
  245. Description:
  246.     Default message handler (called when message isn't directed towards a window
  247.  
  248. Parameters:
  249.     [IN] LONG uMsg - Windows message
  250.     [IN] LONG wParam, lParam - Standard window message params
  251. -------------------------------------------------------------------*/
  252.  
  253. void CSimpleClockApp::ProcessMessage(LONG uMsg, LONG wParam, LONG lParam)
  254. {
  255.     ;
  256. }
  257.  
  258. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  259. Function:
  260.     Clean
  261.  
  262. Description:
  263.     Releases all interface pointers
  264. -------------------------------------------------------------------*/
  265.  
  266. void CSimpleClockApp::Clean()
  267. {
  268.     m_pManager->DeregisterStartedApplication(m_hFC, m_bstrAppName);
  269.     SysFreeString(m_bstrAppName);
  270.     SysFreeString(m_bstrExitText);
  271.     if(m_pClock) m_pClock->Release();
  272.     if(m_pLabel) m_pLabel->Release();
  273.     if(m_pManager) m_pManager->Release();
  274.     if(m_pForm) {
  275.         m_pForm->Close();
  276.         m_pForm->Release();
  277.     }
  278.     if(m_pFmMsgSink) m_pFmMsgSink->Release();
  279.     if(m_pFormEventSink) m_pFormEventSink->Release();
  280. }
  281.  
  282. BOOL CSimpleClockApp::LoadBSTR(UINT uID, BSTR* pBStr)
  283. {
  284.     if(!pBStr)
  285.         return    FALSE;
  286.  
  287.     WCHAR wszTemp[128];
  288.     int iStringLen = 128;
  289.  
  290.     if((iStringLen = LoadStringW(m_hInst, uID, wszTemp, iStringLen)) == 0)
  291.     {
  292.         return(FALSE);
  293.     }        
  294.     
  295.     *pBStr = SysAllocString(wszTemp);
  296.     // see if the string was allocated
  297.     if (!(*pBStr))
  298.     {
  299.         return(FALSE);
  300.     }
  301.  
  302.     return(TRUE);
  303. }
  304.  
  305.