Click here to Skip to main content
15,885,757 members
Articles / Desktop Programming / MFC

Property list ActiveX control

Rate me:
Please Sign up or sign in to vote.
3.88/5 (23 votes)
4 Nov 20043 min read 190K   7K   58  
Property list similar to VB.NET, implemented as ActiveX control.
// FunctionItem.cpp: implementation of the CFunctionItem class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PropList.h"
#include "FunctionItem.h"
#include "MyList.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

// Utility
#define GETICONRESOURCE16(i) (HICON)LoadImage(AfxGetResourceHandle( ),MAKEINTRESOURCE(i),IMAGE_ICON,16,16,0)
#define GETICONRESOURCE32(i) (HICON)LoadImage(AfxGetResourceHandle( ),MAKEINTRESOURCE(i),IMAGE_ICON,32,32,0)


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CFunctionItem::CFunctionItem()
{
	type = TYPE_FUNCTION;
}

CFunctionItem::~CFunctionItem()
{

}

void CFunctionItem::CreateControl(CWnd *pParent,CFont* pFont, UINT id)
{
	CListItem::CreateControl(pParent,pFont,id);
	m_button.Create("",WS_CHILD|BS_PUSHBUTTON|BS_ICON|BS_FLAT,CRect(0,0,10,10),pParent,id);
	m_button.SetIcon(GETICONRESOURCE16(IDI_SCRIPT));

	m_scripter.CreateInstance(__uuidof(ScriptControl));
	_bstr_t bstrLanguage(L"VBScript");
	m_scripter->put_Language(bstrLanguage);
}

void CFunctionItem::Activate(CRect rect)
{
	if (!bEnabled)
		return;
}

void CFunctionItem::DrawValue(CDC *pDc, CRect rect)
{
	CListItem::DrawValue(pDc,rect);
	rect.right -= 1;
	rect.bottom -= 1;
	CRect rcButton = rect;
	rcButton.left = rect.right - rect.Height()-4;
	m_button.MoveWindow(rcButton);
	m_button.SetButtonStyle(m_button.GetButtonStyle() & ~BS_DEFPUSHBUTTON);
	m_button.ShowWindow(SW_SHOW);
	m_button.Invalidate();
	rect.right -= rect.Height()+8;
	rect.left+= 5;
	rect.right-= 2;
	pDc->DrawText(GetString(),rect,DT_PATH_ELLIPSIS|DT_END_ELLIPSIS|DT_SINGLELINE|DT_VCENTER);
}

CString CFunctionItem::GetString()
{
	_variant_t var;
	CString str;
	CString val = value.bstrVal;
	
	// Replace variable names with values
	CMyList* pList = (CMyList*)pOwner;
	if (!pList)
		return "";
	val = pList->ReplaceValues(val);
	try  // Make sure exception handling is turned on.
	{
		
		var = m_scripter->Eval(_bstr_t(val));
	}
	catch(_com_error e)
	{
		IScriptErrorPtr scriptError(m_scripter->GetError());
		CString strDescription = (char*)scriptError->GetDescription();
		
		scriptError->Clear();

		str.Format("Error: %s",strDescription);
		return str;
	}
	switch (var.vt)
	{
	case VT_I2:
		str.Format("%d",var.iVal);
		break;
	case VT_I4:
		str.Format("%d",var.lVal);
		break;
	case VT_R4:
		str.Format("%.2f",var.fltVal);
		break;
	case VT_R8:
		str.Format("%.2f",var.dblVal);
		break;
	case VT_BSTR:
		str = var.bstrVal;
		break;
	}
	return str;
}

void CFunctionItem::setIsOpened(BOOL val)
{
	CListItem::setIsOpened(val);
	if (IsWindow(m_button.m_hWnd))
	{
		if (val)
			m_button.ShowWindow(SW_SHOW);
		else
			m_button.ShowWindow(SW_HIDE);
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) RDV Systems
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions