📄 cosmo.cpp
字号:
/*
* COSMO.CPP
* Cosmo Chapter 14
*
* WinMain and CCosmoFrame implementations.
*
* Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
*
* Kraig Brockschmidt, Microsoft
* Internet : kraigb@microsoft.com
* Compuserve: >INTERNET:kraigb@microsoft.com
*/
#define INITGUIDS
#include "cosmo.h"
//CHAPTER14MOD
/*
* These are for proper implementation of the class factory,
* automation support, and shutdown conditions.
*/
ULONG g_cObj=0;
ULONG g_cLock=0;
HWND g_hWnd=NULL;
BOOL g_fUser=FALSE;
//End CHAPTER14MOD
/*
* WinMain
*
* Purpose:
* Main entry point of application. Should register the app class
* if a previous instance has not done so and do any other one-time
* initializations.
*/
int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev
, LPSTR pszCmdLine, int nCmdShow)
{
PCCosmoFrame pFR;
FRAMEINIT fi;
WPARAM wRet;
SETMESSAGEQUEUE;
//Attempt to allocate and initialize the application
pFR=new CCosmoFrame(hInst, hPrev, pszCmdLine, nCmdShow);
if (NULL==pFR)
return -1;
fi.idsMin=IDS_FRAMEMIN;
fi.idsMax=IDS_FRAMEMAX;
fi.idsStatMin=IDS_STATMESSAGEMIN;
fi.idsStatMax=IDS_STATMESSAGEMAX;
fi.idStatMenuMin=ID_MENUFILE;
fi.idStatMenuMax=ID_MENUHELP;
fi.iPosWindowMenu=WINDOW_MENU;
fi.cMenus=CMENUS;
fi.x=CW_USEDEFAULT;
fi.y=CW_USEDEFAULT;
fi.cx=440;
fi.cy=460;
//If we can initialize pFR, start chugging messages
if (pFR->Init(&fi))
wRet=pFR->MessageLoop();
delete pFR;
return wRet;
}
//CHAPTER14MOD
/*
* ObjectDestroyed
*
* Purpose:
* Function for various objects to call when they are destroyed.
* The main window is closed if the proper conditions are met.
*/
void ObjectDestroyed(void)
{
g_cObj--;
//No more objects, no locks, no user control, shut the app down.
if (0L==g_cObj && 0L==g_cLock && IsWindow(g_hWnd) && !g_fUser)
PostMessage(g_hWnd, WM_CLOSE, 0, 0L);
return;
}
//CHAPTER14MOD
/*
* CCosmoFrame::CCosmoFrame
* CCosmoFrame::~CCosmoFrame
*
* Constructor Parameters:
* hInst HINSTANCE from WinMain
* hInstPrev HINSTANCE from WinMain
* pszCmdLine LPSTR from WinMain
* nCmdShow int from WInMain
*/
CCosmoFrame::CCosmoFrame(HINSTANCE hInst, HINSTANCE hInstPrev
, LPSTR pszCmdLine, int nCmdShow)
: CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
{
UINT i;
for (i=0; i<5; i++)
m_hBmpLines[i]=NULL;
m_uIDCurLine=0;
m_fInitialized=FALSE;
m_pIClassDataTran=NULL;
//CHAPTER14MOD
m_dwRegCOApp=0;
m_dwRegCOFig=0;
m_pIClassFactoryApp=NULL;
m_pIClassFactoryFig=NULL;
m_fEmbedding=FALSE;
m_fAutomation=FALSE;
m_pAutoApp=NULL;
m_dwActiveApp=0L;
//End CHAPTER14MOD
return;
}
CCosmoFrame::~CCosmoFrame(void)
{
UINT i;
//CHAPTER14MOD
if (0L!=m_dwActiveApp)
{
RevokeActiveObject(m_dwActiveApp, NULL);
m_dwActiveApp=0L;
}
//m_pAutoApp is cleaned up in CAutoApp::Release
//Reverse CoRegisterClassObject, takes class factory refs to 1
if (0L!=m_dwRegCOApp)
CoRevokeClassObject(m_dwRegCOApp);
if (0L!=m_dwRegCOFig)
CoRevokeClassObject(m_dwRegCOFig);
//Should be last Releases to free the class factories
ReleaseInterface(m_pIClassFactoryApp);
ReleaseInterface(m_pIClassFactoryFig);
//End CHAPTER14MOD
for (i=0; i<5; i++)
{
if (NULL!=m_hBmpLines[i])
DeleteObject(m_hBmpLines[i]);
}
if (NULL!=m_pIClassDataTran)
{
m_pIClassDataTran->LockServer(FALSE);
m_pIClassDataTran->Release();
}
OleFlushClipboard();
if (m_fInitialized)
OleUninitialize();
return;
}
/*
* CCosmoFrame::Init
*
* Purpose:
* Call CoInitialize then calling down into the base class
* initialization.
*
* Parameters:
* pFI PFRAMEINIT containing initialization parameters.
*
* Return Value:
* BOOL TRUE if initialization succeeded, FALSE otherwise.
*/
BOOL CCosmoFrame::Init(PFRAMEINIT pFI)
{
HRESULT hr;
//CHAPTER14MOD
UINT i;
//End CHAPTER14MOD
CHECKVER_OLE;
if (FAILED(OleInitialize(NULL)))
return FALSE;
m_fInitialized=TRUE;
/*
* Obtain the Data Transfer Object class factory and lock it.
* This will improve the speed of clipboard and (later)
* drag & drop operations that involve this class.
*/
hr=CoGetClassObject(CLSID_DataTransferObject
, CLSCTX_INPROC_SERVER, NULL, IID_IClassFactory
, (PPVOID)&m_pIClassDataTran);
if (SUCCEEDED(hr))
m_pIClassDataTran->LockServer(TRUE);
//CHAPTER14MOD
//Check for command line flags
ParseCommandLine();
for (i=0; i < m_cCmdArgs; i++)
{
if(0==lstrcmpi(m_ppszCmdArgs[i], TEXT("-Embedding"))
|| 0==lstrcmpi(m_ppszCmdArgs[i], TEXT("/Embedding")))
m_fEmbedding=TRUE;
if(0==lstrcmpi(m_ppszCmdArgs[i], TEXT("-Automation"))
|| 0==lstrcmpi(m_ppszCmdArgs[i], TEXT("/Automation")))
m_fAutomation=TRUE;
}
/*
* Create the 'application' object for automation and make
* it the 'active' one with SetActiveObject.
*/
m_pAutoApp=new CAutoApp(this);
if (NULL==m_pAutoApp)
return FALSE;
if (!m_pAutoApp->Init(TRUE))
return FALSE;
/*
* We potentially have two class factories to register.
* We always register the multi-use factory for
* CLSID_CosmoFigure. We only register the single-use
* factory for CLSID_Cosmo2Application if the /Automation
* flag is present, that is, if we actually were launched
* for that express purpose.
*/
if (m_fAutomation)
{
m_pIClassFactoryApp=new CClassFactory(this
, CLSID_Cosmo2Application);
if (NULL==m_pIClassFactoryApp)
return FALSE;
m_pIClassFactoryApp->AddRef();
hr=CoRegisterClassObject(CLSID_Cosmo2Application
, m_pIClassFactoryApp, CLSCTX_LOCAL_SERVER
, REGCLS_SINGLEUSE, &m_dwRegCOApp);
if (FAILED(hr))
return FALSE;
/*
* When registering the application as active, you can
* either register weak, which will not AddRef your object,
* or you can register strong and implement
* IExternalConnection. For Cosmo it is most appropriate
* to register weak, but in order to work on Win16 as well
* as demonstrate the more complex technique, we'll register
* strong and implement IExternalConnection. This interface
* is handled in CAutoBase and simply calls Release to
* match the AddRef here, which will do shutdown as needed.
*/
RegisterActiveObject((IUnknown *)m_pAutoApp
, CLSID_Cosmo2Application, ACTIVEOBJECT_STRONG
, &m_dwActiveApp);
/*
* This is necessary to balance the Release in
* IExternalConnection::ReleaseConnection
*/
m_pAutoApp->AddRef();
}
m_pIClassFactoryFig=new CClassFactory(this, CLSID_CosmoFigure);
if (NULL==m_pIClassFactoryFig)
return FALSE;
m_pIClassFactoryFig->AddRef();
hr=CoRegisterClassObject(CLSID_CosmoFigure
, m_pIClassFactoryFig, CLSCTX_LOCAL_SERVER
, REGCLS_MULTIPLEUSE, &m_dwRegCOFig);
if (FAILED(hr))
return FALSE;
//End CHAPTER14MOD
return CFrame::Init(pFI);
}
/*
* CCosmoFrame::CreateCClient
*
* Purpose:
* Constructs a new client specific to the application.
*
* Parameters:
* None
*
* Return Value:
* PCClient Pointer to the new client object.
*/
PCClient CCosmoFrame::CreateCClient(void)
{
return (PCClient)(new CCosmoClient(m_hInst, this));
}
/*
* CCosmoFrame::RegisterAllClasses
*
* Purpose:
* Registers all classes used in this application.
*
* Parameters:
* None
*
* Return Value:
* BOOL TRUE if registration succeeded, FALSE otherwise.
*/
BOOL CCosmoFrame::RegisterAllClasses(void)
{
WNDCLASS wc;
//First let the standard frame do its thing
if (!CFrame::RegisterAllClasses())
return FALSE;
/*
* We want a different background color for the document
* because the Polyline we put in the document will paint
* with COLOR_WINDOW which by default which is CLASSLIB's
* default document color.
*/
GetClassInfo(m_hInst, SZCLASSDOCUMENT, &wc);
UnregisterClass(SZCLASSDOCUMENT, m_hInst);
wc.hbrBackground=(HBRUSH)(COLOR_APPWORKSPACE+1);
if (!RegisterClass(&wc))
return FALSE;
//Register the Polyline window.
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.hInstance = m_hInst;
wc.cbClsExtra = 0;
wc.lpfnWndProc = PolylineWndProc;
wc.cbWndExtra = CBPOLYLINEWNDEXTRA;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_CROSS);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = SZCLASSPOLYLINE;
if (!RegisterClass(&wc))
return FALSE;
return TRUE;
}
/*
* CCosmoFrame::PreShowInit
*
* Purpose:
* Called from Init before intially showing the window. We do
* whatever else we want here, modifying nCmdShow as necessary
* which affects ShowWindow in Init.
*
* Parameters:
* None
*
* Return Value:
* BOOL TRUE if this initialization succeeded,
* FALSE otherwise.
*/
BOOL CCosmoFrame::PreShowInit(void)
{
CreateLineMenu();
CheckLineSelection(IDM_LINESOLID);
//CHAPTER14MOD
//Save the window handle for shutdown if necessary.
g_hWnd=m_hWnd;
//If we're under OLE control, don't show the main window.
if (m_fEmbedding || m_fAutomation)
m_nCmdShow=SW_HIDE;
//End CHAPTER14MOD
return TRUE;
}
/*
* CCosmoFrame::CreateLineMenu
*
* Purpose:
* Initializes the bitmaps used to create the Line menu and
* replaces the text items defined in the application resources
* with these bitmaps. Note that the contents of m_hBmpLines
* must be cleaned up when the application terminates.
*
* Parameters:
* None
*
* Return Value:
* None
*/
void CCosmoFrame::CreateLineMenu(void)
{
HMENU hMenu;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -