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

📄 extpaintmanager.cpp

📁 FormEditor,模拟Visual Basic窗口编辑界面
💻 CPP
📖 第 1 页 / 共 5 页
字号:
// This is part of the Professional User Interface Suite library.
// Copyright (C) 2001-2002 FOSS Software, Inc.
// All rights reserved.
//
// http://www.fossware.com
// mailto:foss@fossware.com
//
// This source code can be used, modified and redistributed
// under the terms of the license agreement that is included
// in the Professional User Interface Suite package.
//
// Warranties and Disclaimers:
// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND
// INCLUDING, BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
// IN NO EVENT WILL FOSS SOFTWARE INC. BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES,
// INCLUDING DAMAGES FOR LOSS OF PROFITS, LOSS OR INACCURACY OF DATA,
// INCURRED BY ANY PERSON FROM SUCH PERSON'S USAGE OF THIS SOFTWARE
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

#include "StdAfx.h"

//#define __DEFAULT_PAINT_MANAGER_CLASS CExtPaintManager
//#define __DEFAULT_PAINT_MANAGER_CLASS CExtPaintManagerXP

#if _MFC_VER < 0x700
	#include <../src/AfxImpl.h>
#else
	#include <../src/mfc/AfxImpl.h>
#endif

#include "ExtPaintManager.h"




#include <math.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNAMIC( CExtPaintManager, CObject )
IMPLEMENT_DYNAMIC( CExtPaintManagerXP, CExtPaintManager )

#define CXTEXTMARGIN 1

CExtPaintManager::CExtPaintManagerAutoPtr g_PaintManager;

CExtPaintManager::CExtPaintManagerAutoPtr::
	CExtPaintManagerAutoPtr()
	: m_pPaintMenager( NULL )
	, m_strOsVer( _T("Unknown") )
	, m_bIsWin32s( false )
	, m_bIsWin9x( false )
	, m_bIsWin98( false )
	, m_bIsWin98orLater( false )
	, m_bIsWinNT( false )
	, m_bIsWinNT4( false )
	, m_bIsWin2000( false )
	, m_bIsWinXP( false )
{
	
	memset((char *)&m_osVerData,0,sizeof(OSVERSIONINFO));
	m_osVerData.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

CString sTmp( _T("") );
	VERIFY( ::GetVersionEx(&m_osVerData) );
	switch(m_osVerData.dwPlatformId)
	{
	case VER_PLATFORM_WIN32s:
		m_bIsWin32s = true;
		sTmp = _T("Win32s");
	break;
	case VER_PLATFORM_WIN32_WINDOWS:
	{
		m_bIsWin9x = true;
		m_bIsWin98orLater = 
			(m_osVerData.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) &&
			( (m_osVerData.dwMajorVersion > 4) ||
			( (m_osVerData.dwMajorVersion == 4) && (m_osVerData.dwMinorVersion > 0) )
			);
		sTmp = _T("Windows ");
		if(m_bIsWin98orLater)
		{
			if((m_osVerData.dwMajorVersion == 4) && (m_osVerData.dwMinorVersion > 0))
			{
				m_bIsWin98 = true;
				sTmp += _T("98");
			}
			else
				sTmp += _T("98 or later");
		}
		else
			sTmp += _T("95");
	}
	break;
	case VER_PLATFORM_WIN32_NT:
		m_bIsWinNT = true;
		if( m_osVerData.dwMajorVersion == 6 )
		{
			m_bIsWinXP = true;
			sTmp = _T("Windows XP");
		}
		else if( m_osVerData.dwMajorVersion == 5 )
		{
			m_bIsWin2000 = true;
			sTmp = _T("Windows 2000");
		}
		else if( m_osVerData.dwMajorVersion == 4 )
		{
			m_bIsWinNT4 = true;
			sTmp = _T("Windows NT");
		}
		else
			sTmp = _T("Windows NT");
	break;
	default:
	break;
	} // switch(m_osVerData.dwPlatformId)

	m_strOsVer.Format(
		_T("%s v.%lu.%lu (build %lu) %s"),
		(LPCTSTR)sTmp,
		m_osVerData.dwMajorVersion, 
		m_osVerData.dwMinorVersion, 
		m_osVerData.dwBuildNumber,
		m_osVerData.szCSDVersion
		);
	
	VERIFY(
		InitPaintManagerInstance()
		);
}

