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

  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2.  
  3. Copyright (c) 1998 Microsoft Corporation
  4.  
  5. Module Name:
  6.  
  7.     hello.cpp
  8.  
  9. Abstract:
  10.  
  11.     Hello World sample application.  Only source file.
  12.  
  13.     This sample creates one form, an EventSink attached to the form 
  14.     (to receive WM_COMMAND messages), and a power list box (a menu-like
  15.     object) control placed on the form.  
  16.  
  17. Environment:
  18.  
  19.     AutoPC
  20.  
  21. -------------------------------------------------------------------*/
  22. //Standard includes
  23. #include <windows.h>        // Standard Win CE header file
  24. #include <olectl.h>            // COM functions
  25. #include <asfc.h>           // AutoPC Forms Manager
  26. #include <ascmnctl.h>       // AutoPC Common Controls
  27.  
  28. //Application specific includes
  29. #include "resource.h"
  30.  
  31. // Globals
  32. WCHAR*            SZEXIT = L"Exit";
  33.  
  34. IfmManage*        g_pManage = NULL;      // the forms manager object
  35. IASForm*        g_pForm = NULL;        // the main application form
  36. IASEventSink*    g_pSink = NULL;        // the default event sink
  37. DWORD           g_idThread = 0;         // the ID of the main application thread
  38. BSTR            g_bstrAppName=0;
  39. HINSTANCE        g_hInst;
  40.  
  41. // Prototypes
  42. HRESULT CreateEventSink();
  43. HRESULT GetFormsManager();
  44. HRESULT CreateMainForm();
  45. HRESULT AddControls();
  46. HRESULT PutCaption(IASControl *pControl, WCHAR *wsz);
  47.  
  48. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  49. Class:
  50.     CAppEventSink
  51.  
  52. Description:
  53.     Implements IASEventSink interface (to receive the exit WM_COMMAND)
  54. -------------------------------------------------------------------*/
  55. class CAppEventSink : public IASEventSink
  56. {
  57. public:
  58.  
  59.     CAppEventSink() { m_cRef = 0; }        //Constructor initializes reference count
  60.     ~CAppEventSink(void) {}                //Destructor does nothing
  61.  
  62.     // IUnknown implementation
  63.     // Standard COM QueryInterface. We support both IUnknown, IDispatch and IASEventSink
  64.     STDMETHODIMP QueryInterface(REFIID riid, PVOID *ppv)
  65.     {
  66.         if (!ppv) return E_INVALIDARG;        //incoming pointer invalid
  67.  
  68.         //Return an interface pointer if we implement the requested interface
  69.         if (riid == IID_ASEVENTSINK) *ppv = (PVOID) (IASEventSink *) this; 
  70.         else if (riid == IID_IUnknown) *ppv = (PVOID) (IUnknown *) this;
  71.         else if (riid == IID_IDispatch)    *ppv = (PVOID) (IDispatch *) this;
  72.         else return E_NOINTERFACE;
  73.  
  74.         AddRef();    //Add to our reference count
  75.  
  76.         return NOERROR;
  77.     }
  78.  
  79.     // Increment the object's reference count
  80.     STDMETHODIMP_(ULONG) AddRef(void)
  81.     {
  82.         InterlockedIncrement(&m_cRef);
  83.         return m_cRef;         
  84.     }
  85.  
  86.     // Decrease reference count, delete this object if necessary
  87.     STDMETHODIMP_(ULONG) Release(void)
  88.     {
  89.         if (InterlockedDecrement(&m_cRef) > 0)
  90.             return m_cRef;
  91.  
  92.         delete this;
  93.         return 0; 
  94.     }            
  95.  
  96.     // IASEventSink
  97.     // This method is invoked whenever an event is fired
  98.     STDMETHODIMP ReceiveMsg(long uMsg, long wParam, long lParam)
  99.     {
  100.         if(uMsg != WM_COMMAND) return NOERROR;    //We only care about WM_COMMAND msgs
  101.         
  102.         switch(HIWORD(lParam))
  103.         {
  104.             case ID_EXIT:
  105.                 PostThreadMessage(g_idThread, WM_QUIT, 0, 0); //Send the quit mesage to this app's thread
  106.                 break;
  107.                     
  108.             default:
  109.                 break;
  110.         }
  111.         return NOERROR;
  112.     }
  113.  
  114.     // IDispatch token implementation.  All functions return E_NOTIMPL
  115.     STDMETHODIMP GetTypeInfoCount(UINT *pctInfo)            
  116.         { return E_NOTIMPL; }                                                    
  117.     STDMETHODIMP GetTypeInfo(UINT itinfo, LCID lcid, ITypeInfo **pptinfo)            
  118.         { return E_NOTIMPL; }                                                    
  119.     STDMETHODIMP GetIDsOfNames(REFIID riid,    OLECHAR **rgszNames, UINT cNames, LCID    lcid, DISPID *rgdispids)
  120.         { return E_NOTIMPL; }                                                    
  121.     STDMETHODIMP Invoke(DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pdispparams,            
  122.                         VARIANT *pVarResult, EXCEPINFO *pei, UINT *puArgErr)                        
  123.         { return E_NOTIMPL;    }                                                    
  124.  
  125. protected:
  126.     long     m_cRef; // this object's ref count
  127. };
  128.  
  129.  
  130. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  131. Function:
  132.     WinMain
  133.  
  134. Description:
  135.     Main Win32 Application entry point
  136.  
  137. Parameters:
  138.     Standard Win32 CE params.
  139. -------------------------------------------------------------------*/
  140. int APIENTRY WinMain(HINSTANCE hInst, 
  141.                      HINSTANCE hPrevInst, 
  142.                      LPWSTR wszCmd, 
  143.                      int nCmdShow)
  144. {
  145.     MSG         msg;
  146.     HRESULT     hr;
  147.  
  148.     g_idThread = GetCurrentThreadId();  // save the id of the main thread
  149.     g_hInst = hInst;                    // Save instance handle for LoadString calls
  150.     
  151.     // Initialize COM and the Forms Manager
  152.     CoInitializeEx(NULL, COINIT_MULTITHREADED);
  153.     hr = InitASFormsManager();
  154.     if (FAILED(hr)) goto LReturn;
  155.  
  156.     hr = CreateEventSink(); // create the event sink
  157.     if (FAILED(hr)) goto LReturn;
  158.     
  159.     hr = GetFormsManager(); // get a pointer to the forms manager
  160.     if (FAILED(hr)) goto LReturn;
  161.  
  162.     hr = CreateMainForm();  // create the main application form
  163.     if (FAILED(hr)) goto LReturn;
  164.     
  165.     // Our main message pump.  We just dispatch all messages coming
  166.     // into our application
  167.     while (GetMessage(&msg, NULL, 0, 0))  // loop until the application quits
  168.     {
  169.         DispatchMessage(&msg);
  170.     }
  171.     // If GetMessage returns NULL, that means our application received
  172.     // a WM_QUIT or WM_CLOSE message, so we exit the message loop
  173.     // and clean up.
  174.  
  175. LReturn:   
  176.  
  177.     // We HAVE to deregister with the forms manager
  178.     if (g_pManage && g_bstrAppName){
  179.            g_pManage->DeregisterStartedApplication(FC_MAINFACEPLATE, g_bstrAppName);
  180.     }
  181.  
  182.     if (g_pForm){
  183.         g_pForm->Close();
  184.         g_pForm->Release();        // release the form
  185.     }
  186.  
  187.     if (g_pManage){
  188.         g_pManage->Release();
  189.     }
  190.  
  191.     if (g_pSink){               // release event sink
  192.         g_pSink->Release();
  193.     }
  194.  
  195.     if (g_bstrAppName){
  196.         SysFreeString(g_bstrAppName);
  197.     }
  198.  
  199.     UnInitASFormsManager();     // uninitialize the forms manager
  200.     CoUninitialize();           // uninitialize COM
  201.         
  202.     return TRUE; 
  203. }
  204.  
  205. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  206. Function:
  207.     CreateMainForm
  208.  
  209. Description:
  210.     Creates the one and only form and calls AddControls
  211. -------------------------------------------------------------------*/
  212. HRESULT CreateMainForm(void)
  213. {
  214.     HRESULT hr = NOERROR;
  215.  
  216.     // Create the form
  217.     hr = CoCreateInstance(CLSID_ASFORM, NULL, CLSCTX_INPROC_SERVER, IID_ASFORM, 
  218.                             (PVOID *) &g_pForm);
  219.     if (FAILED(hr)) return hr;
  220.  
  221.     hr = g_pForm->Init(ID_MAINFORM, NULL, g_pSink); // initialize it
  222.     if (FAILED(hr)) goto LReturn;
  223.     
  224.     hr = g_pManage->Start(g_pForm); // start the form
  225.     if (FAILED(hr)) goto LReturn;
  226.  
  227.     PutCaption(g_pForm, g_bstrAppName);  // give it a title
  228.  
  229.     hr = AddControls();                      // add controls
  230.     if (FAILED(hr)) goto LReturn;
  231.  
  232.     // We have to make the form visible (or we'll get no display)
  233.     hr = g_pForm->put_Visible(TRUE);
  234.         
  235. LReturn:
  236.     if (FAILED(hr))             // there was an error. Cleanup
  237.         {
  238.         g_pForm->Close();   // close the form
  239.         g_pForm->Release();
  240.         g_pForm = NULL;
  241.         }
  242.  
  243.     return hr;
  244. }
  245.  
  246. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  247. Function:
  248.     CreateEventSink
  249.  
  250. Description:
  251.     Creates application event sink.  First we create our object of
  252.     type CAppEventSink, which leaves that objects reference count at
  253.     0.  Then we QueryInterface, get an IASEventSink interface pointer,
  254.     and increment the objects reference count.
  255.  
  256.     At application clean-up time, we will Release() our interface,
  257.     which will decrement the object's reference count to 0, and delete
  258.     the object.
  259. -------------------------------------------------------------------*/
  260. HRESULT CreateEventSink(void)
  261. {
  262.     HRESULT hr;
  263.     CAppEventSink *pSink;
  264.  
  265.     pSink = new CAppEventSink();  // create an event sink object
  266.     if (!pSink) return E_OUTOFMEMORY;
  267.     
  268.     // get an interface for us to keep
  269.     hr = pSink->QueryInterface(IID_ASEVENTSINK, (PVOID *) &g_pSink);
  270.     if (FAILED(hr)) delete pSink;
  271.     
  272.     return hr;
  273. }
  274.  
  275. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  276. Function:
  277.     GetFormsManager
  278.  
  279. Description:
  280.     Creates the forms manager and registers the application as started
  281. -------------------------------------------------------------------*/
  282. HRESULT GetFormsManager()
  283. {
  284.     HRESULT         hr;
  285.                 
  286.     hr = CoCreateInstance(CLSID_FMMANAGE, NULL, CLSCTX_INPROC_SERVER, IID_FMMANAGE, 
  287.                             (PVOID *) &g_pManage);
  288.     if (FAILED(hr)) goto LReturn;
  289.  
  290.     WCHAR wszAppName[ _MAX_PATH + 1 ];
  291.  
  292.     //We have to use the name in the resource script with ID #1 (STR_APP_SHELL_NAME)
  293.     LoadString(g_hInst, STR_APP_SHELL_NAME, wszAppName,_MAX_PATH );
  294.  
  295.     g_bstrAppName = ::SysAllocString(wszAppName);
  296.  
  297.     if (g_bstrAppName){ 
  298.         // Register or application as started with the forms manager, Also, send it
  299.         // Which faceplate to execute on (for future support of multiple screens)
  300.         g_pManage->RegisterStartedApplication(FC_MAINFACEPLATE, g_bstrAppName, 0, 0);
  301.     } 
  302.     else {
  303.         hr = E_OUTOFMEMORY;
  304.     }
  305.     
  306.     hr = g_pManage->MoveAppToForeground(GetCurrentProcessId(), 0, 0);    
  307.  
  308. LReturn:
  309.     return hr;
  310. }
  311.  
  312. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  313. Function:
  314.     AddControls
  315.  
  316. Description:
  317.     Adds the Clock, Label, and Power List Box control to the main form
  318. -------------------------------------------------------------------*/
  319. HRESULT AddControls(void)
  320. {
  321.     HRESULT                    hr;
  322.     IASPowerListBox*        pPLB = NULL;
  323.     IASPowerListBoxItem*    pPLBCExit = NULL;
  324.     BSTR                    m_bstrExit = NULL;
  325.  
  326.     //Create the powerlistbox control
  327.     hr = CoCreateInstance(
  328.         CLSID_ASPOWERLISTBOX,        // GUID of power list box class 
  329.         NULL,                         // Doesn't use COM aggregation
  330.         CLSCTX_INPROC_SERVER,         // Create object in our process
  331.         IID_ASPOWERLISTBOX,            // Give us the IASPowerListBox interface
  332.         (PVOID *) &pPLB);     
  333.  
  334.     if (FAILED(hr))    
  335.         goto LReturn;
  336.  
  337.     //Create a command item in the power list box
  338.     hr = pPLB->CreateItem(&pPLBCExit, ID_EXIT, ASFC_PWRLB_TYPE_COMMAND);
  339.     if (FAILED(hr)) 
  340.         goto LReturn;
  341.  
  342.     //Give the command item a name
  343.     m_bstrExit = SysAllocString(SZEXIT);
  344.     if (!m_bstrExit) 
  345.     {
  346.         hr = E_OUTOFMEMORY;
  347.         goto LReturn;
  348.     }
  349.     pPLBCExit->put_Caption(m_bstrExit);
  350.     pPLBCExit->Release();
  351.     SysFreeString(m_bstrExit);
  352.  
  353.     pPLB->SetBounds(0,18,225,45);                //Format
  354.     pPLB->put_Alignment(ASFC_ALIGN_CENTER);        //And
  355.     g_pForm->Add(pPLB, IDC_PLB);                //Add control to form
  356.  
  357. LReturn:
  358.     if(pPLB) pPLB->Release();
  359.  
  360.     return hr;
  361. }
  362.  
  363. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  364. Function:
  365.     PutCaption
  366.  
  367. Description:
  368.     Takes the given WCHAR *, converts it to a BSTR and does a 
  369.     put_caption on the given control
  370.  
  371. Parameters:
  372.     IASControl *pControl - Control to add caption too
  373.     WCHAR *wsz - Caption string
  374. -------------------------------------------------------------------*/
  375. HRESULT PutCaption(IASControl *pControl, WCHAR *wsz)
  376. {
  377.     BSTR bstr;
  378.     HRESULT hr;
  379.  
  380.     bstr = ::SysAllocString(wsz);
  381.     if (!bstr)
  382.         return E_OUTOFMEMORY;
  383.  
  384.     // Set the caption
  385.     hr = pControl->put_Caption(bstr);
  386.  
  387.     ::SysFreeString(bstr);
  388.  
  389.     return hr;
  390. }
  391.