Click here to Skip to main content
15,886,110 members
Articles / Desktop Programming / MFC

Resource ID Organiser Add-In for Visual C++ 5.0/6.0/.NET

Rate me:
Please Sign up or sign in to vote.
4.98/5 (71 votes)
10 Jan 2005CPOL25 min read 530.7K   12.1K   201  
An application/add-in to organise and renumber resource symbol IDs
//CNGAutoFont class implementation
#include "stdafx.h"
#include "NGAutoFont.h"

CNGAutoFont::CNGAutoFont()
{
	lf.lfHeight=-12;
	lf.lfWidth=0;
	lf.lfEscapement=0;
	lf.lfOrientation=0;
	lf.lfWeight=FW_NORMAL;
	lf.lfItalic=0;
	lf.lfUnderline=0;
	lf.lfStrikeOut=0;
	lf.lfCharSet=ANSI_CHARSET;
	lf.lfOutPrecision=OUT_DEFAULT_PRECIS;
	lf.lfClipPrecision=CLIP_DEFAULT_PRECIS;
	lf.lfQuality=PROOF_QUALITY;
	lf.lfPitchAndFamily=VARIABLE_PITCH | FF_ROMAN;
	strcpy(lf.lfFaceName, "Times New Roman");

	CreateFontIndirect(&lf);

	fontColor=0;
	hDC=NULL;
}

CNGAutoFont::CNGAutoFont(CString facename)
{
	lf.lfHeight=-12;
	lf.lfWidth=0;
	lf.lfEscapement=0;
	lf.lfOrientation=0;
	lf.lfWeight=FW_NORMAL;
	lf.lfItalic=0;
	lf.lfUnderline=0;
	lf.lfStrikeOut=0;
	lf.lfCharSet=ANSI_CHARSET;
	lf.lfOutPrecision=OUT_DEFAULT_PRECIS;
	lf.lfClipPrecision=CLIP_DEFAULT_PRECIS;
	lf.lfQuality=PROOF_QUALITY;
	lf.lfPitchAndFamily=VARIABLE_PITCH | FF_ROMAN;
	strcpy(lf.lfFaceName, (LPCTSTR)facename);

	CreateFontIndirect(&lf);

	fontColor=0;
	hDC=NULL;
}

CNGAutoFont::CNGAutoFont(LOGFONT& logfont)
{
	lf=logfont;
	CreateFontIndirect(&lf);

	fontColor=0;
	hDC=NULL;
}

CNGAutoFont::CNGAutoFont(CFont* pFont)
{
	HFONT hFont=(HFONT)*pFont;
	Attach((HFONT)hFont);

	GetLogFont(&lf);

	fontColor=0;
	hDC=NULL;
}


CNGAutoFont::CNGAutoFont(HFONT hFont)
{
	Attach((HFONT)hFont);

	GetLogFont(&lf);

	fontColor=0;
	hDC=NULL;
}

CNGAutoFont::~CNGAutoFont()
{
}

BOOL CNGAutoFont::CreateStockObject(int nIndex)
{
	//Thanks to Magnus Sigurdsson for this fix.
	ASSERT (nIndex >= OEM_FIXED_FONT && nIndex <=DEFAULT_GUI_FONT);

	BOOL bResult;
	bResult=CFont::CreateStockObject(nIndex);
	if (bResult)
	{
		GetLogFont(&lf);
		DeleteObject();
		CreateFontIndirect(&lf);
	}
	return bResult;
}

LONG CNGAutoFont::SetHeight(LONG height)
{
	LONG l=lf.lfHeight;

	DeleteObject();
	lf.lfHeight=height;
	CreateFontIndirect(&lf);

	return l;
}

LONG CNGAutoFont::SetHeightA(LONG height)
{
	LONG l=lf.lfHeight;

	DeleteObject();
	if (height>0)
		height=0-height;
	lf.lfHeight=height * 4;
	lf.lfHeight=(int)lf.lfHeight/3;
	CreateFontIndirect(&lf);

	return l;
}

LONG CNGAutoFont::SetWidth(LONG width)
{
	LONG l=lf.lfWidth;

	DeleteObject();
	lf.lfWidth=width;
	CreateFontIndirect(&lf);

	return l;
}

LONG CNGAutoFont::SetEscapement(LONG esc)
{
	LONG l=lf.lfEscapement;

	DeleteObject();
	lf.lfEscapement=esc;
	CreateFontIndirect(&lf);

	return l;
}

LONG CNGAutoFont::SetOrientation(LONG or)
{
	LONG l=lf.lfOrientation;

	DeleteObject();
	lf.lfOrientation=or;
	CreateFontIndirect(&lf);

	return l;
}

