Click here to Skip to main content
15,886,714 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
////////////////////////////////////////////////////////////////
// CNGWindowPlacement Copyright 1996 Microsoft Systems Journal.
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.

#include "StdAfx.h"
#include "NGWindowPlacement.h"


/////////////////////////////////////////////////////////////////////////////
//	Helpers for saving/restoring window state
//	(taken from the MFC SuperPad sample)

static TCHAR szSection[]	= _T("Settings");
static TCHAR szWindowPos[]	= _T("WindowPos");
static TCHAR szFormat[]		= _T("%u,%u,%d,%d,%d,%d,%d,%d,%d,%d");


NGLIB_EXT_API BOOL ReadWindowPlacement(LPWINDOWPLACEMENT pwp)
{
	CString strBuffer = AfxGetApp()->GetProfileString(szSection, szWindowPos);
	if (strBuffer.IsEmpty())
		return FALSE;

	WINDOWPLACEMENT wp;
	int nRead = sscanf(strBuffer, szFormat,
		&wp.flags, &wp.showCmd,
		&wp.ptMinPosition.x, &wp.ptMinPosition.y,
		&wp.ptMaxPosition.x, &wp.ptMaxPosition.y,
		&wp.rcNormalPosition.left, &wp.rcNormalPosition.top,
		&wp.rcNormalPosition.right, &wp.rcNormalPosition.bottom);

	if (nRead != 10)
		return FALSE;

	wp.length = sizeof wp;
	*pwp = wp;
	return TRUE;
}


// Write a window placement to settings section of app's ini file
NGLIB_EXT_API void WriteWindowPlacement(LPWINDOWPLACEMENT pwp)
{
	TCHAR szBuffer[sizeof(_T("-32767"))*8 + sizeof(_T("65535"))*2];

	sprintf(szBuffer, szFormat,
		pwp->flags, pwp->showCmd,
		pwp->ptMinPosition.x, pwp->ptMinPosition.y,
		pwp->ptMaxPosition.x, pwp->ptMaxPosition.y,
		pwp->rcNormalPosition.left, pwp->rcNormalPosition.top,
		pwp->rcNormalPosition.right, pwp->rcNormalPosition.bottom);

	AfxGetApp()->WriteProfileString(szSection, szWindowPos, szBuffer);
}



BOOL CNGWindowPlacement::ParsePlacementString(const CString& sBuffer)
{
	if (!sBuffer.IsEmpty())
	{
		int nRead = sscanf( sBuffer,
							szFormat,
							&flags,
							&showCmd,
							&ptMinPosition.x, &ptMinPosition.y,
							&ptMaxPosition.x, &ptMaxPosition.y,
							&rcNormalPosition.left, &rcNormalPosition.top,
							&rcNormalPosition.right, &rcNormalPosition.bottom);

		if (10 == nRead)
			return TRUE;
	}
	return FALSE;
}


CString CNGWindowPlacement::BuildPlacementString(void)
{
   	TCHAR szBuffer[sizeof(_T("-32767"))*8 + sizeof(_T("65535"))*2];

	sprintf(	szBuffer,
				szFormat,
				flags, showCmd,
				ptMinPosition.x, ptMinPosition.y,
				ptMaxPosition.x, ptMaxPosition.y,
				rcNormalPosition.left, rcNormalPosition.top,
				rcNormalPosition.right, rcNormalPosition.bottom);

	return szBuffer;
}


CNGWindowPlacement::CNGWindowPlacement()
{
	// Note: "length" is inherited from WINDOWPLACEMENT
	length = sizeof(WINDOWPLACEMENT);
}

CNGWindowPlacement::~CNGWindowPlacement()
{
}



//////////////////
// Restore window placement from profile key
//
BOOL CNGWindowPlacement::Restore(LPCSTR lpKeyName, CWnd* pWnd)
{
	if (GetProfileWP(lpKeyName))
	{
		// Only restore if window intersects the screen.
		//
		CRect rcTemp, rcScreen(0,0,GetSystemMetrics(SM_CXSCREEN),
			GetSystemMetrics(SM_CYSCREEN));
		if (!::IntersectRect(&rcTemp, &rcNormalPosition, &rcScreen))
			return FALSE;

		pWnd->SetWindowPlacement(this);	// set placement

		return TRUE;
	}
	return FALSE;
}


//////////////////
// Get window placement from profile.
//
BOOL CNGWindowPlacement::GetProfileWP(LPCSTR lpKeyName)
{
	CWinApp *pApp = AfxGetApp();
	ASSERT_VALID(pApp);

	CString sBuffer = AfxGetApp()->GetProfileString(lpKeyName, szWindowPos);

	if (ParsePlacementString(sBuffer))
		return TRUE;

	return FALSE;
}

////////////////
// Save window placement in app profile
//
BOOL CNGWindowPlacement::Save(LPCSTR lpKeyName, CWnd* pWnd)
{
	pWnd->GetWindowPlacement(this);

	return WriteProfileWP(lpKeyName);
}

//////////////////
// Write window placement to app profile
//
BOOL CNGWindowPlacement::WriteProfileWP(LPCSTR lpKeyName)
{
	CString sBuffer = BuildPlacementString();
	if (!sBuffer.IsEmpty())
	{
		AfxGetApp()->WriteProfileString(lpKeyName, szWindowPos, sBuffer);

		return TRUE;
	}
	return FALSE;
}


// The ugly casts are required to help the VC++ 3.0 compiler decide which
// operator<< or operator>> to use. If you're using VC++ 4.0 or later, you 
// can delete this stuff.
//
#if (_MSC_VER < 1000)		// 1000 = VC++ 4.0
#define UINT_CAST (LONG)
#define UINT_CASTREF (LONG&)
#else
#define UINT_CAST
#define UINT_CASTREF
#endif

//////////////////
// Write window placement to archive
// WARNING: archiving functions are untested.
//
CArchive& operator<<(CArchive& ar, const CNGWindowPlacement& wp)
{
	ar << UINT_CAST wp.length;
   ar << UINT_CAST wp.flags;
	ar << UINT_CAST wp.showCmd;
   ar << wp.ptMinPosition;
   ar << wp.ptMaxPosition;
   ar << wp.rcNormalPosition;
	return ar;
}

//////////////////
// Read window placement from archive
// WARNING: archiving functions are untested.
//
CArchive& operator>>(CArchive& ar, CNGWindowPlacement& wp)
{
	ar >> UINT_CASTREF wp.length;
   ar >> UINT_CASTREF wp.flags;
	ar >> UINT_CASTREF wp.showCmd;
   ar >> wp.ptMinPosition;
   ar >> wp.ptMaxPosition;
   ar >> wp.rcNormalPosition;
	return ar;
}

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