📄 square.cpp
字号:
/*
* SQUARE.CPP
* Square Rendering/Calculation Automation Object Chapter 15
*
* An automation object that draws a square in various rotational
* states and provides access to it through IDispatch.
*
* This file contains all the code for this sample.
*
* Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
*
* Kraig Brockschmidt, Microsoft
* Internet : kraigb@microsoft.com
* Compuserve: >INTERNET:kraigb@microsoft.com
*/
#define INITGUIDS
#include "square.h"
ULONG g_cObj=0;
ULONG g_cLock=0;
HWND g_hWnd=NULL;
/*
* WinMain
*
* Purpose:
* Main entry point of application.
*/
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hInstPrev
, LPSTR pszCmdLine, int nCmdShow)
{
MSG msg;
PCApp pApp;
SETMESSAGEQUEUE;
pApp=new CApp(hInst, hInstPrev, pszCmdLine, nCmdShow);
if (NULL==pApp)
return -1;
if (pApp->Init())
{
while (GetMessage(&msg, NULL, 0,0 ))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
delete pApp;
return msg.wParam;
}
/*
* MainWndProc
*
* Purpose:
* Standard window class procedure.
*/
LRESULT APIENTRY MainWndProc(HWND hWnd, UINT iMsg
, WPARAM wParam, LPARAM lParam)
{
switch (iMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return (DefWindowProc(hWnd, iMsg, wParam, lParam));
}
return 0L;
}
/*
* ObjectDestroyed
*
* Purpose:
* Function for the object to call when it gets destroyed.
* We destroy the main window if the proper conditions are met
* for shutdown.
*/
void ObjectDestroyed(void)
{
g_cObj--;
//No more objects and no locks, shut the app down.
if (0L==g_cObj && 0L==g_cLock && IsWindow(g_hWnd))
PostMessage(g_hWnd, WM_CLOSE, 0, 0L);
return;
}
/*
* CApp::CApp
* CApp::~CApp
*
* Constructor Parameters:
* hInst HINSTANCE of the Application from WinMain
* hInstPrev HINSTANCE of a previous instance from WinMain
* pszCmdLine LPSTR of the command line.
* nCmdShow UINT specifying how to show the app window,
* from WinMain.
*/
CApp::CApp(HINSTANCE hInst, HINSTANCE hInstPrev
, LPSTR pszCmdLine, UINT nCmdShow)
{
//Initialize WinMain parameter holders.
m_hInst =hInst;
m_hInstPrev =hInstPrev;
m_pszCmdLine=pszCmdLine;
m_nCmdShow =nCmdShow;
m_dwRegCO=0;
m_pIClassFactory=NULL;
m_fInitialized=FALSE;
return;
}
CApp::~CApp(void)
{
//Opposite of CoRegisterClassObject; class factory ref is now 1
if (0L!=m_dwRegCO)
CoRevokeClassObject(m_dwRegCO);
//The last Release, which frees the class factory.
if (NULL!=m_pIClassFactory)
m_pIClassFactory->Release();
if (m_fInitialized)
CoUninitialize();
return;
}
/*
* CApp::Init
*
* Purpose:
* Initializes an CApp object by registering window classes,
* creating the main window, and doing anything else prone to
* failure. If this function fails the caller should guarantee
* that the destructor is called.
*
* Return Value:
* BOOL TRUE if successful, FALSE otherwise.
*/
BOOL CApp::Init(void)
{
WNDCLASS wc;
HRESULT hr;
HWND hWnd;
if (lstrcmpiA(m_pszCmdLine, "-Embedding"))
return FALSE;
CHECKVER_OLE;
if (FAILED(CoInitialize(NULL)))
return FALSE;
m_fInitialized=TRUE;
if (!m_hInstPrev)
{
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = m_hInst;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("SphereSquare");
if (!RegisterClass(&wc))
return FALSE;
//Child window in which to draw
wc.lpfnWndProc = SquareWndProc;
wc.cbWndExtra = CBSQUAREWNDEXTRA;
wc.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = SZCLASSSQUARE;
if (!RegisterClass(&wc))
return FALSE;
}
//This window is never shown
hWnd=CreateWindow(TEXT("SphereSquare"), TEXT("SphereSquare")
, WS_OVERLAPPEDWINDOW, 100, 100, 400, 400
, NULL, NULL, m_hInst, NULL);
if (NULL==hWnd)
return FALSE;
g_hWnd=hWnd;
m_pIClassFactory=new CSquareClassFactory(hWnd, m_hInst);
if (NULL==m_pIClassFactory)
return FALSE;
m_pIClassFactory->AddRef();
hr=CoRegisterClassObject(CLSID_SphereSquare, m_pIClassFactory
, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &m_dwRegCO);
if (FAILED(hr))
return FALSE;
return TRUE;
}
//Class Factory implementation
/*
* CSquareClassFactory::CSquareClassFactory
* CSquareClassFactory::~CSquareClassFactory
*
* Constructor Parameters:
* hWnd HWND of the application.
* hInst HINSTANCE to pass to objects we create.
*/
CSquareClassFactory::CSquareClassFactory(HWND hWnd
, HINSTANCE hInst)
{
m_cRef=0L;
m_hWnd=hWnd;
m_hInst=hInst;
return;
}
CSquareClassFactory::~CSquareClassFactory(void)
{
return;
}
/*
* CSquareClassFactory::QueryInterface
* CSquareClassFactory::AddRef
* CSquareClassFactory::Release
*/
STDMETHODIMP CSquareClassFactory::QueryInterface(REFIID riid
, PPVOID ppv)
{
*ppv=NULL;
if (IID_IUnknown==riid || IID_IClassFactory==riid)
*ppv=this;
if (NULL!=*ppv)
{
((LPUNKNOWN)*ppv)->AddRef();
return NOERROR;
}
return ResultFromScode(E_NOINTERFACE);
}
STDMETHODIMP_(ULONG) CSquareClassFactory::AddRef(void)
{
return ++m_cRef;
}
STDMETHODIMP_(ULONG) CSquareClassFactory::Release(void)
{
if (0!=--m_cRef)
return m_cRef;
delete this;
return 0;
}
/*
* CSquareClassFactory::CreateInstance
* CSquareClassFactory::LockServer
*/
STDMETHODIMP CSquareClassFactory::CreateInstance(LPUNKNOWN pUnkOuter
, REFIID riid, PPVOID ppvObj)
{
PCSquare pObj;
HRESULT hr;
*ppvObj=NULL;
if (NULL!=pUnkOuter)
return ResultFromScode(CLASS_E_NOAGGREGATION);
pObj=new CSquare();
g_cObj++;
hr=ResultFromScode(E_OUTOFMEMORY);
if (NULL!=pObj)
{
if (pObj->Init(m_hWnd, m_hInst))
{
hr=pObj->QueryInterface(riid, ppvObj);
}
}
if (FAILED(hr))
{
if (NULL!=pObj)
delete pObj;
ObjectDestroyed();
}
return hr;
}
STDMETHODIMP CSquareClassFactory::LockServer(BOOL fLock)
{
if (fLock)
g_cLock++;
else
{
g_cLock--;
//Centralizes shutdown in ObjectDestroyed
g_cObj++;
ObjectDestroyed();
}
return NOERROR;
}
//Object implementation
/*
* CSquare::CSquare
* CSquare::~CSquare
*/
CSquare::CSquare(void)
{
m_cRef=0;
m_hWnd=NULL;
m_pITypeInfo=NULL;
m_pIUnkDisp=NULL;
m_cRadius=100.0;
m_dDeclin=0.0;
m_dTheta=0.0;
m_xOrg=(int)(1.5*m_cRadius);
m_yOrg=m_xOrg;
m_crBack=GetSysColor(COLOR_WINDOW);
m_crLinePos=RGB(255, 255, 0);
m_crLineNeg=RGB(255, 0, 0);
m_hPenPos=NULL;
m_hPenNeg=NULL;
m_cx=200;
m_cy=220;
m_xPos=100;
m_yPos=100;
return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -