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

📄 gdihelper.h

📁 vc编写的
💻 H
字号:
/*
 Copyright (c) 1999, kSet Lab 
 Author: Konstantin Bukreev 
 E-mail: konstantin@mail.primorye.ru 

 Created: 12.10.99 16:46:04
 Version: 1.0.0

 Description: simple wrappers for gdi objects
*/

#ifndef _GdiHelper_0d9ed482_80c2_11d3_9285_0080adb811c5
#define _GdiHelper_0d9ed482_80c2_11d3_9285_0080adb811c5

#if _MSC_VER > 1000 
#pragma once
#endif // _MSC_VER > 1000

//where T is GDI handle such as: HDC, HBRUSH, HPEN, HFONT, etc.
template <class T>
class _kGDIObject
{
protected:
	T m_handle;
public:
	_kGDIObject(T handle = T(0)) : m_handle(handle) {}
	~_kGDIObject()  {}
	
	T Attach(T handle) {T hOld = m_handle; m_handle = handle; return hOld;}
	T Detach() {return Attach(T(0));}

	T Get() const {return m_handle;}
	operator T () const {return m_handle;}
	operator T* () const {return &m_handle;}
	T* operator& () {return &m_handle;}
	operator bool () const {return m_handle != 0;}
	bool operator ! () const {return m_handle == 0;}
	bool isOk () const {return m_handle != 0;}
};


class _kDrawDC : 
	public _kGDIObject<HDC>
{
public:
	_kDrawDC (HDC hdc) : _kGDIObject<HDC>(hdc) {}

	void FillSolidRect(LPCRECT lpRect, COLORREF clr)
	{
		::SetBkColor(m_handle, clr);
		::ExtTextOut(m_handle, 0, 0, ETO_OPAQUE, lpRect, 0, 0, 0);
	}
	void FillSolidRect(int x, int y, int cx, int cy, COLORREF clr)
	{
		::SetBkColor(m_handle, clr);
		RECT rect = {x, y, x + cx, y + cy};
		::ExtTextOut(m_handle, 0, 0, ETO_OPAQUE, &rect, 0, 0, 0);
	}
	void Draw3dRect(int x, int y, int cx, int cy, COLORREF clrTopLeft, COLORREF clrBottomRight)
	{
		FillSolidRect(x, y, cx - 1, 1, clrTopLeft);
		FillSolidRect(x, y, 1, cy - 1, clrTopLeft);
		FillSolidRect(x + cx, y, -1, cy, clrBottomRight);
		FillSolidRect(x, y + cy, cx, -1, clrBottomRight);
	}
	void Draw3dRect(RECT& rc, COLORREF clrTopLeft, COLORREF clrBottomRight)
	{
		Draw3dRect(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, clrTopLeft, clrBottomRight);
	}
	void Draw3DBorder (int x, int y, int cx, int cy, COLORREF clrSh, bool bRaised = false, COLORREF clrHl = RGB(255, 255, 255))
	{
		if (bRaised)
		{
			Draw3dRect (x, y, cx, cy, clrHl, clrSh);
			Draw3dRect (x + 1, y + 1, cx - 2, cy - 2, clrSh, clrHl);
		}
		else
		{
			Draw3dRect (x, y, cx, cy, clrSh, clrHl);
			Draw3dRect (x + 1, y + 1, cx - 2, cy - 2, clrHl, clrSh);
		}		
	}
	void Draw3DBorder (RECT& rc, COLORREF clrSh, bool bRaised = false, COLORREF clrHl = RGB(255, 255, 255))
	{
		Draw3DBorder(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, clrSh, bRaised, clrHl);
	}
	void DrawLowEdge (int x, int y, int cx, int cy, COLORREF clrHl, COLORREF clrSh, bool bUp)
	{		
		if (bUp)
			Draw3dRect(x, y, cx, cy, clrHl, clrSh);    	
		else
			Draw3dRect(x, y, cx, cy, clrSh, clrHl);    	
	}
	void DrawLowEdge (RECT& rc, COLORREF clrHl, COLORREF clrSh, bool bUp)
	{		
		DrawLowEdge(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, clrHl, clrSh, bUp);
	}	
	void DrawHighEdge (int x, int y, int cx, int cy, COLORREF clrHl, COLORREF clrSh, bool bUp)
	{		
		if (bUp)
		{
			Draw3dRect(x, y, cx - 1 , cy - 1, clrHl, clrSh); 
			Draw3dRect(x, y, cx, cy, clrHl, RGB(0, 0, 0)); 			
		}
		else
		{
			Draw3dRect(x + 1, y + 1, cx - 1, cy - 1, RGB(0, 0, 0), clrHl); 			
			Draw3dRect(x, y, cx, cy, clrSh, clrHl); 			
		}
	}
	void DrawHighEdge (RECT& rc, COLORREF clrHl, COLORREF clrSh, bool bUp)
	{	
		DrawHighEdge(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, clrHl, clrSh, bUp);
	}	
	void DrawLowButton (int x, int y, int cx, int cy, 
		COLORREF clrBack, COLORREF clrHl, COLORREF clrSh, bool bUp = true, bool bBorderPresent = false, COLORREF clrBorder = RGB(0, 0, 0))
	{		
		FillSolidRect(x, y, cx, cy, clrBack);
		if (bBorderPresent)
		{
			DrawBorder(x, y, cx, cy, clrBorder);
			x++; y++; cx -= 2; cy -= 2;
		}		
		DrawLowEdge(x, y, cx, cy, clrHl, clrSh, bUp);	
	}
	void DrawLowButton (RECT& rc, COLORREF clrBack, COLORREF clrHl, COLORREF clrSh, 
		bool bUp = true, bool bBorderPresent = false, COLORREF clrBorder = RGB(0, 0, 0))
	{
		DrawLowButton(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, clrBack, clrHl, clrSh, bUp, bBorderPresent, clrBorder);
	}
	void DrawHighButton (int x, int y, int cx, int cy, 
		COLORREF clrBack, COLORREF clrHl, COLORREF clrSh, bool bUp = true, bool bBorderPresent = false, COLORREF clrBorder = RGB(0, 0, 0))
	{		
		FillSolidRect(x, y, cx, cy, clrBack);
		if (bBorderPresent)
		{	
			DrawBorder(x, y, cx, cy, clrBorder);
			x++; y++; cx -= 2; cy -= 2;
		}		
		DrawHighEdge(x, y, cx, cy, clrHl, clrSh, bUp);	
	}
	void DrawHighButton (RECT& rc, COLORREF clrBack, COLORREF clrHl, COLORREF clrSh, 
		bool bUp = true, bool bBorderPresent = false, COLORREF clrBorder = RGB(0, 0, 0))
	{
		DrawHighButton(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, clrBack, clrHl, clrSh, bUp, bBorderPresent, clrBorder);
	}
	void DrawBorder (int x, int y, int cx, int cy, COLORREF clr)
	{
		RECT rc = {x, y, x + cx, y + cy};
		HBRUSH hbr = CreateSolidBrush(clr);
		::FrameRect(m_handle, &rc, hbr);
		DeleteObject(hbr);
	}		
	void DrawBorder (RECT& rc, COLORREF clr)
	{
		HBRUSH hbr = CreateSolidBrush(clr);
		::FrameRect(m_handle, &rc, hbr);
		DeleteObject(hbr);
	}
};

