Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / WTL

Form Designer

26 Jul 2021CPOL24 min read 349.8K   82.5K   230  
Component for adding scriptable forms capabilities to an application.
// InsertItemDlg.cpp : Implementation of CInsertItemDlg
//
// Author : David Shepherd
//			Copyright (c) 2002, DaeDoe-Software
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "InsertItemDlg.h"

/////////////////////////////////////////////////////////////////////////////
// CInsertableItem

CInsertableItem::CInsertableItem()
{
	// initialise everything
	m_ClassId=CLSID_NULL;
}

CInsertableItem::~CInsertableItem()
{
	// clean up
}

/////////////////////////////////////////////////////////////////////////////
// CInsertableItem predicates

bool IsItemNameLess(const CInsertableItem &r1,const CInsertableItem &r2)
{
	// get the name for item 1
	std::wstring Name1=r1.m_Name;
	// get the name for item 2
	std::wstring Name2=r2.m_Name;
	// compare the names
	return (_wcsicmp(Name1.c_str(),Name2.c_str()) < 0) ? true : false;
}

/////////////////////////////////////////////////////////////////////////////
// CInsertItemDlg

CInsertItemDlg::CInsertItemDlg()
{
	// initialise everything
}

CInsertItemDlg::~CInsertItemDlg()
{
	// clean up
}

LRESULT CInsertItemDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
TRY
	USES_CONVERSION;

	// wrap all dialog controls
	m_ctrlItemList.Attach(GetDlgItem(IDC_ITEM_LIST));
	// todo : add error checking here if GetDlgItem() fails
	// obtain the ICatInformation interface on the component category manager
	CComPtr<ICatInformation> spCatInformation;
	if(!SUCCEEDED(spCatInformation.
		CoCreateInstance(CLSID_StdComponentCategoriesMgr)))
	{
		throw std::exception();
	}
	// obtain an enumerator over the control classes
	CATID CategoryId=CATID_Control;
	CComPtr<IEnumCLSID> spEnumCLSID;
	if(!SUCCEEDED(spCatInformation->
		EnumClassesOfCategories(1,&CategoryId,0,NULL,&spEnumCLSID)))
	{
		throw std::exception();
	}
	// iterate over the control classes
	CLSID ClassId=CLSID_NULL;
	while(spEnumCLSID->Next(1,&ClassId,NULL)==S_OK)
	{
		// get the class name
		CAutoCoTaskMem<WCHAR> Name;
		if(!SUCCEEDED(OleRegGetUserType(ClassId,USERCLASSTYPE_FULL,&Name)))
		{
			continue;	// ignore this class
		}
		// get the class prog id
		CAutoCoTaskMem<WCHAR> ProgId;
		if(!SUCCEEDED(ProgIDFromCLSID(ClassId,&ProgId)))
		{
			continue;	// ignore this class
		}
		// create the insertable item
		CInsertableItem InsertableItem;
		InsertableItem.m_Name=Name;
		InsertableItem.m_ClassId=ClassId;
		InsertableItem.m_ProgId=ProgId;
		// update the vector
		m_InsertableItems.push_back(InsertableItem);
	}	
	// add all items to the item list
	std::sort(m_InsertableItems.begin(),m_InsertableItems.end(),IsItemNameLess);
	for(long l=0; l<(long)m_InsertableItems.size(); l++)
	{
		LRESULT Result=
			m_ctrlItemList.AddString(W2CT(m_InsertableItems[l].m_Name.c_str()));
		if(Result==LB_ERR or Result==LB_ERRSPACE)
		{
			throw std::exception();
		}
	}
	// center the dialog
	(void)CenterWindow();
	// enable horizontal scrolling
	// todo : calculate the horizontal extent correctly
	m_ctrlItemList.SetHorizontalExtent(512);
CATCH_ALL
	return TRUE;
}

LRESULT CInsertItemDlg::OnOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
TRY
	// shut down the dialog
	(void)EndDialog(wID);
CATCH_ALL
	return 0;
}

LRESULT CInsertItemDlg::OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
TRY
	// shut down the dialog
	(void)EndDialog(wID);
CATCH_ALL
	return 0;
}

LRESULT CInsertItemDlg::OnSelChangeItemList(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
TRY
	// get the selected item
	DWORD ItemIndex=m_ctrlItemList.GetCurSel();
	if(ItemIndex==LB_ERR)
	{
		throw std::exception();
	}
	// get the item prog id
	ATLASSERT(ItemIndex >=0 and ItemIndex < m_InsertableItems.size());
	m_ProgId=m_InsertableItems[ItemIndex].m_ProgId;
CATCH_ALL
	return 0;
}

LRESULT CInsertItemDlg::OnDblClkItemList(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
TRY
	// shut down the dialog
	(void)EndDialog(IDOK);
CATCH_ALL
	return 0;
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions