Click here to Skip to main content
15,892,809 members
Articles / Programming Languages / C++

Writing Win32 Apps with C++ only classes

Rate me:
Please Sign up or sign in to vote.
4.97/5 (32 votes)
1 Mar 2004CPOL21 min read 134.6K   3.5K   109  
C++ classes and wrappers to write W32 apps without MFC, ATL or other (part 1?)
//GdiWrp.h
#pragma once
//wrapper classes for GDI handles


namespace GE_{ namespace NGDI{

	template<class W>
	struct SHdcSave_base:
		public NWrp::HndBase<HDC, W>
	{
	protected:
		int _savedDC;
	public:
		void OnAddRef(NWrp::XRefCount_base* pRC)
		{  _savedDC = SaveDC(Value()); }

		void OnRelease(NWrp::XRefCount_base* pRC)
		{	RestoreDC(Value(), _savedDC); }
	};
	
  
	class SHdcSave:
		public SHdcSave_base<SHdcSave>
	{
	public:
		SHdcSave(HDC hdc) { Attach(hdc); }
	};

	class SHdcPaint:
		public SHdcSave_base<SHdcPaint>
	{
	protected:
		PAINTSTRUCT _ps;
		HWND _hWnd;

		friend TWrpBase;
		
        void Create(HDC& h)
		{
			BeginPaint(_hWnd, &_ps);
			h = _ps.hdc;
		}

		void Destroy(HDC& h)
		{
			EndPaint(_hWnd, &_ps);
		}

	public:
		SHdcPaint(HWND hWnd)
		{	_hWnd = hWnd;  New(); }

		const PAINTSTRUCT& PaintStruct() const { return _ps; }

	};

	class SHdcClient:
		public SHdcSave_base<SHdcClient>
	{
	protected:
		HWND _hWnd;

		friend TWrpBase;

		void Create(HDC& h)
		{	h = GetDC(_hWnd);	}

		void Destroy(HDC& h)
		{	ReleaseDC(_hWnd, h); }

	public:
		SHdcClient(HWND hWnd)
		{	_hWnd = hWnd; New(); }
	};


	class SHdcWnd:
		public SHdcSave_base<SHdcClient>
	{
	protected:
		HWND _hWnd;

		friend TWrpBase;

		void Create(HDC& h)
		{	h = GetWindowDC(_hWnd);	}

		void Destroy(HDC& h)
		{	ReleaseDC(_hWnd, h); }

	public:
		SHdcWnd(HWND hWnd)
		{	_hWnd = hWnd; New(); }
	};

	
	class SHdcMem:
		public SHdcSave_base<SHdcMem>
	{
	protected:
		HBITMAP _hBmp;
		SIZE _size; //null size means _hBmp not created here
		
		friend TWrpBase;
		
		void Create(HDC& h)
		{
			HDC hScreen = GetWindowDC(GetDesktopWindow());
			h = CreateCompatibleDC(hScreen);
			if(!_hBmp && _size.cx && _size.cy)
				_hBmp = CreateCompatibleBitmap(hScreen, _size.cx, _size.cy);
			ReleaseDC(GetDesktopWindow(), hScreen);
		}

		void Destroy(HDC& h)
		{
			DeleteDC(h);
			if(_size.cx && _size.cy && _hBmp)
				DeleteObject(_hBmp);
		}

	public:

		SHdcMem(const SIZE& sz)
		{
			_size = sz;
			_hBmp = NULL;
			New();
			SelectObject(*this, _hBmp);
		}

        SHdcMem(HBITMAP bmp)
		{
			_size.cx = _size.cy = 0;
			_hBmp = bmp;
			New();
			SelectObject(*this, _hBmp);
		}
	};

	
	template<class H>
	class SGdiObj:
		public NWrp::HndBase<H, SGdiObj>
	{
	protected:
		friend TWrpBase;
		void Destroy(H& h)
		{	DeleteObject((HGDIOBJ)h);	}
	};

	typedef NWrp::Wrp<SGdiObj<HPEN> > TPen;
	typedef NWrp::Wrp<SGdiObj<HBRUSH> > TBrush;
	typedef NWrp::Wrp<SGdiObj<HFONT> > TFont;
	typedef NWrp::Wrp<SGdiObj<HRGN> > TRgn;

}}

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