Click here to Skip to main content
15,884,836 members
Articles / Desktop Programming / WTL

Form Designer

26 Jul 2021CPOL24 min read 351.2K   82.5K   230  
Component for adding scriptable forms capabilities to an application.
// PropertyHelpers.cpp
//
// Author : David Shepherd
//			Copyright (c) 2003, DaeDoe-Software
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PropertyHelpers.h"

// default font name
#define DEFAULT_FONT_NAME	L"MS Sans Serif"
// default font size
#define DEFAULT_FONT_SIZE	8

CComPtr<IFontDisp> CreateDefaultFont()
{
	// create the default font
	FONTDESC FontDesc;
	ZeroMemory(&FontDesc,sizeof(FONTDESC));
	CY cy={ DEFAULT_FONT_SIZE*10000, 0 };
	FontDesc.cbSizeofstruct=sizeof(FONTDESC);
	FontDesc.lpstrName=DEFAULT_FONT_NAME;
	FontDesc.cySize=cy;
	FontDesc.sWeight=FW_NORMAL;
	FontDesc.sCharset=DEFAULT_CHARSET;
	FontDesc.fItalic=FALSE;
	FontDesc.fUnderline=FALSE;
	FontDesc.fStrikethrough=FALSE;
	CComPtr<IFontDisp> spFontDisp;
	if(!SUCCEEDED(OleCreateFontIndirect(
		&FontDesc,IID_IFontDisp,(void**)&spFontDisp)))
	{
		throw std::exception();
	}
	return spFontDisp;
}

CComPtr<IFontDisp> CloneFont(const CComPtr<IFontDisp> &spFontDisp)
{
	// clone the passed font
	if(spFontDisp==NULL)
	{
		throw std::exception();
	}
	CComQIPtr<IFont> spFont(spFontDisp);
	if(spFont==NULL)
	{
		throw std::exception();
	}
	CComPtr<IFont> spFontClone;
	if(!SUCCEEDED(spFont->Clone(&spFontClone)))
	{
		throw std::exception();
	}
	CComQIPtr<IFontDisp> spFontDispClone(spFontClone);
	if(spFontDispClone==NULL)
	{
		throw std::exception();
	}
	return spFontDispClone;
}

CComPtr<IPictureDisp> CreateDefaultPicture()
{
	// create the default picture
	PICTDESC PictDesc;
	ZeroMemory(&PictDesc,sizeof(PICTDESC));
	PictDesc.cbSizeofstruct=sizeof(PICTDESC);
	PictDesc.picType=PICTYPE_NONE;
	CComPtr<IPictureDisp> spPictureDisp;
	if(!SUCCEEDED(OleCreatePictureIndirect(
		&PictDesc,IID_IPictureDisp,TRUE,(void**)&spPictureDisp)))
	{
		throw std::exception();
	}
	return spPictureDisp;
}

CComPtr<IPictureDisp> ClonePicture(const CComPtr<IPictureDisp> &spPictureDisp)
{
	// clone the passed picture
	if(spPictureDisp==NULL)
	{
		throw std::exception();
	}
	CComPtr<IStream> spStream;
	if(!SUCCEEDED(CreateStreamOnHGlobal(NULL,TRUE,&spStream)))
	{
		throw std::exception();
	}
	CComQIPtr<IPersistStream> spPersistStream(spPictureDisp);
	if(spPersistStream==NULL)
	{
		throw std::exception();
	}
	if(!SUCCEEDED(spPersistStream->Save(spStream,FALSE)))
	{
		throw std::exception();
	}
	LARGE_INTEGER Offset;
	Offset.QuadPart=0;
	if(!SUCCEEDED(spStream->Seek(Offset,STREAM_SEEK_SET,NULL)))
	{
		throw std::exception();
	}
	CComPtr<IPictureDisp> spPictureDispClone;
	if(!SUCCEEDED(OleLoadPicture(
		spStream,0,FALSE,IID_IPictureDisp,(void**)&spPictureDispClone)))
	{
		throw std::exception();
	}
	return spPictureDispClone;
}

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