CExtPaintManager::CExtPaintManagerAutoPtr::
	~CExtPaintManagerAutoPtr()
{
	if( m_pPaintMenager )
		delete m_pPaintMenager;
	m_pPaintMenager = NULL;
}

bool CExtPaintManager::CExtPaintManagerAutoPtr::
	InitPaintManagerInstance()
{
	if( m_pPaintMenager != NULL )
		return true;
	return
		InstallPaintManager(
			(CExtPaintManager *)NULL
			);
}

CExtPaintManager *
	CExtPaintManager::CExtPaintManagerAutoPtr::
		operator->()
{
//	InitPaintManagerInstance();
	ASSERT( m_pPaintMenager != NULL );
	return m_pPaintMenager;
}

bool CExtPaintManager::CExtPaintManagerAutoPtr::
	InstallPaintManager(
		CExtPaintManager * pPaintMenager
		)
{
	if( m_pPaintMenager != NULL )
	{
		if( pPaintMenager == m_pPaintMenager )
			return true;
		if( m_pPaintMenager != NULL )
		{
			delete m_pPaintMenager;
			m_pPaintMenager = NULL;
		}
	} // if( m_pPaintMenager != NULL )

	if( pPaintMenager != NULL )
		m_pPaintMenager = pPaintMenager;
	else
	{
		m_pPaintMenager = new __DEFAULT_PAINT_MANAGER_CLASS;
		if(	!m_pPaintMenager->
				IsKindOf(
					RUNTIME_CLASS( CExtPaintManager )
					)
			)
		{
			ASSERT( FALSE );
			return false;
		}
	}

	m_pPaintMenager->SyncSysColors();
	m_pPaintMenager->InitTranslatedColors();
	return true;
}

bool CExtPaintManager::CExtPaintManagerAutoPtr::
	InstallPaintManager(
		CRuntimeClass * pRtcPaintMenager
		)
{
	if( pRtcPaintMenager == NULL )
		return InitPaintManagerInstance();
CObject* pObj = pRtcPaintMenager->CreateObject();
	if( pObj == NULL )
	{
		ASSERT( FALSE );
		return false;
	}
	ASSERT_VALID( pObj );
CExtPaintManager * pPaintManager =
		DYNAMIC_DOWNCAST(CExtPaintManager,pObj);
	if( pPaintManager == NULL )
	{
		delete pObj;
		ASSERT( FALSE );
		return false;
	}
	return InstallPaintManager( pPaintManager );
}

CExtPaintManager::CExtPaintManager()
{
	ASSERT( this != NULL );

	m_DSS_DISABLED_style = DSS_DISABLED;

/**/
#define __3D_COLOR_DEPTH_R 7
#define __3D_COLOR_DEPTH_G 6
#define __3D_COLOR_DEPTH_B 6

struct
{
    LOGPALETTE    LogPalette;
    PALETTEENTRY  PalEntry[ (__3D_COLOR_DEPTH_R*__3D_COLOR_DEPTH_G*__3D_COLOR_DEPTH_B) ];
} pal;
LOGPALETTE * pLogPalette = (LOGPALETTE*) &pal;
	pLogPalette->palVersion    = 0x300;
	pLogPalette->palNumEntries = (WORD)(__3D_COLOR_DEPTH_R*__3D_COLOR_DEPTH_G*__3D_COLOR_DEPTH_B); 
int i = 0;
    for( int nX = 0; nX < __3D_COLOR_DEPTH_R; nX++ )
    {
		for( int nY = 0; nY < __3D_COLOR_DEPTH_G; nY++ )
		{
			for( int nZ = 0; nZ < __3D_COLOR_DEPTH_B; nZ++ )
			{
				pLogPalette->palPalEntry[i].peRed   =
					BYTE( (nX*255)/(__3D_COLOR_DEPTH_R-1) );
				pLogPalette->palPalEntry[i].peGreen =
					BYTE( (nY*255)/(__3D_COLOR_DEPTH_G-1) );
				pLogPalette->palPalEntry[i].peBlue  =
					BYTE( (nZ*255)/(__3D_COLOR_DEPTH_B-1) );
				pLogPalette->palPalEntry[i].peFlags = 0;
				i++;
			}
		}
    }
    VERIFY( m_PaletteWide.CreatePalette(pLogPalette) );
/**/
}

