Click here to Skip to main content
15,892,575 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.
//coords.h
//	coordinates aritmethic
#pragma once


namespace GE_{ namespace win{

	struct size: public tagSIZE
	{
		size() { cx = cy = 0; }
		size(const size& _s) { cx = _s.cx; cy = _s.cy; }
		size(const tagPOINT& _p) { cx = _p.x; cy = _p.y; }
		size(const tagSIZE& _s) { cx = _s.cx; cy = _s.cy; }
		size(long _cx, long _cy) { cx = _cx; cy = _cy; }

		size& operator+=(const size& _s) { cx+=_s.cx; cy+=_s.cy; return *this; }
		size& operator-=(const size& _s) { cx-=_s.cx; cy-=_s.cy; return *this; }
		size& operator*=(const size& _s) { cx*=_s.cx; cy*=_s.cy; return *this; }
		size& operator/=(const size& _s) { cx/=_s.cx; cy/=_s.cy; return *this; }

		size& operator*=(long _n) { cx*=_n; cy*=_n; return *this; }
		size& operator/=(long _n) { cx/=_n; cy/=_n; return *this; }

		size& operator=(const size& _s) {  cx = _s.cx; cy = _s.cy; return *this; }

		bool operator!() const { return !cx || !cy; } 
		bool operator==(const size& _s) const { return cx == _s.cx && cy == _s.cy; }
	};

	struct point: public tagPOINT
	{
		point() { x = y = 0; }
		point(const point& _p) { x = _p.x; y = _p.y; }
		point(const tagPOINT& _p) { x = _p.x; y = _p.y; }
		point(const tagSIZE& _s) { x = _s.cx; y = _s.cy; }
		point(long _x, long _y) { x = _x; y = _y; }

		point& operator+=(const size& _s) { x+=_s.cx; y+=_s.cx; return *this; }
		point& operator-=(const size& _s) { x-=_s.cx; y-=_s.cx; return *this; }

		point& operator*=(const size& _s) { x*=_s.cx; y*=_s.cx; return *this; }
		point& operator/=(const size& _s) { x/=_s.cx; y/=_s.cx; return *this; }

		point& operator*=(long _n) { x*=_n; y*=_n; return *this; }
		point& operator/=(long _n) { x/=_n; y/=_n; return *this; }

		size operator-(const point& _p) const { return size(x-_p.x, y-_p.y); }

		point& operator=(const point& _p) {  x = _p.x; y = _p.y; return *this; }

		bool operator!() const { return !x && !y; }
		bool operator==(const point& _p) const { return x == _p.x && y == _p.y; }
	};

	
	struct rect: public tagRECT
	{
		rect() { left = right = top = bottom = 0; }
		rect(long _left, long _top, long _right, long _bottom) { left=_left; right=_right; top=_top; bottom=_bottom; }
		rect(const rect& _r) { memcpy(&left, &_r.left, 4*sizeof(left));  }
		rect(const LPCRECT _lprc) { memcpy(&left, &_lprc->left, 4*sizeof(left));  }
		rect(const point& _p, const size& _s) { left = _p.x; top = _p.y; right = left+_s.cx; bottom=top+_s.cy; }
		rect(const point& _p, const point& _q) { left = _p.x; top = _p.y; right = _q.x; bottom=_q.y; }
		operator LPCRECT() const { return this; }
		operator LPRECT() { return this; }

		rect& operator=(const rect& _r) { memcpy(&left, &_r.left, 4*sizeof(left)); return *this;}

		void normalize() { if (right<left) std::swap(left,right); if(bottom<top) std::swap(top,bottom); }
		point& topleft() { return *(point*)&left; }
		point& bottomright() { return *(point*)&right; }
		const point& topleft() const { return *(point*)&left; }
		const point& bottomright() const { return *(point*)&right; }
		long width() const { return right- left; }
		long height() const { return bottom - top; }
		win::size size() const { return  win::size(width(),height()); }
		long& operator[](int i) { return ((long*)&left)[i]; }
		const long& operator[](int i) const { return ((const long*)&left)[i]; }

		void set_widh(long _w) { right = left+_w; }
		void set_height(long _h) { bottom = top + _h; }
		void set_size(const win::size& _s) { set_widh(_s.cx); set_height(_s.cy); }

		rect& operator&=(const rect& _r) { IntersectRect(this,*this,_r); return *this; }
		rect& operator|=(const rect& _r) { UnionRect(this,*this,_r); return *this; }
 
		rect& operator+=(const win::size& _p) { OffsetRect(this,_p.cx,_p.cy); return *this; }
		rect& operator-=(const win::size& _p) { OffsetRect(this,-_p.cx,-_p.cy); return *this; }

		bool operator!() const { return 0!=IsRectEmpty(this); }
		bool operator==(const rect& r) { return 0!=EqualRect(this, &r); }
		bool operator<(const rect& r) {	using namespace operators; return (r & *this) == *this; }

		bool operator&(const point& p) { return 0!=PtInRect(this,p); }		
	};


}}

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