|
#include "StdAfx.h"
#include "DCandGraph.h"
#include "TplOperators.h"
using namespace GE_::Mfc;
CAutoDC::CAutoDC(CWnd* pWnd)
{
Attach(GetDC(pWnd->GetSafeHwnd()));
m_save = SaveDC();
m_bAutorelease = true;
}
CAutoDC::CAutoDC(HDC hDC, bool bAutoRelease)
{
Attach(hDC);
m_save = SaveDC();
m_bAutorelease = bAutoRelease;
}
CAutoDC::CAutoDC(CBitmap* pBmp)
{
CreateCompatibleDC(CWnd::GetDesktopWindow()->GetWindowDC());
m_save = SaveDC();
SelectObject(pBmp);
m_bAutorelease = true;
}
CAutoDC::~CAutoDC()
{
if(m_hDC)
{
HWND hWnd = GetWindow()->GetSafeHwnd();
HDC hDC = Detach(); //CAutoDC::Detach
if(m_bAutorelease)
{
if(hWnd)
ReleaseDC(hWnd, hDC);
else
::DeleteDC(hDC);
}
}
}
HDC CAutoDC::Detach()
{
if (m_save != -1)
RestoreDC(m_save);
HDC hdc = CDC::Detach();
m_hDC = NULL;
return hdc;
}
/////////////////////////////////////////////////////////////////
double CColor::HuetoRGB(double m1, double m2, double h )
{
if( h < 0 ) h += 1.0;
if( h > 1 ) h -= 1.0;
if( 6.0*h < 1 )
return (m1+(m2-m1)*h*6.0);
if( 2.0*h < 1 )
return m2;
if( 3.0*h < 2.0 )
return (m1+(m2-m1)*((2.0/3.0)-h)*6.0);
return m1;
}
CColor::CColor(double H, double L, double S )
{
double r,g,b;
double m1, m2;
if( S==0 )
{
r = g = b = L;
}
else
{
if( L <= 0.5 )
m2 = L*(1.0+S);
else
m2 = L+S-L*S;
m1 = 2.0*L-m2;
r = HuetoRGB(m1,m2,H+1.0/3.0);
g = HuetoRGB(m1,m2,H);
b = HuetoRGB(m1,m2,H-1.0/3.0);
}
m_color = RGB((BYTE)(r*255),(BYTE)(g*255),(BYTE)(b*255));
}
void CColor::RGBtoHSL(double *H, double *S, double *L )
{
double delta;
double r = (double)R()/255;
double g = (double)G()/255;
double b = (double)B()/255;
double cmax = max(r,max(g,b));
double cmin = min(r,min(g,b));
*L = (cmax + cmin) / 2.0;
if(cmax==cmin)
{
*S = 0;
*H = 0; // it's really undefined
}
else
{
if( *L < 0.5 )
*S = (cmax-cmin)/(cmax+cmin);
else
*S = (cmax-cmin)/(2.0-cmax-cmin);
delta = cmax - cmin;
if( r == cmax )
*H = (g-b)/delta;
else if( g == cmax )
*H = 2.0 +(b-r)/delta;
else
*H = 4.0 + (r-g)/delta;
*H /= 6.0;
if( *H < 0.0 )
*H += 1;
}
}
CColor CColor::Mix(const CColor& c, int w) //0 = *this, 10000 = c
{
CColor ret;
GE_::Operators::Limit(w,0,10000);
for(int i=0; i<4; i++)
{
int w1 = GetByte(i);
int w2 = c.GetByte(i);
int r = ((w1*(10000-w))+(w2*w))/10000;
ret.GetByte(i) = (BYTE)r;
}
return ret;
}
//////////////////////////////////////////////////////////////////////////
CDCPathSaver::CDCPathSaver()
{
m_nCount = 0;
m_pPoints = NULL;
m_pTypes = NULL;
}
CDCPathSaver::~CDCPathSaver()
{
Clear();
}
void CDCPathSaver::Clear()
{
if (m_pPoints) delete m_pPoints; m_pPoints=NULL;
if (m_pTypes) delete m_pTypes; m_pTypes=NULL;
m_nCount = 0;
}
void CDCPathSaver::SavePathFrom(CDC* pDC)
{
Clear();
m_nCount = pDC->GetPath(NULL, NULL, 0); //just count
m_pPoints = new POINT[m_nCount]; //allocate memory
m_pTypes = new BYTE[m_nCount];
ASSERT(m_pPoints && m_pTypes);
pDC->GetPath(m_pPoints,m_pTypes,m_nCount); //saves
}
void CDCPathSaver::RenderPathTo(CDC* pDC)
{
if (!m_nCount) return;
pDC->PolyDraw(m_pPoints,m_pTypes,m_nCount); //just draw
}
///////////////////////////////////////////////////////////////////////////////////
CDragDC::CDragDC(CWnd* pWnd)
:CWindowDC(pWnd? pWnd: CWnd::GetDesktopWindow())
{
m_pen.CreatePen(PS_SOLID, 3, GetSysColor(COLOR_BTNFACE));
m_nSave = SaveDC();
SelectObject(&m_pen);
SelectStockObject(NULL_BRUSH);
SetROP2(R2_NOTXORPEN);
SelectStockObject(DEFAULT_GUI_FONT);
SetTextColor(GetSysColor(COLOR_BTNFACE));
SetBkMode(TRANSPARENT);
if(pWnd)
{
CPoint O(0,0); pWnd->ClientToScreen(&O);
SetViewportOrg(O);
}
}
CDragDC::~CDragDC()
{
RestoreDC(m_nSave);
}
|
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.
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.