📄 square.cpp
字号:
}
CSquare::~CSquare(void)
{
ReleaseInterface(m_pITypeInfo);
ReleaseInterface(m_pIUnkDisp);
if (NULL!=m_hPenPos)
DeleteObject(m_hPenPos);
if (NULL!=m_hPenNeg)
DeleteObject(m_hPenNeg);
if (NULL!=m_hWnd)
DestroyWindow(m_hWnd);
return;
}
/*
* CSquare::Init
*
* Purpose:
* Creates the window in which we draw.
*/
BOOL CSquare::Init(HWND hWndOwner, HINSTANCE hInst)
{
ITypeLib *pITypeLib;
HRESULT hr;
//Create both default pens
CreatePens(TRUE, TRUE);
m_hWnd=CreateWindow(SZCLASSSQUARE, SZCLASSSQUARE
, WS_CAPTION | WS_POPUP, m_xPos, m_yPos, m_cx, m_cy
, hWndOwner, NULL, hInst, this);
if (NULL==m_hWnd)
return NULL;
if (FAILED(LoadRegTypeLib(LIBID_SphereSquareLibrary, 1, 0
, LANG_NEUTRAL, &pITypeLib)))
{
if (FAILED(LoadTypeLib(OLETEXT("SQUARE.TLB"), &pITypeLib)))
return FALSE;
}
hr=pITypeLib->GetTypeInfoOfGuid(IID_ISphereSquare, &m_pITypeInfo);
if (FAILED(hr))
{
pITypeLib->Release();
return FALSE;
}
hr=CreateStdDispatch((IUnknown *)this, (ISphereSquare *)this
, m_pITypeInfo, &m_pIUnkDisp);
pITypeLib->Release();
if (FAILED(hr))
return FALSE;
return TRUE;
}
/*
* CSquare::CreatePens
*
* Purpose:
* Creates one or both line color pens
*
* Parameters:
* fPositive BOOL instructing us to create positive line color.
* fNegative BOOL instructing us to create negative line color.
*
* Return Value:
* None
*/
void CSquare::CreatePens(BOOL fPositive, BOOL fNegative)
{
if (fPositive)
{
if (NULL!=m_hPenPos)
DeleteObject(m_hPenPos);
m_hPenPos=CreatePen(PS_SOLID, 1, m_crLinePos);
if (NULL==m_hPenPos)
m_hPenPos=(HPEN)GetStockObject(BLACK_PEN);
}
if (fNegative)
{
if (NULL!=m_hPenNeg)
DeleteObject(m_hPenNeg);
m_hPenNeg=CreatePen(PS_SOLID, 1, m_crLineNeg);
if (NULL==m_hPenNeg)
m_hPenNeg=(HPEN)GetStockObject(BLACK_PEN);
}
return;
}
/*
* CSquare::Draw
*
* Purpose:
* Draws the image of a square rotated according to m_dDeclin
* and m_dTheta with an edge length of m_cRadius.
*
* Parameters:
* hDC HDC on which to draw.
*
* Return Value;
* None
*/
void CSquare::Draw(HDC hDC)
{
POINT pt[5];
int x, y;
double rad;
RECT rc;
HGDIOBJ hGDI;
SetBkColor(hDC, m_crBack);
//Erase the background
SetRect(&rc, 0, 0, m_xOrg*2, m_yOrg*2);
ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
rad=cos(m_dDeclin);
x=(int)(m_cRadius*cos(m_dTheta));
y=(int)(m_cRadius*rad*sin(m_dTheta));
SETPOINT(pt[0], m_xOrg+x, m_yOrg+y);
SETPOINT(pt[2], m_xOrg-x, m_yOrg-y);
x=(int)(m_cRadius*cos(m_dTheta+PI/2));
y=(int)(m_cRadius*rad*sin(m_dTheta+PI/2));
SETPOINT(pt[3], m_xOrg+x, m_yOrg+y);
SETPOINT(pt[1], m_xOrg-x, m_yOrg-y);
pt[4]=pt[0];
/*
* Set the line color according to which side of the square
* is facing out of the screen. We'll draw the positive
* z lines a yellow (default), the negative face red
* (default). Both colors can be changed by the controller.
*
* To determine which side is out, we only have to look at
* the rad value which is cos(m_dDeclin). If positive, then
* we're in the positive range.
*/
hGDI=SelectObject(hDC, (rad > 0) ? m_hPenPos : m_hPenNeg);
Polyline(hDC, pt, sizeof(pt)/sizeof(POINT));
SelectObject(hDC, GetStockObject(WHITE_PEN));
MoveToEx(hDC, m_xOrg, m_yOrg, NULL);
LineTo(hDC, pt[0].x, pt[0].y);
SelectObject(hDC, hGDI);
return;
}
/*
* SquareWndProc
*
* Purpose:
* Window procedure for the Square window.
*/
LRESULT APIENTRY SquareWndProc(HWND hWnd, UINT iMsg, WPARAM wParam
, LPARAM lParam)
{
PCSquare pcp;
PAINTSTRUCT ps;
HDC hDC;
RECT rc;
pcp=(PCSquare)GetWindowLong(hWnd, SQWL_STRUCTURE);
switch (iMsg)
{
case WM_CREATE:
pcp=(PCSquare)((LPCREATESTRUCT)lParam)
->lpCreateParams;
SetWindowLong(hWnd, SQWL_STRUCTURE, (LONG)pcp);
break;
case WM_ERASEBKGND:
hDC=(HDC)wParam;
SetBkColor(hDC, pcp->m_crBack);
GetClientRect(hWnd, &rc);
ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
break;
case WM_PAINT:
hDC=BeginPaint(hWnd, &ps);
pcp->Draw(hDC);
EndPaint(hWnd, &ps);
break;
case WM_CLOSE:
/*
* Eat this or Alt+F4 can destroy the window, even
* though we don't have a system menu.
*/
break;
default:
return DefWindowProc(hWnd, iMsg, wParam, lParam);
}
return 0L;
}
/*
* CSquare::QueryInterface
* CSquare::AddRef
* CSquare::Release
*
* Purpose:
* IUnknown members for CSquare object.
*/
STDMETHODIMP CSquare::QueryInterface(REFIID riid, PPVOID ppv)
{
*ppv=NULL;
if (IID_IUnknown==riid || IID_ISphereSquare==riid)
*ppv=this;
if (IID_IDispatch==riid || DIID_DISphereSquare==riid)
return m_pIUnkDisp->QueryInterface(IID_IDispatch, ppv);
if (NULL!=*ppv)
{
((IUnknown *)*ppv)->AddRef();
return NOERROR;
}
return ResultFromScode(E_NOINTERFACE);
}
STDMETHODIMP_(ULONG) CSquare::AddRef(void)
{
return ++m_cRef;
}
STDMETHODIMP_(ULONG) CSquare::Release(void)
{
if (0L!=--m_cRef)
return m_cRef;
ObjectDestroyed();
delete this;
return 0L;
}
//ISphereSquare members
STDMETHODIMP_(double) CSquare::get_Radius(void)
{
return m_cRadius;
}
STDMETHODIMP_(void) CSquare::put_Radius(double cRadius)
{
//Only use positive radii.
if (cRadius > 0.0)
m_cRadius=cRadius;
//We'll be lazy and not throw exceptions.
return;
}
STDMETHODIMP_(double) CSquare::get_Theta(void)
{
return m_dTheta;
}
STDMETHODIMP_(void) CSquare::put_Theta(double dTheta)
{
//Anything is valid when you do trig
m_dTheta=dTheta;
return;
}
STDMETHODIMP_(double) CSquare::get_Declination(void)
{
return m_dDeclin;
}
STDMETHODIMP_(void) CSquare::put_Declination(double dDeclin)
{
m_dDeclin=dDeclin;
return;
}
STDMETHODIMP_(long) CSquare::get_BackColor(void)
{
return m_crBack;
}
STDMETHODIMP_(void) CSquare::put_BackColor(long crBack)
{
m_crBack=crBack;
return;
}
STDMETHODIMP_(long) CSquare::get_LineColorPositive(void)
{
return m_crLinePos;
}
STDMETHODIMP_(void) CSquare::put_LineColorPositive(long crLinePos)
{
m_crLinePos=crLinePos;
return;
}
STDMETHODIMP_(long) CSquare::get_LineColorNegative(void)
{
return m_crLineNeg;
}
STDMETHODIMP_(void) CSquare::put_LineColorNegative(long crLineNeg)
{
m_crLineNeg=crLineNeg;
return;
}
STDMETHODIMP_(void) CSquare::Draw(void)
{
InvalidateRect(m_hWnd, NULL, FALSE);
UpdateWindow(m_hWnd);
return;
}
STDMETHODIMP_(void) CSquare::SetCenterPoint(int cx, int cy)
{
//No validation...we're deing lazy
m_xOrg=cx;
m_yOrg=cy;
return;
}
STDMETHODIMP_(void) CSquare::ShowWindow(int nCmdShow)
{
::ShowWindow(m_hWnd, nCmdShow);
UpdateWindow(m_hWnd);
return;
}
STDMETHODIMP_(void) CSquare::SetWindowPosition(int xPos, int yPos)
{
m_xPos=xPos;
m_yPos=yPos;
SetWindowPos(m_hWnd, NULL, m_xPos, m_yPos, 0, 0
, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
return;
}
STDMETHODIMP_(void) CSquare::SetWindowSize(int cx, int cy)
{
m_cx=cx;
m_cy=cy;
SetWindowPos(m_hWnd, NULL, 0, 0, m_cx, m_cy
, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -