⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utils1.h

📁 实时监控
💻 H
字号:
#ifndef _UTILS_H
#define _UTILS_H

#include "vector"
#define ShowChild(x) OpChildWnd( this, x, ChildOperations::show )
#define HideChild(x) OpChildWnd( this, x, ChildOperations::hide )
#define EnableChild(x) OpChildWnd( this, x, ChildOperations::enable )
#define DisableChild(x) OpChildWnd( this, x, ChildOperations::disable )

#define ShowChild1(x,y) OpChildWnd( this, x, (y)?ChildOperations::show:ChildOperations::hide )
#define EnableChild1(x,y) OpChildWnd( this, x, (y)?ChildOperations::enable:ChildOperations::disable )

#define OpChildWndThis(x,y) OpChildWnd( this, x, y )

namespace ChildOperations
{

	struct IChildWndOpBase
	{
		virtual void operator()( CWnd *p ) = 0;
	};
	class _Show: public IChildWndOpBase{
	public: 
		UINT _nShow;
		_Show(UINT nShow=SW_SHOW): _nShow(nShow){}
		virtual void operator ()( CWnd *p ){ p->ShowWindow(_nShow); }
	};
	class _Enable: public IChildWndOpBase{
	public: 
		bool _b;
		_Enable(bool b=true):_b(b){}
		virtual void operator ()( CWnd *p ){ p->EnableWindow( _b ); }
	};
	static _Show show;
	static _Show hide(SW_HIDE);
	static _Enable enable;
	static _Enable disable(false);
}
/*
inline
void _Show( CWnd* p, UINT nID ){
	OpChildWnd( p, nID, ChildOperations::show );
}

inline
void _Hide( CWnd *p, UINT nID ){
	OpChildWnd( p, nID, ChildOperations::hide );
}

inline
void _Enable( CWnd *p, UINT nID ){
	OpChildWnd( p, nID, ChildOperations::enable );
}

inline
void _Disable( CWnd *p, UINT nID ){
	OpChildWnd( p, nID, ChildOperations::disable );
}
*/
namespace utils
{

inline
void VCenterRect( const CRect& host, CRect& client )
{
	int h1 = host.Height();
	int h2 = client.Height();
	client.top = host.top + (h1-h2)/2;
	client.bottom = client.top + h2;
}

//////////////////////////////////////////////////////////////////////////

inline
void HCenterRect( const CRect& host, CRect& client )
{
	int w1 = host.Width();
	int w2 = client.Width();
	client.left = host.left + (w1-w2)/2;
	client.right = client.left + w2;
}

//////////////////////////////////////////////////////////////////////////

inline
void CenterRect( const CRect& host, CRect& client )
{
	VCenterRect( host, client );
	HCenterRect( host, client );
}

//////////////////////////////////////////////////////////////////////////

inline
void ResizeWindow( CWnd *p, int w, int h )
{
	if( !p || !IsWindow(p->m_hWnd) )
		return;
	CRect rc;
	p->GetWindowRect( rc );
	p->ScreenToClient( rc );
	rc.OffsetRect( -rc.left, -rc.top );

	p->SetWindowPos( NULL, 0, 0, w, h, SWP_NOMOVE | SWP_NOZORDER );
}

//////////////////////////////////////////////////////////////////////////

inline
void GrowWindowSize( CWnd *p, int dw, int dh )
{
	CRect rc;
	p->GetWindowRect( rc );
	ResizeWindow( p, rc.Width() + dw, rc.Height() + dh );
}
//////////////////////////////////////////////////////////////////////////

class is_window
{
public:
	is_window(CWnd *p):_is_window(false)
	{
		if( p && ::IsWindow(p->m_hWnd) )
			_is_window = true;
		else
			_is_window = false;
	}
	operator bool() const { return _is_window; }
	operator !() const { return !_is_window; }
private:
	bool _is_window;
};

//////////////////////////////////////////////////////////////////////////

class window_rect: public CRect
{
public:
	window_rect( CWnd *p )
	{
		if( is_window(p) )
			p->GetWindowRect( *this );
	}
};

//////////////////////////////////////////////////////////////////////////

class client_rect: public CRect
{
public:
	client_rect( CWnd *p )
	{
		if( p )
			p->GetClientRect( *this );
	}
};

//////////////////////////////////////////////////////////////////////////

class window_client_rect: public window_rect
{
public:
	window_client_rect( CWnd *pWnd, CWnd *p2Client ): window_rect( pWnd )
	{
		if( p2Client )
			p2Client->ScreenToClient( *this );
	}
};

//////////////////////////////////////////////////////////////////////////
class move
{
	CRect _rc;
public:
	move(const CRect& rc){_rc = rc;}
	void operator()( CWnd *p )
	{
		p->MoveWindow(_rc);
	}
};

template<class T>
class dlg_item
{
public:
	dlg_item(CWnd *p):pThis(p){}
	dlg_item(CWnd& p):pThis(&p){}
	dlg_item(CWnd *p, UINT nID): pThis(NULL)
	{
		if( !is_window(p) )
			return;
		pThis = p->GetDlgItem(nID);
	}