LONG CNGAutoFont::SetWeight(LONG weight)
{
	LONG l=lf.lfWeight;

	DeleteObject();
	lf.lfWeight=weight;
	CreateFontIndirect(&lf);

	return l;
}

BYTE CNGAutoFont::SetCharset(BYTE charset)
{
	BYTE b=lf.lfCharSet;

	DeleteObject();
	lf.lfCharSet=charset;
	CreateFontIndirect(&lf);

	return b;
}

BYTE CNGAutoFont::SetOutPrecision(BYTE op)
{
	BYTE b=lf.lfOutPrecision;

	DeleteObject();
	lf.lfOutPrecision=op;
	CreateFontIndirect(&lf);

	return b;
}

BYTE CNGAutoFont::SetClipPrecision(BYTE cp)
{
	BYTE b=lf.lfClipPrecision;

	DeleteObject();
	lf.lfClipPrecision=cp;
	CreateFontIndirect(&lf);

	return b;
}

BYTE CNGAutoFont::SetQuality(BYTE qual)
{
	BYTE b=lf.lfQuality;

	DeleteObject();
	lf.lfQuality=qual;
	CreateFontIndirect(&lf);

	return b;
}

BYTE CNGAutoFont::SetPitchAndFamily(BYTE paf)
{
	BYTE b=lf.lfPitchAndFamily;

	DeleteObject();
	lf.lfPitchAndFamily=paf;
	CreateFontIndirect(&lf);

	return b;
}

CString CNGAutoFont::SetFaceName(CString facename)
{
	CString str=lf.lfFaceName;

	DeleteObject();
	strcpy(lf.lfFaceName, (LPCTSTR)facename);
	CreateFontIndirect(&lf);

	return str;
}

LPCTSTR CNGAutoFont::SetFaceName(LPCTSTR facename)
{
	LPCTSTR str=lf.lfFaceName;

	DeleteObject();
	strcpy(lf.lfFaceName, facename);
	CreateFontIndirect(&lf);

	return str;
}

BOOL CNGAutoFont::SetBold(BOOL B)
{
	BOOL b;

	if (B)
		b=SetWeight(FW_BOLD);
	else
		b=SetWeight(FW_NORMAL);

	if (b >= FW_MEDIUM)
		return TRUE;
	else
		return FALSE;
}

BOOL CNGAutoFont::SetItalic(BOOL i)
{
	BOOL b=(BOOL)lf.lfItalic;

	DeleteObject();
	lf.lfItalic=(TCHAR)i;
	CreateFontIndirect(&lf);

	return b;
}

BOOL CNGAutoFont::SetUnderline(BOOL u)
{
	BOOL b=(BOOL)lf.lfUnderline;

	DeleteObject();
	lf.lfUnderline=(TCHAR)u;
	CreateFontIndirect(&lf);

	return b;
}

BOOL CNGAutoFont::SetStrikeOut(BOOL s)
{
	BOOL b=(BOOL)lf.lfStrikeOut;

	DeleteObject();
	lf.lfStrikeOut=(TCHAR)s;
	CreateFontIndirect(&lf);

	return b;
}

void CNGAutoFont::SetLogFont(LOGFONT& logfont)
{
	lf=logfont;
	DeleteObject();
	CreateFontIndirect(&lf);
}

LONG CNGAutoFont::GetHeight()
{
	return lf.lfHeight;
}

LONG CNGAutoFont::GetWidth()
{
	return lf.lfWidth;
}

LONG CNGAutoFont::GetEscapement()
{
	return lf.lfEscapement;
}

LONG CNGAutoFont::GetOrientation()
{
	return lf.lfEscapement;
}

LONG CNGAutoFont::GetWeight()
{
	return lf.lfWeight;
}

BYTE CNGAutoFont::GetCharset()
{
	return lf.lfCharSet;
}

BYTE CNGAutoFont::GetOutPrecision()
{
	return lf.lfOutPrecision;
}

BYTE CNGAutoFont::GetClipPrecision()
{
	return lf.lfClipPrecision;
}

BYTE CNGAutoFont::GetQuality()
{
	return lf.lfQuality;
}

BYTE CNGAutoFont::GetPitchAndFamily()
{
	return lf.lfPitchAndFamily;
}

LPCTSTR CNGAutoFont::GetFaceName()
{
	return lf.lfFaceName;
}

BOOL CNGAutoFont::GetBold()
{
	return lf.lfWeight >= FW_MEDIUM ? TRUE : FALSE;
}

BOOL CNGAutoFont::GetItalic()
{
	return (BOOL)lf.lfItalic;
}

BOOL CNGAutoFont::GetUnderline()
{
	return (BOOL)lf.lfUnderline;
}

BOOL CNGAutoFont::GetStrikeOut()
{
	return (BOOL)lf.lfStrikeOut;
}

