|
LINES.CPP (LINES OLE Sample)
/*************************************************************************
-
- OLE Automation Lines object
-
- lines.cpp
-
- CLines collection implementation
-
- Written by Microsoft Product Support Services, Windows Developer Support
- (c) Copyright Microsoft Corp. 1993 All Rights Reserved
-
- ***********************************************************************/
#include <windows.h>
#include <windowsx.h>
#ifdef WIN16
#include <ole2.h>
#include <compobj.h>
#include <dispatch.h>
#include <variant.h>
#include <olenls.h>
#endif
#include "lines.h"
/*
* CLines::Create
*
* Purpose:
* Creates an instance of a Lines collection object and initializes it.
*
* Parameters:
* lMaxSize Maximum number of items that can added to collection.
* lLBound Lower bound of index of collection.
* pPane Pointer to pane that contains this collection. Required because
the pane coordinates
* the lines and points collection.
* ppLines Returns Lines collection object.
*
* Return Value:
* HRESULT
*
*/
HRESULT
CLines::Create(ULONG lMaxSize, long lLBound, CPane FAR* pPane, CLines FAR*
FAR* ppLines)
{
HRESULT hr;
CLines FAR* pLines = NULL;
SAFEARRAYBOUND sabound[1];
*ppLines = NULL;
// Create new collection
pLines = new CLines();
if (pLines == NULL)
goto error;
pLines->m_cMax = lMaxSize;
pLines->m_cElements = 0;
pLines->m_lLBound = lLBound;
pLines->m_pPane = pPane;
// Load type information for the Lines collection from type library.
hr = LoadTypeInfo(&pLines->m_ptinfo, IID_ILines);
if (FAILED(hr))
goto error;
// Create a safe array of variants which is used to implement the
collection.
sabound[0].cElements = lMaxSize;
sabound[0].lLbound = lLBound;
pLines->m_psa = SafeArrayCreate(VT_VARIANT, 1, sabound);
if (pLines->m_psa == NULL)
{
hr = ResultFromScode(E_OUTOFMEMORY);
goto error;
}
*ppLines = pLines;
return NOERROR;
error:
if (pLines == NULL)
return ResultFromScode(E_OUTOFMEMORY);
if (pLines->m_ptinfo)
pLines->m_ptinfo->Release();
if (pLines->m_psa)
SafeArrayDestroy(pLines->m_psa);
pLines->m_psa = NULL;
pLines->m_ptinfo = NULL;
delete pLines;
return hr;
}
/*
* CLines::CLines
*
* Purpose:
* Constructor for CLines object. Initializes members to NULL.
*
*/
#pragma warning (disable : 4355)
CLines::CLines() : m_SupportErrorInfo(this, IID_ILines)
#pragma warning (default : 4355)
{
m_cRef = 0;
m_psa = NULL;
m_ptinfo = NULL;
}
/*
* CLines::~CLines
*
* Purpose:
* Destructor for CLines object.
*
*/
CLines::~CLines()
{
if (m_ptinfo) m_ptinfo->Release();
if (m_psa) SafeArrayDestroy(m_psa);
}
/*
* CLines::QueryInterface, AddRef, Release
*
* Purpose:
* Implements IUnknown::QueryInterface, AddRef, Release
*
*/
STDMETHODIMP
CLines::QueryInterface(REFIID iid, void FAR* FAR* ppv)
{
*ppv = NULL;
if (iid == IID_IUnknown || iid == IID_ILines || iid == IID_IDispatch)
*ppv = this;
else if (iid == IID_ISupportErrorInfo)
*ppv = &m_SupportErrorInfo;
else return ResultFromScode(E_NOINTERFACE);
AddRef();
return NOERROR;
}
STDMETHODIMP_(ULONG)
CLines::AddRef(void)
{
#ifdef _DEBUG
TCHAR ach[50];
wsprintf(ach, TEXT("Ref = %ld, Lines\r\n"), m_cRef+1);
OutputDebugString(ach);
#endif
return ++m_cRef;
}
STDMETHODIMP_(ULONG)
CLines::Release(void)
{
#ifdef _DEBUG
TCHAR ach[50];
wsprintf(ach, TEXT("Ref = %ld, Lines\r\n"), m_cRef-1);
OutputDebugString(ach);
#endif
if(--m_cRef == 0)
{
delete this;
return 0;
}
return m_cRef;
}
/*
* CLines::GetTypeInfoCount
*
* Purpose:
* Implements IDispatch::GetTypeInfoCount.
*
*/
STDMETHODIMP
CLines::GetTypeInfoCount(UINT FAR* pctinfo)
{
*pctinfo = 1;
return NOERROR;
}
/*
* CLines::GetTypeInfo
*
* Purpose:
* Implements IDispatch::GetTypeInfo.
*
*/
STDMETHODIMP
CLines::GetTypeInfo(
UINT itinfo,
LCID lcid,
ITypeInfo FAR* FAR* pptinfo)
{
*pptinfo = NULL;
if(itinfo != 0)
return ResultFromScode(DISP_E_BADINDEX);
m_ptinfo->AddRef();
*pptinfo = m_ptinfo;
return NOERROR;
}
/*
* CLines::GetIDsOfNames
*
* Purpose:
* Implements IDispatch::GetIDsOfNames. The standard implementation,
DispGetIDsOfNames,
* is used.
*
*/
STDMETHODIMP
CLines::GetIDsOfNames(
REFIID riid,
OLECHAR FAR* FAR* rgszNames,
UINT cNames,
LCID lcid,
DISPID FAR* rgdispid)
{
return DispGetIDsOfNames(m_ptinfo, rgszNames, cNames, rgdispid);
}
/*
* CLines::Invoke
*
* Purpose:
* Implements IDispatch::Invoke. The standard implementation, DispInvoke,
* is used. Properties and methods exposed by the lines collection object will
* set m_bRaiseException to raise an exception.
*
*/
STDMETHODIMP
CLines::Invoke(
DISPID dispidMember,
REFIID riid,
LCID lcid,
WORD wFlags,
DISPPARAMS FAR* pdispparams,
VARIANT FAR* pvarResult,
EXCEPINFO FAR* pexcepinfo,
UINT FAR* puArgErr)
{
HRESULT hr;
m_bRaiseException = FALSE;
hr = DispInvoke(
this, m_ptinfo,
dispidMember, wFlags, pdispparams,
pvarResult, pexcepinfo, puArgErr);
if (m_bRaiseException)
{
if (NULL != pexcepinfo)
_fmemcpy(pexcepinfo, &m_excepinfo, sizeof(EXCEPINFO));
return ResultFromScode(DISP_E_EXCEPTION);
}
else return hr;
}
/*
*
* Properties and methods exposed through automation.
*
*/
/*
* CLines::Add
*
* Purpose:
* Adds a line to the lines collection.
*
* Parameters:
* pLineNew IDispatch of line to be added to collection.
*
*/
STDMETHODIMP
CLines::Add(ILine FAR* pLineNew)
{
HRESULT hr;
LONG l;
CLine FAR* pLine = NULL;
VARIANT v;
HDC hdc;
// Is the collection full?
if (m_cElements == m_cMax)
return RaiseException(IDS_CollectionFull);
hr = pLineNew->QueryInterface(IID_ILine, (void FAR* FAR*)&pLine);
if (FAILED(hr))
return RaiseException(IDS_LineFromOtherInstance);
// Add end points of line to the pane's point collection
if (FALSE == pLine->AddEndPointsToPane(m_pPane))
{hr = RaiseException(IDS_CantAddEndPoints); goto error;}
// Add new line to collection
l = m_lLBound+m_cElements;
VariantInit(&v);
V_VT(&v) = VT_DISPATCH;
V_DISPATCH(&v) = pLineNew;
hr = SafeArrayPutElement(m_psa, &l, &v);
if (FAILED(hr))
{hr = RaiseException(IDS_Unexpected); goto error;}
m_cElements++;
// Draw the new line
hdc = m_pPane->GetDC();
pLine->Draw(hdc);
m_pPane->ReleaseDC(hdc);
pLine->Release();
return NOERROR;
error:
if (pLine)
pLine->Release();
return hr;
}
/*
* CLines::get_Count
*
* Purpose:
* Returns number of items in collection.
*
*/
STDMETHODIMP
CLines::get_Count(long FAR* lCount)
{
*lCount = m_cElements;
return NOERROR;
}
/*
* CLines::get_Item
*
* Purpose:
* Retrieves item from collection, given an index.
*
* Parameters:
* lIndex Index of item to be retrieved.
* ppLine Returns IDispatch of item retrieved from collection.
*
*/
STDMETHODIMP
CLines::get_Item(long lIndex, ILine FAR* FAR* ppLine)
{
HRESULT hr;
VARIANT v;
// Check if index is within range
if (lIndex < m_lLBound || lIndex >= (long)(m_lLBound+m_cElements))
return RaiseException(IDS_InvalidIndex);
// Retrieve and return item. Note that SafeArrayGetElement AddRefs, so an
additional AddRef
// is not required.
VariantInit(&v);
hr = SafeArrayGetElement(m_psa, &lIndex, &v);
if (FAILED(hr))
return RaiseException(IDS_Unexpected);
*ppLine = (ILine FAR*) V_DISPATCH(&v);
return NOERROR;
}
/*
* CLines::get_NewEnum
*
* Purpose:
* Returns an enumerator (IEnumVARIANT) for the items curently in the
collection.
* The NewEnum property is restricted and so is invisible to users of an
* automation controller's scripting language. Automation controllers that
support
* a 'For Each' statement to iterate through the elements of a collection
will use
* the enumerator returned by NewEnum. The enumerator creates a snapshot of
the
* the current state of the collection.
*
*/
STDMETHODIMP
CLines::get__NewEnum(IUnknown FAR* FAR* ppunkEnum)
{
CEnumVariant FAR* penum = NULL;;
HRESULT hr;
*ppunkEnum = NULL;
// Create new enumerator for items currently in collection and QI for
IUnknown
hr = CEnumVariant::Create(m_psa, m_cElements, &penum);
if (FAILED(hr))
{hr = RaiseException(IDS_OutOfMemory); goto error;}
hr = penum->QueryInterface(IID_IUnknown, (VOID FAR* FAR*)ppunkEnum);
if (FAILED(hr))
{hr = RaiseException(IDS_Unexpected); goto error;}
return NOERROR;
error:
if (penum)
delete penum;
return hr;
}
/*
* CLines::Remove
*
* Purpose:
* Removes specified item from collection.
*
* Parameters:
* lIndex Index of item to be removed.
*
*/
STDMETHODIMP
CLines::Remove(long lIndex)
{
HRESULT hr;
long l;
VARIANT HUGEP *pvar;
CLine FAR* pLine = NULL;
RECT rc;
// Check if integer index is within range.
if (lIndex < m_lLBound || lIndex >= (long)(m_lLBound+m_cElements))
return RaiseException(IDS_InvalidIndex);
hr = SafeArrayAccessData(m_psa, (void HUGEP* FAR*)&pvar);
if (FAILED(hr))
return RaiseException(IDS_Unexpected);
V_DISPATCH(&pvar[lIndex-m_lLBound])->QueryInterface(IID_ILine, (void FAR*
FAR*)&pLine);
// Ask the pane to invalidate the area where the line is drawn.
pLine->GetInvalidateRect(&rc);
m_pPane->InvalidateRect(&rc);
// Remove end points of line from the pane's point collection.
pLine->RemoveEndPointsFromPane(m_pPane);
// Remove Line
V_DISPATCH(&pvar[lIndex-m_lLBound])->Release();
// Move up the array elements, after the element to be removed.
for (l=lIndex-m_lLBound; l<(long)(m_cElements-1); l++)
pvar[l] = pvar[l+1];
// Remove last element.
V_VT(&pvar[l]) = VT_EMPTY;
pLine->Release();
hr = SafeArrayUnaccessData(m_psa);
if (FAILED(hr))
return RaiseException(IDS_Unexpected);
m_cElements--;
m_pPane->Update(); // Ask the pane to repaint invalidated areas caused
by removal of line.
return NOERROR;
}
/*
*
* The following methods are not exposed through Automation
*
*/
/*
* CLines::Draw
*
* Purpose:
* Draws all lines in collection.
*
*/
STDMETHODIMP_(void)
CLines::Draw(HDC hdc)
{
HRESULT hr;
long l;
CLine FAR* pLine = NULL;
VARIANT HUGEP *pvar;
// Draw each line in the Lines collection
hr = SafeArrayAccessData(m_psa, (void HUGEP* FAR*)&pvar);
if (FAILED(hr))
return;
for (l=0; l<(long)m_cElements; l++)
{
hr = V_DISPATCH(&pvar[l])->QueryInterface(IID_ILine, (void FAR*
FAR*)&pLine);
if (FAILED(hr))
continue;
pLine->Draw(hdc);
pLine->Release();
}
hr = SafeArrayUnaccessData(m_psa);
if (FAILED(hr))
return;
return;
}
/*
* CLines::Clear
*
* Purpose:
* Removes all items from collection.
*
*/
STDMETHODIMP_(void)
CLines::Clear(void)
{
SafeArrayDestroyData(m_psa);
SafeArrayAllocData(m_psa);
m_cElements = 0;
}
/*
* CLines::RaiseException
*
* Purpose:
* Fills the EXCEPINFO structure and signal IDispatch::Invoke to return
DISP_E_EXCEPTION.
* Sets ErrorInfo object for vtable-binding controllers.
*
*/
STDMETHODIMP
CLines::RaiseException(int nID)
{
extern SCODE g_scodes[];
TCHAR szError[STR_LEN];
ICreateErrorInfo *pcerrinfo;
IErrorInfo *perrinfo;
HRESULT hr;
_fmemset(&m_excepinfo, 0, sizeof(EXCEPINFO));
m_excepinfo.wCode = nID;
if (LoadString(g_pApplication->m_hinst, nID, szError, sizeof(szError)))
m_excepinfo.bstrDescription = SysAllocString(TO_OLE_STRING(szError));
m_excepinfo.bstrSource = SysAllocString(g_pApplication->m_bstrProgID);
m_bRaiseException = TRUE;
// Set ErrInfo object so that vtable binding containers can get
// rich error information.
hr = CreateErrorInfo(&pcerrinfo);
if (SUCCEEDED(hr))
{
pcerrinfo->SetGUID(IID_ILines);
if (m_excepinfo.bstrSource)
pcerrinfo->SetSource(m_excepinfo.bstrSource);
if (m_excepinfo.bstrDescription)
pcerrinfo->SetDescription(m_excepinfo.bstrDescription);
hr = pcerrinfo->QueryInterface(IID_IErrorInfo, (LPVOID FAR*) &perrinfo);
if (SUCCEEDED(hr))
{
SetErrorInfo(0, perrinfo);
perrinfo->Release();
}
pcerrinfo->Release();
}
return ResultFromScode(g_scodes[nID-1001]);
}
| Last news from Greatis Software |
 |
|
Nostalgia .Net |
|
.Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes. More » |
| Recommended software for developers |
 |
|
Ultimate Pack |
|
Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price. More » |
 |
|
Form Designer .Net |
|
Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro! More » |
 |
|
Print Suite .Net |
|
Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available More » |
 |
|
Gradient Controls .Net |
|
Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available More » |
 |
|
Greatis iGrid |
|
iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors. More » |
All the contacts and projectsDmitry Vasiliev (just.dmitry)
Related LinksSoftware for Visual Studio .NET developers Software for Delphi and C++ Builder developers Software for Visual Basic 6 developers Delphi Tips&Tricks MegaDetailed.NET More Online Helps Win32 Programmer's Reference Win32 Multimedia Programmer's Reference OLE Programmer's Reference Microsoft Windows Pen API Programmer's Reference Microsoft Windows Sockets 2 Reference Microsoft Windows Telephony API (TAPI) Programmer's Reference Unix Manual Pages
|