home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / AutoPC / apcsdk10.exe / data1.cab / Win32_Samples / win32 / simpleclock / scapp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-13  |  9.4 KB  |  377 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. #include <apcdebug.h>
  26.  
  27. //Application header files
  28. #include "resource.h"
  29. #include "simpleclock.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_pClockLabel    = NULL;
  52.     m_pForm            = NULL;
  53.     m_pFormEventSink= NULL;
  54.     m_pLabel        = NULL;    
  55.     m_pManager        = NULL;
  56. }
  57.  
  58. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  59. Function:
  60.     ~CSimpleClockApp
  61.  
  62. Description:
  63.     Destructor, does nothing in this case
  64. -------------------------------------------------------------------*/
  65.  
  66. CSimpleClockApp::~CSimpleClockApp()
  67. {
  68.  
  69. }
  70.  
  71. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  72. Function:
  73.     Init
  74.  
  75. Description:
  76.     Creates sinks, forms manager, form, and adds controls
  77.  
  78. Parameters:
  79.     returns TRUE/FALSE success
  80. -------------------------------------------------------------------*/
  81.  
  82. BOOL CSimpleClockApp::Init()
  83. {
  84.     HRESULT hr;
  85.  
  86.     hr = InitASFormsManager();   // Initialize the forms manager
  87.     if(FAILED(hr)) return FALSE;
  88.  
  89.     if(!LoadBSTR(STR_APP_SHELL_NAME, &m_bstrAppName)) return FALSE;
  90.     if(!LoadBSTR(IDS_EXITTEXT, &m_bstrExitText)) return FALSE;
  91.     if(!CreateSinks()) return FALSE;
  92.     if(!CreateFormsManager()) return FALSE;
  93.     if(!CreateMainForm()) return FALSE;
  94.     if(!AddControls()) return FALSE;
  95.  
  96.     //Bring app to the foreground (only necessary for applications not launched from shell)
  97.     hr = m_pManager->MoveAppToForeground(GetCurrentProcessId(), 0, ASFC_APPSTYLEFLG_POPUP);
  98.     if(FAILED(hr)) return FALSE;
  99.  
  100.     return TRUE;
  101. }
  102.  
  103. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  104. Function:
  105.     CreateSinks
  106.  
  107. Description:
  108.     Creates form event sink (m_pFormEventSink) and form message sink (m_pFormMsmSink)
  109.  
  110. Parameters:
  111.     returns TRUE/FALSE success
  112. -------------------------------------------------------------------*/
  113.  
  114.  
  115. BOOL CSimpleClockApp::CreateSinks()
  116. {
  117.     CSimpleClockSink*    pAppSink;
  118.     HRESULT             hr;
  119.  
  120.     pAppSink = new CSimpleClockSink(m_dwThreadID);  //Create our event sink object
  121.  
  122.     if (!pAppSink) return FALSE;
  123.  
  124.     hr = pAppSink->QueryInterface(IID_ASEVENTSINK, (PVOID *) &m_pFormEventSink);
  125.     if(FAILED(hr)) {
  126.         delete pAppSink;
  127.         return FALSE;
  128.         }
  129.     
  130.     hr = pAppSink->QueryInterface(IID_ASCLASSMSGSINK, (PVOID *) &m_pFmMsgSink);
  131.     if(FAILED(hr)) {
  132.         delete pAppSink;
  133.         return FALSE;
  134.         }
  135.     
  136.     return TRUE;
  137. }
  138.  
  139. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  140. Function:
  141.     CreateFormsManager
  142.  
  143. Description:
  144.     Gets form manager interface and Forms Context Handle
  145.  
  146. Parameters:
  147.     returns TRUE/FALSE success
  148. -------------------------------------------------------------------*/
  149.  
  150.  
  151. BOOL CSimpleClockApp::CreateFormsManager()
  152. {
  153.        HRESULT hr;
  154.  
  155.     // Create the forms manager
  156.     hr = CoCreateInstance(CLSID_FMMANAGE,    NULL, CLSCTX_INPROC_SERVER,
  157.         IID_FMMANAGE, (PVOID *) &m_pManager);
  158.  
  159.     if(FAILED(hr)) return FALSE;
  160.  
  161.     hr = m_pManager->GetFormsContextHandle(&m_hFC);
  162.     if(FAILED(hr)) return FALSE;
  163.  
  164.     return TRUE;
  165. }
  166.  
  167. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  168. Function:
  169.     CreateMainForm
  170.  
  171. Description:
  172.     Create the main form.
  173.  
  174. Parameters:
  175.     returns TRUE/FALSE success
  176. -------------------------------------------------------------------*/
  177.  
  178. BOOL CSimpleClockApp::CreateMainForm()
  179. {
  180.     HRESULT hr;
  181.  
  182.     // Create the form
  183.     hr = CoCreateInstance(CLSID_ASFORM, NULL, CLSCTX_INPROC_SERVER, 
  184.         IID_ASFORM, (PVOID *) &m_pForm);
  185.     if(FAILED(hr)) goto Error;
  186.     
  187.     // initialize the newly created form
  188.     hr = m_pForm->Init(ID_MAINFORM, NULL, m_pFormEventSink); 
  189.     if(FAILED(hr)) goto Error;
  190.     
  191.     IASUserNavArrows * pNavArrows;    //Get nav arrow interface and hide them
  192.     m_pForm->get_NavArrows(&pNavArrows);
  193.     pNavArrows->put_Visible(FALSE);
  194.     pNavArrows->Release();
  195.  
  196.     hr = m_pManager->Start(m_pForm); //Start the form up
  197.     if(FAILED(hr)) goto Error;
  198.  
  199.     //Set up message and event sinks
  200.     hr = m_pManager->put_ClassMsgSink(m_pFmMsgSink);
  201.     if(FAILED(hr)) goto Error;
  202.  
  203.     hr = m_pManager->put_EventSink(m_pFormEventSink);
  204.     if(FAILED(hr)) goto Error;
  205.  
  206.     m_pForm->put_Visible(TRUE); //Show it
  207.     m_pForm->put_Caption(m_bstrAppName);
  208.  
  209.     // Register the application with the forms manager.
  210.     hr = m_pManager->RegisterStartedApplication(m_hFC, m_bstrAppName, 0, 0);
  211.     if(FAILED(hr)) goto Error;
  212.  
  213.     return TRUE;
  214.  
  215. Error:
  216.     return FALSE;
  217.  
  218. }
  219.  
  220. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  221. Function:
  222.     AddControls
  223.  
  224. Description:
  225.     Adds the clock and label controls
  226.  
  227. Parameters:
  228.     returns TRUE/FALSE success
  229. -------------------------------------------------------------------*/
  230.  
  231. BOOL CSimpleClockApp::AddControls()
  232. {
  233.     HRESULT hr;
  234.  
  235.     //Create clock and add it to form
  236.     hr = CoCreateInstance(
  237.         CLSID_ASCLOCK,            // Clock control Class ID
  238.         NULL,                    // No COM Aggregation
  239.         CLSCTX_INPROC_SERVER,    // Create object in our process space
  240.         IID_ASCLOCK,            // Request the IASClock interface
  241.         (PVOID *) &m_pClock);    // Target for requested interface
  242.     if(FAILED(hr)) return FALSE;
  243.     m_pForm->Add(m_pClock, IDC_CLOCK);
  244.  
  245.     //Create a label for the clock to convey information through
  246.     hr = CoCreateInstance(
  247.         CLSID_ASLABEL,            // Label control class ID
  248.         NULL,                    // No COM aggregation
  249.         CLSCTX_INPROC_SERVER,    // Create object in our process space
  250.         IID_ASLABEL,            // Request the IASLabel interface
  251.         (PVOID *) &m_pClockLabel);//Target for requested interface
  252.     if(FAILED(hr)) return FALSE;
  253.  
  254.     // Place control bounds (x, y, cx, cy)
  255.     m_pClockLabel->SetBounds(0,20,256, 20);
  256.     
  257.     // Center text within bounds
  258.     m_pClockLabel->put_Format(ASFC_ALIGN_VCENTER | ASFC_ALIGN_CENTER);
  259.  
  260.     // Change the font (make it a couple points larger)
  261.     IFontDisp* pFontDisp;
  262.     m_pClockLabel->get_Font(&pFontDisp);
  263.     IFont* pFont;
  264.     pFontDisp->QueryInterface(IID_IFont, (void**)&pFont);
  265.  
  266.     CY cyFont = { 14 * 10000, 0 };    // Point size 12
  267.     pFont->put_Size(cyFont);
  268.  
  269.     m_pClockLabel->put_Font(pFontDisp);
  270.     pFontDisp->Release();
  271.     pFont->Release();
  272.  
  273.     // Add the label to the form
  274.     m_pForm->Add(m_pClockLabel, ASFC_ID_NONE);
  275.  
  276.     // Set our new label as the clock's output label
  277.     m_pClock->put_ClockLabel0(m_pClockLabel);
  278.  
  279.     //Create the label and add it to form
  280.     hr = CoCreateInstance(
  281.         CLSID_ASLABEL, 
  282.         NULL, 
  283.         CLSCTX_INPROC_SERVER, 
  284.         IID_ASLABEL, 
  285.         (PVOID *) &m_pLabel);
  286.     if(FAILED(hr)) return FALSE;
  287.     m_pLabel->SetBounds(0, 40, 256, 20);
  288.     m_pLabel->put_ForeColor(RGB(255,0,0));
  289.     m_pLabel->put_Caption(m_bstrExitText);
  290.     m_pLabel->put_Format(ASFC_ALIGN_VCENTER | ASFC_ALIGN_CENTER);
  291.     m_pForm->Add(m_pLabel, ASFC_ID_NONE);
  292.  
  293.     return TRUE;
  294. }
  295.  
  296. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  297. Function:
  298.     ProcessMessages
  299.  
  300. Description:
  301.     Default message handler (called when message isn't directed towards a window
  302.  
  303. Parameters:
  304.     [IN] LONG uMsg - Windows message
  305.     [IN] LONG wParam, lParam - Standard window message params
  306. -------------------------------------------------------------------*/
  307.  
  308. void CSimpleClockApp::ProcessMessage(LONG uMsg, LONG wParam, LONG lParam)
  309. {
  310.     
  311. }
  312.  
  313. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  314. Function:
  315.     Clean
  316.  
  317. Description:
  318.     Releases all interface pointers
  319. -------------------------------------------------------------------*/
  320.  
  321. void CSimpleClockApp::Clean()
  322. {
  323.     // Since our application is finished, deregister with the forms manager
  324.     m_pManager->DeregisterStartedApplication(m_hFC, m_bstrAppName);
  325.  
  326.     // Clean up any strings we use
  327.     SysFreeString(m_bstrAppName);
  328.     SysFreeString(m_bstrExitText);
  329.  
  330.     // Close and release the form
  331.     if(m_pForm) {
  332.         m_pForm->Close();
  333.         m_pForm->Release();
  334.     }
  335.     
  336.     // Release all the other interfaces we used
  337.     if(m_pClock)        m_pClock->Release();
  338.     if(m_pClockLabel)    m_pClockLabel->Release();
  339.     if(m_pLabel)        m_pLabel->Release();
  340.     if(m_pManager)        m_pManager->Release();
  341.     if(m_pFmMsgSink)    m_pFmMsgSink->Release();
  342.     if(m_pFormEventSink)m_pFormEventSink->Release();
  343. }
  344.  
  345.  
  346. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  347. Function:
  348.     LoadBSTR
  349.  
  350. Description:
  351.     Loads a resource string (uID) into a BSTR.  Max length 128
  352. -------------------------------------------------------------------*/
  353.  
  354. BOOL CSimpleClockApp::LoadBSTR(UINT uID, BSTR* pBStr)
  355. {
  356.     if(!pBStr)
  357.         return    FALSE;
  358.  
  359.     WCHAR wszTemp[128];
  360.     int iStringLen = 128;
  361.  
  362.     if((iStringLen = LoadStringW(m_hInst, uID, wszTemp, iStringLen)) == 0)
  363.     {
  364.         return(FALSE);
  365.     }        
  366.     
  367.     *pBStr = SysAllocString(wszTemp);
  368.     // see if the string was allocated
  369.     if (!(*pBStr))
  370.     {
  371.         return(FALSE);
  372.     }
  373.  
  374.     return(TRUE);
  375. }
  376.  
  377.