	operator T() const {return static_cast<T>(pThis);}
private:
	CWnd *pThis;
};


//////////////////////////////////////////////////////////////////////////

class CDbRect: public CRect
{
public:
	CDbRect():actw(0), acth(0)
	{
		memset(this,0,sizeof(CDbRect));
	}

	CDbRect( double l, double t, double w, double h, int aw = 0, int ah = 0 ):
	  actw(0), acth(0)
	{
		SetRect( l, t, w, h, aw, ah );
	}

	CDbRect& SetRect( double l, double t, double w, double h, int aw = 0, int ah = 0 )
	{
		if( aw )
			actw = aw;
		if( ah )
			acth = ah;

		left = actw*l;
		top = acth*t;
		right = left + actw*w;
		bottom = top + acth*h;
		return *this;
	}
	
	void SetWidth( int w )
	{
		right = left + w;
	}

	void SetHeight( int h )
	{
		bottom = top + h;
	}
	int actw, acth;
};

//////////////////////////////////////////////////////////////////////////

template<class T>
class CAutoGDI: public T
{
public:
	CAutoGDI():m_pDC(NULL), m_pOld(NULL)
	{
	}

	CAutoGDI(CDC *pDC):m_pDC(pDC), m_pOld(NULL)
	{
	}

	virtual ~CAutoGDI()
	{
		LeaveDC();
	}

	void JoinDC( CDC *pDC = NULL )
	{
		ASSERT( pDC || m_pDC );
		ASSERT( !m_pOld );

		if( pDC )
			m_pDC = pDC;
		m_pOld = m_pDC->SelectObject( this );
	}

	void LeaveDC()
	{
		if( !m_pDC || !m_pDC->m_hDC )
			return;

		m_pDC->SelectObject( m_pOld );

		//m_pDC = NULL;
		m_pOld = NULL;
	}

	void Update()
	{
		LeaveDC();
		JoinDC();
	}
protected:
	CDC *m_pDC;
	T *m_pOld;
};

//////////////////////////////////////////////////////////////////////////
class CPointFont: public utils::CAutoGDI<CFont>
{
public:
	CPointFont(CDC *pDC, LPCTSTR lpszFont, 
		int nPt, bool bBold = false, 
		COLORREF cl = RGB(0,0,0) )
	{
		Create(pDC,lpszFont,nPt,bBold,cl);
	}

	void Create(CDC *pDC, LPCTSTR lpszFont, 
		int nPt, bool bBold = false, 
		COLORREF cl = RGB(0,0,0) )
	{
		CFont ft;
		ft.CreatePointFont( nPt, lpszFont, pDC );
		pDC->SetTextColor( cl );

		LOGFONT lf;
		ft.GetLogFont(&lf);
		ft.DeleteObject();
		lf.lfWeight = bBold?FW_BOLD:FW_NORMAL;

		CreateFontIndirect( &lf );
		JoinDC( pDC );
	}
	static CPointFont* CreateFont( CDC *pDC, 
		LPCTSTR lpszFont, 
		int nPt, 
		bool bBold = false,
		COLORREF cl = RGB(0,0,0) )
	{
		CPointFont *p = new CPointFont(pDC,lpszFont,nPt,bBold,cl);
		return p;
	}
	virtual ~CPointFont()
	{
		LeaveDC();
	}
};

class center_child
{
	typedef std::vector<CWnd*> children;
	typedef std::vector<CRect> rects;
	children _children;
	rects _rects;
	CRect rcTotal;
	CWnd *parent;
public:
	center_child(CWnd *pParent):parent(pParent){}
	center_child& operator<<(CWnd& wnd)
	{
		_children.push_back(&wnd);
		return *this;
	}
	void operator()()
	{
		CRect rcTotal1(0,0,0,0);
		for( int i=0; i<_children.size(); i++ )
		{
			CWnd*& p = _children[i];
			if( utils::is_window(p) )
			{
				utils::window_rect rc(p);
				_rects.push_back(rc);
				rcTotal1.UnionRect(rcTotal1,rc);
			}
			else
			{
				_children.clear();
				_rects.clear();
				return;
			}
		}
		rcTotal = rcTotal1;
		utils::client_rect rcClient(parent);
		utils::CenterRect(rcClient, rcTotal1);
		int dx = rcTotal1.left - rcTotal.left;
		int dy = rcTotal1.top - rcTotal.top;

		for( i=0; i<_children.size(); i++ )
		{
			CWnd*& p = _children[i];
			CRect& rect = _rects[i];
			rect.OffsetRect(dx,dy);
			if( utils::is_window(p) )
			{
				p->MoveWindow(rect);
			}
		}
	}
};	// center_child

//////////////////////////////////////////////////////////////////////////
// 初始化结构体
// 返回值为结构体的大小(字节)
template<typename T>
inline int InitStruct(T *p)
{
	int size = sizeof(T);
	ZeroMemory( p, size );
	return size;
}	// InitStruct

};	// namespace utils

template<class Fn> inline
void OpChildWnd( CWnd *pParent, UINT nID, Fn& fn )
{
	CWnd *t = pParent->GetDlgItem( nID );
	if( utils::is_window(t) )
		fn( t );
}

#define USING_UTILS using namespace utils
#endif // _UTILS_H

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -