Click here to Skip to main content
15,893,904 members
Articles / Programming Languages / C++

Writing Win32 Apps with C++: V2 - part 1

Rate me:
Please Sign up or sign in to vote.
4.70/5 (34 votes)
20 Jun 2005CPOL14 min read 107.9K   1.2K   73  
An independent framework to handle Win32 objects inside C++ classes.
//resread.h
//	win32 cirtom resource reader

#pragma once
#include "dependency.h"
#include "stdx/smart.hpp"
#pragma message("including "__FILE__)

namespace GE_ { namespace winx {

	class string_or_id
	{
	public:
		operator LPCTSTR() const { return lpctstr; }
		string_or_id(LPCTSTR lp) { lpctstr = lp; }
		string_or_id(UINT n) { lpctstr = (LPCTSTR)LOWORD(n); }
	private:
		LPCTSTR lpctstr;
	};

	
	template<class ResData>
	class resource
	{
	public:
		struct derefexcp {};

		resource() {}
		resource(const resource& r) { pData = r.pData; }
		resource(const string_or_id& res_name, const string_or_id& res_type, HINSTANCE hInst=0)
		{
			if(!hInst) hInst = (HINSTANCE)GetModuleHandle(0);
			pData.New();
			pData->create(res_name, res_type, hInst);
		}
		typedef ResData resource_data;
		typedef ResData* resource_data_pointer;

		operator ResData*() const { return (!pData)?0: pData->pResData; }
		ResData* operator->() const { if(!Data) stdx::throw_excpptr<derefexcp>(); return pData->pResData; }
		ResData& operator*() const { if(!Data) stdx::throw_excpptr<derefexcp>(); return *pData->pResData; }

	private:
		friend class resource;
		struct data: public stdx::refcountable
		{
			HRSRC hRsrc;
			HGLOBAL hRes;
			ResData* pResData;

			void create(LPCTSTR res_name, LPCTSTR res_type, HINSTANCE hInst)
			{
				hRsrc = FindResource(hInst, res_name, res_type);
				if(hRsrc)
				{
					hRes = LoadResource(hInst, hRsrc);
					if(hRes)
					{
						pResData = (ResData*)LockResource(hRes);
						return;
					}
				}
			}
			void on_lastrelease()
			{
				UnlockResource(hRes);
				FreeResource(hRes);
				pResData = 0;
				hRsrc = 0; hRes = 0;
			}
		};
		typedef typename stdx::ptr<data>::strong data_p;
		data_p pData;
	};




	//////////////////////////////
	// TOOLBAR RESOURE HANDLING
	#ifndef RT_TOOLBAR
	#define RT_TOOLBAR  MAKEINTRESOURCE(241)
	#endif

	struct toolbardata
	{
		WORD wVersion;
		WORD wWidth;
		WORD wHeight;
		WORD wItemCount;
		//WORD aItems[wItemCount]

		WORD* items()
			{ return (WORD*)(this+1); }
	};

	typedef resource<toolbardata> HSTOOLBAR;	

}}

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
Architect
Italy Italy
Born and living in Milan (Italy), I'm an engineer in electronics actually working in the ICT department of an important oil/gas & energy company as responsible for planning and engineering of ICT infrastructures.
Interested in programming since the '70s, today I still define architectures for the ICT, deploying dedicated specific client application for engineering purposes, working with C++, MFC, STL, and recently also C# and D.

Comments and Discussions