CExtPaintManager::~CExtPaintManager()
{
	ASSERT( this != NULL );

	m_colors.RemoveAll();
	m_brushes.RemoveAll();
}

COLORREF CExtPaintManager::stat_RGBtoCMY( COLORREF clr )
{
int nR = GetRValue( clr );
int nG = GetGValue( clr );
int nB = GetBValue( clr );
int nC = 255-nR;
int nM = 255-nG;
int nY = 255-nB;
COLORREF clrCMY = RGB( nC, nM, nY );
	return clrCMY;
}

COLORREF CExtPaintManager::stat_CMYtoRGB( COLORREF clr )
{
int nC = GetRValue( clr );
int nM = GetGValue( clr );
int nY = GetBValue( clr );
int nR = 255 - nC;
int nG = 255 - nM;
int nB = 255 - nY;
COLORREF clrRGB = RGB( nR, nG, nB );
	return clrRGB;
}

//----------------------------------------------------------------------
// Conversion between the HSL (Hue, Saturation, and Luminosity) 
// and RBG color model.
//----------------------------------------------------------------------
// The conversion algorithms presented here come from the book by 
// Fundamentals of Interactive Computer Graphics by Foley and van Dam. 
// In the example code, HSL values are represented as floating point 
// number in the range 0 to 1. RGB tridrants use the Windows convention 
// of 0 to 255 of each element. 
//----------------------------------------------------------------------
double CExtPaintManager::stat_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;
}

COLORREF CExtPaintManager::stat_HLStoRGB( 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 = stat_HuetoRGB(m1,m2,H+1.0/3.0);
		g = stat_HuetoRGB(m1,m2,H);
		b = stat_HuetoRGB(m1,m2,H-1.0/3.0);
	}
	return RGB((BYTE)(r*255),(BYTE)(g*255),(BYTE)(b*255));
}

void CExtPaintManager::stat_RGBtoHSL( COLORREF rgb, double *H, double *S, double *L )
{   
double delta;
double r = (double)GetRValue(rgb)/255;
double g = (double)GetGValue(rgb)/255;
double b = (double)GetBValue(rgb)/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;  
	}
}

BYTE CExtPaintManager::stat_GetRGBFromHue(float rm1, float rm2, float rh)
{
	if (rh > 360.0f)
		rh -= 360.0f;
	else if (rh < 0.0f)
		rh += 360.0f;
	if (rh <  60.0f)
		rm1 = rm1 + (rm2 - rm1) * rh / 60.0f;   
	else if (rh < 180.0f)
		rm1 = rm2;
	else if (rh < 240.0f)
		rm1 = rm1 + (rm2 - rm1) * (240.0f - rh) / 60.0f;      
	return static_cast<BYTE>(rm1 * 255);
}

COLORREF CExtPaintManager::stat_GetRGBFromHLSExtend( double H, double L, double S )
{
WORD R, G, B;
	if( S == 0.0 )
	{
		R = G = B = unsigned char(L * 255.0);
	}
	else
	{
		float rm1, rm2;
		
		if( L <= 0.5f )
			rm2 = (float)(L + L * S);
		else
			rm2 = (float)(L + S - L * S);
		
		rm1 = (float)(2.0f * L - rm2);
		R = stat_GetRGBFromHue(rm1, rm2, (float)(H + 120.0f));
		G = stat_GetRGBFromHue(rm1, rm2, (float)(H));
		B = stat_GetRGBFromHue(rm1, rm2, (float)(H - 120.0f));
	}
	return RGB(R, G, B);
}

