home *** CD-ROM | disk | FTP | other *** search
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- Copyright (c) 1998 Microsoft Corporation
-
- Module Name:
-
- SimpleClockApp.cpp
-
- Abstract:
-
- Main Application class. Interface pointers, init functions, etc.
-
- Environment:
-
- AutoPC
-
- -------------------------------------------------------------------*/
-
- //Non-application header files
- #include <windows.h>
- #include <olectl.h>
- #include <asfc.h> //Header for AS Forms Manager
- #include <ascmnctl.h> //Header for AS Common Controls
- #include <keypad.h>
-
- //Application header files
- #include "resource.h"
- #include "SimpleClockSink.h"
- #include "SimpleClockApp.h"
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CSimpleClockApp
-
- Description:
- App constructor, Nulls all member variables.
-
- Parameters:
- [IN] DWORD - Current process thread Id
- -------------------------------------------------------------------*/
-
- CSimpleClockApp::CSimpleClockApp(DWORD dwThreadID)
- {
- //save the threadID
- m_dwThreadID = dwThreadID;
-
- //Null other variables
- m_hFC = NULL;
- m_pFmMsgSink = NULL;
- m_pClock = NULL;
- m_pForm = NULL;
- m_pFormEventSink = NULL;
- m_pLabel = NULL;
- m_pManager = NULL;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- ~CSimpleClockApp
-
- Description:
- Destructor, does nothing in this case
- -------------------------------------------------------------------*/
-
- CSimpleClockApp::~CSimpleClockApp()
- {
-
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- Init
-
- Description:
- Creates sinks, forms manager, form, and adds controls
-
- Parameters:
- returns TRUE/FALSE success
- -------------------------------------------------------------------*/
-
- BOOL CSimpleClockApp::Init()
- {
- HRESULT hr;
-
- InitASFormsManager(); // Initialize the forms manager
-
- if(!LoadBSTR(STR_APP_SHELL_NAME, &m_bstrAppName)) return FALSE;
- if(!LoadBSTR(IDS_EXITTEXT, &m_bstrExitText)) return FALSE;
- if(!CreateSinks()) return FALSE;
- if(!CreateFormsManager()) return FALSE;
- if(!CreateMainForm()) return FALSE;
- if(!AddControls()) return FALSE;
-
- //Bring app to the foreground (only necessary for applications not launched from shell)
- hr = m_pManager->MoveAppToForeground(GetCurrentProcessId(), 0, ASFC_APPSTYLEFLG_POPUP);
- if(FAILED(hr)) return FALSE;
-
- return TRUE;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CreateSinks
-
- Description:
- Creates form event sink (m_pFormEventSink) and form message sink (m_pFormMsmSink)
-
- Parameters:
- returns TRUE/FALSE success
- -------------------------------------------------------------------*/
-
-
- BOOL CSimpleClockApp::CreateSinks()
- {
- CSimpleClockSink* pAppSink;
- HRESULT hr;
-
- pAppSink = new CSimpleClockSink(m_dwThreadID); //Create our event sink object
-
- if (!pAppSink) return FALSE;
-
- hr = pAppSink->QueryInterface(IID_ASEVENTSINK, (PVOID *) &m_pFormEventSink);
- if(FAILED(hr)) {
- delete pAppSink;
- return FALSE;
- }
-
- hr = pAppSink->QueryInterface(IID_ASCLASSMSGSINK, (PVOID *) &m_pFmMsgSink);
- if(FAILED(hr)) {
- delete pAppSink;
- return FALSE;
- }
-
- return TRUE;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- CreateFormsManager
-
- Description:
- Gets form manager interface and Forms Context Handle
-
- Parameters:
- returns TRUE/FALSE success
- -------------------------------------------------------------------*/
-
-
- BOOL CSimpleClockApp::CreateFormsManager()
- {
- HRESULT hr;
-
- // Create the forms manager
- hr = CoCreateInstance(CLSID_FMMANAGE, NULL, CLSCTX_INPROC_SERVER,
- IID_FMMANAGE, (PVOID *) &m_pManager);
-
- if(FAILED(hr)) return FALSE;
-
- hr = m_pManager->GetFormsContextHandle(&m_hFC);
- if(FAILED(hr)) return FALSE;
-
- return TRUE;
- }
-
- BOOL CSimpleClockApp::CreateMainForm()
- {
- HRESULT hr;
-
- // Create the form
- hr = CoCreateInstance(CLSID_ASFORM, NULL, CLSCTX_INPROC_SERVER,
- IID_ASFORM, (PVOID *) &m_pForm);
- if(FAILED(hr)) goto Error;
-
- // initialize the newly created form
- hr = m_pForm->Init(ID_MAINFORM, NULL, m_pFormEventSink);
- if(FAILED(hr)) goto Error;
-
- IASUserNavArrows * pNavArrows; //Get nav arrow interface and hide them
- m_pForm->get_NavArrows(&pNavArrows);
- pNavArrows->put_Visible(FALSE);
-
- hr = m_pManager->Start(m_pForm); //Start the form up
- if(FAILED(hr)) goto Error;
-
- //Set up message and event sinks
- hr = m_pManager->put_ClassMsgSink(m_pFmMsgSink);
- if(FAILED(hr)) goto Error;
-
- hr = m_pManager->put_EventSink(m_pFormEventSink);
- if(FAILED(hr)) goto Error;
-
- m_pForm->put_Visible(TRUE); //Show it
- m_pForm->put_Caption(m_bstrAppName);
-
- // Register the application with the forms manager.
- hr = m_pManager->RegisterStartedApplication(m_hFC, m_bstrAppName, 0, 0);
- if(FAILED(hr)) goto Error;
-
- return TRUE;
-
- Error:
- return FALSE;
-
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- AddControls
-
- Description:
- Adds the clock and label controls
-
- Parameters:
- returns TRUE/FALSE success
- -------------------------------------------------------------------*/
-
- BOOL CSimpleClockApp::AddControls()
- {
- HRESULT hr;
-
- //Create clock and add it to form
- hr = CoCreateInstance(CLSID_ASCLOCK, NULL, CLSCTX_INPROC_SERVER,
- IID_ASCLOCK, (PVOID *) &m_pClock);
- if(FAILED(hr)) return FALSE;
- m_pClock->SetBounds(70, 20, 98, 20);
- m_pForm->Add(m_pClock, IDC_CLOCK);
-
- //Create the label and add it to form
- hr = CoCreateInstance(CLSID_ASLABEL, NULL, CLSCTX_INPROC_SERVER,
- IID_ASLABEL, (PVOID *) &m_pLabel);
- if(FAILED(hr)) return FALSE;
- m_pLabel->SetBounds(0, 40, 256, 20);
- m_pLabel->put_ForeColor(RGB(255,0,0));
- m_pLabel->put_Caption(m_bstrExitText);
- m_pLabel->put_Format(ASFC_ALIGN_VCENTER | ASFC_ALIGN_CENTER);
- m_pForm->Add(m_pLabel, ASFC_ID_NONE);
-
- return TRUE;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- ProcessMessages
-
- Description:
- Default message handler (called when message isn't directed towards a window
-
- Parameters:
- [IN] LONG uMsg - Windows message
- [IN] LONG wParam, lParam - Standard window message params
- -------------------------------------------------------------------*/
-
- void CSimpleClockApp::ProcessMessage(LONG uMsg, LONG wParam, LONG lParam)
- {
- ;
- }
-
- /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- Function:
- Clean
-
- Description:
- Releases all interface pointers
- -------------------------------------------------------------------*/
-
- void CSimpleClockApp::Clean()
- {
- m_pManager->DeregisterStartedApplication(m_hFC, m_bstrAppName);
- SysFreeString(m_bstrAppName);
- SysFreeString(m_bstrExitText);
- if(m_pClock) m_pClock->Release();
- if(m_pLabel) m_pLabel->Release();
- if(m_pManager) m_pManager->Release();
- if(m_pForm) {
- m_pForm->Close();
- m_pForm->Release();
- }
- if(m_pFmMsgSink) m_pFmMsgSink->Release();
- if(m_pFormEventSink) m_pFormEventSink->Release();
- }
-
- BOOL CSimpleClockApp::LoadBSTR(UINT uID, BSTR* pBStr)
- {
- if(!pBStr)
- return FALSE;
-
- WCHAR wszTemp[128];
- int iStringLen = 128;
-
- if((iStringLen = LoadStringW(m_hInst, uID, wszTemp, iStringLen)) == 0)
- {
- return(FALSE);
- }
-
- *pBStr = SysAllocString(wszTemp);
- // see if the string was allocated
- if (!(*pBStr))
- {
- return(FALSE);
- }
-
- return(TRUE);
- }
-
-