// T - class derived from CWindow
template <class T>
class _kAutoDC :
	public _kGDIObject<HDC>
{
	T* m_pT;	
public:
	_kAutoDC(T* pT) : m_pT(pT) 
	{
		m_handle = m_pT->GetDC(); 
		_ASSERTE(m_handle != 0);
	}
	~_kAutoDC() 
	{
		m_pT->ReleaseDC(m_handle);
	}	
};

class _kMemDC :
	public _kGDIObject<HDC>
{
public:
	_kMemDC(HDC hdc) {ATLASSERT(hdc != 0);	m_handle = ::CreateCompatibleDC(hdc);	ATLASSERT(m_handle != 0);}
	~_kMemDC() {::DeleteDC(m_handle);}
};

template <class T>
class _kAutoSelect : 
	public _kGDIObject<T>
{
	HGDIOBJ m_old;	
	HDC m_hdc;
public:
	_kAutoSelect(HDC hDC, T handle) : m_hdc(hDC), _kGDIObject<T>(handle)
	{
		_ASSERTE(m_hdc != 0);
		_ASSERTE(m_handle != 0);
		m_old = (HGDIOBJ)SelectObject(m_hdc, m_handle);
	}
	~_kAutoSelect()
	{
		SelectObject(m_hdc, m_old);
	}
};
	
template <class T>
class _kAutoFree :
	public _kGDIObject<T>
{		
public:
	_kAutoFree(T handle) : _kGDIObject<T>(handle) {_ASSERTE(m_handle != 0);}
	~_kAutoFree() {ATLASSERT(DeleteObject(m_handle) != 0);}
};

template <class T>
class _kAutoFree2 :
	public T
{		
public:
	_kAutoFree2(T handle) : T(handle) {}
	~_kAutoFree2() {if (isOk()) ATLASSERT(DeleteObject(m_handle) != 0);}
};

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

class _kOleFont : 
	public _kGDIObject<HFONT>
{	
	CComQIPtr<IFont> m_pF;
public:
	_kOleFont (IFontDisp* pFontDisp)
		: m_pF(pFontDisp)
	{		
		if (m_pF && SUCCEEDED(m_pF->get_hFont(&m_handle)))
			m_pF->AddRefHfont(m_handle);				
	}
	~_kOleFont()
	{
		if (m_pF && m_handle)		
			m_pF->ReleaseHfont(m_handle);										
	}		
};

class _kPen : 
	public _kAutoFree2< _kGDIObject<HPEN> >
{
public:
	_kPen () : _kAutoFree2< _kGDIObject<HPEN> >(0) {}
	~ _kPen () {}

	void Create(int fnPenStyle, int nWidth, COLORREF crColor)
	{
		ATLASSERT(m_handle == 0);
		m_handle = CreatePen(fnPenStyle, nWidth, crColor);		
		ATLASSERT(m_handle != 0);		
	}	
};

class _kBrush :
	public _kAutoFree2< _kGDIObject<HBRUSH> >
{
public:
	_kBrush (COLORREF clr) : _kAutoFree2< _kGDIObject<HBRUSH> >(CreateSolidBrush(clr)) {}
	~ _kBrush () {}
};

#endif //_GdiHelper_0d9ed482_80c2_11d3_9285_0080adb811c5

⌨️ 快捷键说明

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