COLORREF CExtPaintManager::stat_HLS_Adjust(
	COLORREF clr,
	double percentH, // = 0.0
	double percentL, // = 0.0
	double percentS  // = 0.0
	)
{
double H = 0.0, L = 0.0, S = 0.0;
	stat_RGBtoHSL( clr, &H, &S, &L );

    if( percentH > 0.0 )
        H =
			H +
			(1.0 - H) * percentH
			;
    else if ( percentH < 0.0 )
        H =
			H * (1.0 + percentH);
	if( H < 0.0 )
		H = 0.0;
	else if( H > 1.0 )
		H = 1.0;

    if( percentL > 0.0 )
        L =
			L +
			(1.0 - L) * percentL
			;
    else if ( percentL < 0.0 )
        L =
			L * (1.0 + percentL);
	if( L < 0.0 )
		L = 0.0;
	else if( L > 1.0 )
		L = 1.0;

    if ( percentS > 0.0 )
        S =
			S +
			(1.0 - S) * percentS
			;
    else if ( percentS < 0.0 )
        S =
			S * (1.0 + percentS);
	if( S < 0.0 )
		S = 0.0;
	else if( S > 1.0 )
		S = 1.0;

    return stat_HLStoRGB( H, L, S );
}

BOOL CExtPaintManager::stat_PaintParentBk(
	HWND hWnd,
	HDC hDC,
	LPCRECT rectClip // = NULL
	)
{
	ASSERT( hDC != NULL );
	ASSERT( ::IsWindow(hWnd) );
	if( rectClip != NULL )
	{
		CRgn rgn;
		rgn.CreateRectRgnIndirect( rectClip );
		::SelectClipRgn( hDC, (HRGN)rgn.GetSafeHandle() );
	}
HWND hWndParent = ::GetParent( hWnd );
	ASSERT( ::IsWindow(hWndParent) );
CPoint pt( 0, 0 );
	::MapWindowPoints(hWnd,hWndParent,&pt,1);
	VERIFY(
		::OffsetWindowOrgEx(
			hDC,
			pt.x,
			pt.y,
			&pt
			)
		);
LRESULT lRes =
		::SendMessage(
			hWndParent,
			WM_ERASEBKGND,
			(WPARAM)hDC,
			0
			);
	VERIFY(
		::SetWindowOrgEx(
			hDC,
			pt.x,
			pt.y,
			&pt
			)
		);
	::SelectClipRgn( hDC, NULL );
	return (BOOL)lRes;
}

bool CExtPaintManager::stat_DefIsHwndNeedsDirectRepaint(
	HWND hWndChild
	)
{
DWORD dwChildStyle =
		DWORD( GetWindowLong(hWndChild,GWL_STYLE) );
	if( (dwChildStyle & WS_CHILD) == 0 )
		return true;
CString sClassName;
	::GetClassName(
		hWndChild,
		sClassName.GetBuffer( _MAX_PATH+1 ),
		_MAX_PATH
		);
	sClassName.ReleaseBuffer();
	sClassName.MakeLower();
	if( sClassName == _T("systabcontrol32") )
		return true;
	if( sClassName == _T("button")
		&& ( dwChildStyle & (BS_GROUPBOX|BS_CHECKBOX) ) != 0
		)
		return true;
	return false;
}

bool CExtPaintManager::stat_DefExcludeChildAreaCallback(
	HDC hDC,
	HWND hWnd,
	HWND hWndChild,
	LPVOID pCookie
	)
{
	ASSERT( hDC != NULL );
	ASSERT( hWnd != NULL );
	ASSERT( IsWindow(hWnd) );
	ASSERT( hWndChild != NULL );
	ASSERT( IsWindow(hWndChild) );
	hDC;
	hWnd;
	hWndChild;
	pCookie;
	return stat_DefIsHwndNeedsDirectRepaint( hWndChild );
}

int CExtPaintManager::stat_GetBPP()
{
//	return 4; // <-- test
//	return 8; // <-- test
CWindowDC dc_desktop(NULL);
int nBitsPerPixel =
		dc_desktop.GetDeviceCaps(BITSPIXEL);
	return nBitsPerPixel;
}

CSize CExtPaintManager::GetTextSizes(bool bBold /*= false*/)
{
CWindowDC dc (NULL);
CFont * pOldFont =
		dc.SelectObject(
			bBold ? &m_FontBold : &m_FontNormal
			);
	ASSERT( pOldFont != NULL );
TEXTMETRIC tm;

⌨️ 快捷键说明

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