CString CNGAutoFont::ContractFont()
{
	CString str, color;

	str.Format("%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%s",
		lf.lfHeight,
		lf.lfWidth,
		lf.lfEscapement,
		lf.lfOrientation,
		lf.lfWeight,
		lf.lfItalic,
		lf.lfUnderline,
		lf.lfStrikeOut,
		lf.lfCharSet,
		lf.lfOutPrecision,
		lf.lfClipPrecision,
		lf.lfQuality,
		lf.lfPitchAndFamily,
		lf.lfFaceName);
	color.Format("%u", fontColor);
	str+=",";
	str+=color;

	return str;
}

void CNGAutoFont::ExtractFont(CString& str)
{
	lf.lfHeight=atol((LPCTSTR)GetToken(str, ","));
	lf.lfWidth=atol((LPCTSTR)GetToken(str, ","));
	lf.lfEscapement=atol((LPCTSTR)GetToken(str, ","));
	lf.lfOrientation=atol((LPCTSTR)GetToken(str, ","));
	lf.lfWeight=atol((LPCTSTR)GetToken(str, ","));
	lf.lfItalic=(TCHAR)atoi((LPCTSTR)GetToken(str, ","));
	lf.lfUnderline=(TCHAR)atoi((LPCTSTR)GetToken(str, ","));
	lf.lfStrikeOut=(TCHAR)atoi((LPCTSTR)GetToken(str, ","));
	lf.lfCharSet=(TCHAR)atoi((LPCTSTR)GetToken(str, ","));
	lf.lfOutPrecision=(TCHAR)atoi((LPCTSTR)GetToken(str, ","));
	lf.lfClipPrecision=(TCHAR)atoi((LPCTSTR)GetToken(str, ","));
	lf.lfQuality=(TCHAR)atoi((LPCTSTR)GetToken(str, ","));
	lf.lfPitchAndFamily=(TCHAR)atoi((LPCTSTR)GetToken(str, ","));
	strcpy(lf.lfFaceName, (LPCTSTR)GetToken(str, ","));

	DeleteObject();
	CreateFontIndirect(&lf);

	fontColor=atol((LPCTSTR)str);
}

CString CNGAutoFont::GetToken(CString& str, LPCTSTR c)
{
	int pos;
	CString token;

	pos=str.Find(c);
	token=str.Left(pos);
	str=str.Mid(pos+1);

	return token;
}

void CNGAutoFont::GetFontFromDialog(CFont *f, DWORD *color,
				CDC *pPrinterDC, CWnd *pParentWnd)
{
	LOGFONT tlf;
	if (f==NULL)
		tlf=lf;
	else
		f->GetLogFont(&tlf);

	CFontDialog dlg(&tlf, CF_EFFECTS | CF_SCREENFONTS,
		pPrinterDC, pParentWnd);
	dlg.m_cf.rgbColors=fontColor;
	
	if (dlg.DoModal()==IDOK)
	{
		dlg.GetCurrentFont(&lf);
		DeleteObject();
		CreateFontIndirect(&lf);
		f=(CFont *)this;
		color=&dlg.m_cf.rgbColors;
		SetFontColor(dlg.m_cf.rgbColors);
	}
}

void CNGAutoFont::SetFontColor(COLORREF color)
{
	fontColor=color;
	if (hDC!=NULL)
		::SetTextColor(hDC, color);
}

COLORREF CNGAutoFont::GetFontColor()
{
	return fontColor;
}

void CNGAutoFont::SetDC(HDC dc)
{
	hDC=dc;
}

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
Founder Riverblade Limited
United Kingdom United Kingdom
I haven't always written software for a living. When I graduated from Surrey University in 1989, it was with an Electronic Engineering degree, but unfortunately that never really gave me the opportunity to do anything particularly interesting (with the possible exception of designing Darth Vader's Codpiece * for the UK Army in 1990).
    * Also known as the Standard Army Bootswitch. But that's another story...
Since the opportunity arose to lead a software team developing C++ software for Avionic Test Systems in 1996, I've not looked back. More recently I've been involved in the development of subsea acoustic navigation systems, digital TV broadcast systems, port security/tracking systems, and most recently software development tools with my own company, Riverblade Ltd.

One of my personal specialities is IDE plug-in development. ResOrg was my first attempt at a plug-in, but my day to day work is with Visual Lint, an interactive code analysis tool environment with works within the Visual Studio and Eclipse IDEs or on build servers.

I love lots of things, but particularly music, photography and anything connected with history or engineering. I despise ignorant, intolerant and obstructive people - and it shows...I can be a bolshy cow if you wind me up the wrong way...Laugh | :laugh:

I'm currently based 15 minutes walk from the beach in Bournemouth on the south coast of England. Since I moved here I've grown to love the place - even if it is full of grockles in Summer!

Comments and Discussions