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

📄 skinwin.cpp

📁 换肤窗口也就是窗口内的基本元素及窗口外观会随着要求不断的变换风格。本实例中的窗口可以实现NeoStyle和XpStyle两种风格的互换。教你如何定制窗口框架、标题、边框等
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// SkinWin.cpp: implementation of the CSkinWin class.
//
//		A class to enable draw windows-blind style window
//
//  ToDo:
//		how to popup system menu by my program??
//
//	History:
//		2002.11.24
//				CAN WORK NOW.			
//		2002.11.23	intial version
//
//
//		
//	AUthor:
//		szhao00@mails.tsinghua.edu.cn
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "SkinWin.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSkinWin::CSkinWin()
{
	m_bInit = FALSE;
	m_winstate = 0;

	m_barcolor = RGB(207,207,207);
	m_menucolor = RGB(207,207,207);
}

CSkinWin::~CSkinWin()
{

}

CString GetPathName( const char * filename );
CString GetFileName( const char * filename, int ext = 0);
char *next_token( char *buf, char *token, char *stopchars );

COLORREF ReadColor( CString section, CString key, CString file, COLORREF defcolor )
{
	char buf[1000];
	GetPrivateProfileString( section, key, "", buf, 1000, file );
	if ( *buf )
	{
		char token[255];
		char *p = buf;
		int r, g, b;
		p = next_token( p, token, NULL );
		r = atoi(token);
		p = next_token( p, token, NULL );
		g = atoi(token);
		p = next_token( p, token, NULL );
		b = atoi(token);
		return RGB(r, g, b );			
	}
	else
		return defcolor;
}

BOOL CSkinWin::LoadSkin( const char * skinfile )
{
	static const char * ps = "Personality";
	char buf[1000];
	CString path = GetPathName( skinfile );
	
	if ( m_bInit )
	{
		m_bmpDlg.DeleteObject();
		m_bmpTitle.DeleteObject();
		m_bmpLeft.DeleteObject();
		m_bmpRight.DeleteObject();
		m_bmpBottom.DeleteObject();
		m_bmpMaxBtn.DeleteObject();
		m_bmpRestoreBtn.DeleteObject();
		m_bmpMinBtn.DeleteObject();
		m_bmpCloseBtn.DeleteObject();

		//set deafult
		m_barcolor = RGB(207,207,207);
		m_menucolor = RGB(207,207,207);

		m_bInit = FALSE;
	}

	GetPrivateProfileString( ps, "DialogBmp", "", buf, 1000, skinfile );
	if ( *buf != 0 )
		m_bmpDlg.LoadBitmap( path + "/" + GetFileName( buf,1 ));
		

	GetPrivateProfileString( ps, "Top", "", buf, 1000, skinfile );
	if ( *buf == 0 || !m_bmpTitle.LoadBitmap( path + "/" + GetFileName( buf,1 )) )
		return FALSE;
	GetPrivateProfileString( ps, "Left", "", buf, 1000, skinfile );
	if ( *buf == 0 || !m_bmpLeft.LoadBitmap( path + "/" + GetFileName( buf,1 ) )) 
		return FALSE;
	GetPrivateProfileString( ps, "Right", "", buf, 1000, skinfile );
	if ( *buf == 0 || !m_bmpRight.LoadBitmap( path + "/" + GetFileName( buf,1 ) )) 
		return FALSE;
	GetPrivateProfileString( ps, "Bottom", "", buf, 1000, skinfile );
	if ( *buf == 0 || !m_bmpBottom.LoadBitmap( path + "/" + GetFileName( buf,1 ) )) 
		return FALSE;

	m_TitleHeight = m_bmpTitle.Height()/2;
	m_BorderLeftWidth = m_bmpLeft.Width()/2;
	m_BorderRightWidth = m_bmpRight.Width()/2;
	m_BorderBottomHeight = m_bmpBottom.Height()/2;

	m_titleoff1 = GetPrivateProfileInt( ps, "TopTopHeight", 0, skinfile );
	m_titleoff2 = m_bmpTitle.Width() - GetPrivateProfileInt( ps, "TopBotHeight", 0, skinfile );
	if ( m_titleoff2 <= m_titleoff1 )
		m_titleoff2 = m_titleoff1 + 1;
	m_leftoff1 = GetPrivateProfileInt( ps, "LeftTopHeight", 0, skinfile );
	m_leftoff2 = m_bmpLeft.Height() - GetPrivateProfileInt( ps, "LeftBotHeight", 0, skinfile );
	if ( m_leftoff2 <= m_leftoff1 )
		m_leftoff2 = m_leftoff1 + 1;

	m_rightoff1 = GetPrivateProfileInt( ps, "RightTopHeight", 0, skinfile );
	m_rightoff2 = m_bmpRight.Height() - GetPrivateProfileInt( ps, "RightBotHeight", 0, skinfile );
	if ( m_rightoff2 <= m_rightoff1 )
		m_rightoff2 = m_rightoff1 + 1;

	m_bottomoff1 = GetPrivateProfileInt( ps, "BottomTopHeight", 0, skinfile );
	m_bottomoff2 = m_bmpBottom.Width() - GetPrivateProfileInt( ps, "BottomBotHeight", 0, skinfile );
	if ( m_bottomoff2 <= m_bottomoff1 )
		m_bottomoff2 = m_bottomoff1 + 1;

	//load buttons
	int count = GetPrivateProfileInt( ps, "ButtonCount", 0, skinfile );
	int icount = GetPrivateProfileInt( ps, "ButtonImgCount", 3, skinfile );
	for ( int i = 0; i < count; i++ )
	{
		CString sec;
		sec.Format( "Button%d", i );
		GetPrivateProfileString( sec, "ButtonImage", "", buf, 1000, skinfile );
		int action = GetPrivateProfileInt( sec, "Action", 0,  skinfile );
		int x = GetPrivateProfileInt( sec, "XCoord", 0, skinfile );
		int y = GetPrivateProfileInt( sec, "YCoord", 0, skinfile );

		int state = icount;
		if ( action == 0 )
		{
			//close
			if ( !m_bmpCloseBtn.LoadBitmap( path + "/" + GetFileName( buf,1 )) ) 
				return FALSE;
			//state = m_bmpCloseBtn.Width()/m_bmpCloseBtn.Height();
			m_rectCloseBtn = CRect( x-m_bmpCloseBtn.Width()/state, y, x , y + m_bmpCloseBtn.Height() );
		}
		if ( action == 2 )
		{
			//min
			if ( !m_bmpMinBtn.LoadBitmap( path + "/" + GetFileName( buf,1 )) ) 
				return FALSE;
			//state = m_bmpMinBtn.Width()/m_bmpMinBtn.Height();
			m_rectMinBtn = CRect( x-m_bmpMinBtn.Width()/state, y, x, y + m_bmpMinBtn.Height() );
		}
		if ( action == 1 )
		{			
			if ( !m_bmpMaxBtn.GetSafeHandle() )
			{
				//max
				if ( !m_bmpMaxBtn.LoadBitmap( path + "/" + GetFileName( buf,1 )) ) 
					return FALSE;
				//state = m_bmpMaxBtn.Width()/m_bmpMaxBtn.Height();
				m_rectMaxBtn = CRect( x-m_bmpMaxBtn.Width()/state, y, x , y + m_bmpMaxBtn.Height() );
			}
			else
			{
				//restore
				if ( !m_bmpRestoreBtn.LoadBitmap( path + "/" + GetFileName( buf,1 )) ) 
					return FALSE;
				//state = m_bmpRestoreBtn.Width()/m_bmpRestoreBtn.Height();
				m_rectRestoreBtn = CRect( x-m_bmpRestoreBtn.Width()/state, y, x , y + m_bmpRestoreBtn.Height() );
			}
		}
		if ( action == 4 )
		{
		}
	}
	m_textShift = GetPrivateProfileInt( ps, "TextShift", 0, skinfile );
	m_textShiftVer = GetPrivateProfileInt( ps, "TextShiftVert", 0, skinfile );

	char * colours = "Colours";
	m_colTitle1 = ReadColor(colours, "TitleText", skinfile, GetSysColor(COLOR_CAPTIONTEXT) );
	m_colTitle2 = ReadColor(colours, "InactiveTitleText", skinfile, GetSysColor(COLOR_CAPTIONTEXT) );
	
	m_bTrans = GetPrivateProfileInt( ps, "UsesTran", 0, skinfile );
	if ( m_bTrans )
		m_colTrans = ReadColor(colours, "TransColor", skinfile, RGB(255,0,255) );


	m_barcolor = ReadColor(colours, "BarColor", skinfile, RGB(207,207,207) );
	m_menucolor = ReadColor(colours, "MenuColor", skinfile, RGB(207,207,207) );

	m_btnbgcolor = ReadColor(colours, "ButtonFace", skinfile, GetSysColor(COLOR_BTNFACE) );
	m_btntextcolor = ReadColor(colours, "ButtonText", skinfile, GetSysColor(COLOR_BTNTEXT));
	m_btnhovercolor = ReadColor(colours, "ButtonHilight", skinfile, GetSysColor(COLOR_BTNFACE) );
	m_btnfocuscolor = ReadColor(colours, "ButtonFocus", skinfile, GetSysColor(COLOR_BTNFACE) );
	

	m_bInit = TRUE;
	return TRUE;
}

BOOL CSkinWin::DrawTitle(CDC *pDC, int x, int y, int w, int state)
{
	int padding; 
	int ox = x;
	padding = ( w - m_bmpTitle.Width() )/( m_titleoff2 - m_titleoff1 ) + 1 ;
	if ( padding < 0 ) padding = 0;

	RECT sr;
	if ( state == 0 )
		sr = CRect( 0, 0, m_titleoff1, m_TitleHeight );
	else
		sr = CRect( 0, m_TitleHeight, m_titleoff1, m_bmpTitle.Height()  );	
	m_bmpTitle.Draw( pDC, x, y, &sr );
	 
	x += m_titleoff1;
	if ( state == 0 )
		sr = CRect(  m_titleoff1, 0, m_titleoff2, m_TitleHeight );
	else
		sr = CRect(  m_titleoff1, m_TitleHeight, m_titleoff2, m_bmpTitle.Height()  );	

	for ( int i = 0; i <= padding; i++, x += m_titleoff2 - m_titleoff1 )
	{
		int d = ( x + m_titleoff2 - m_titleoff1 - ox - w);
		if ( d > 0 )
			sr.right = sr.right - d;
		m_bmpTitle.Draw( pDC, x, y, &sr );
	}

	x = ox + w - ( m_bmpTitle.Width() - m_titleoff2 ) + 1 ;
	if ( state == 0 )
		sr = CRect(  m_titleoff2, 0, m_bmpTitle.Width()-1, m_TitleHeight);
	else
		sr = CRect(  m_titleoff2, m_TitleHeight, m_bmpTitle.Width()-1, m_bmpTitle.Height()  );	
	m_bmpTitle.Draw( pDC, x, y, &sr );
	return TRUE;
}

BOOL CSkinWin::DrawBottom(CDC *pDC, int x, int y, int w, int state)
{
	int padding; 
	int ox = x;
	padding = ( w - m_bmpBottom.Width() )/( m_bottomoff2 - m_bottomoff1 ) + 1 ;
	if ( padding < 0 ) padding = 0;

	RECT sr;
	if ( state == 0 )
		sr = CRect( 0, 0, m_bottomoff1, m_BorderBottomHeight);
	else
		sr = CRect( 0, m_BorderBottomHeight, m_bottomoff1, m_bmpBottom.Height()  );	
	m_bmpBottom.Draw( pDC, x, y, &sr );
	
	x += m_bottomoff1;
	if ( state == 0 )
		sr = CRect(  m_bottomoff1, 0, m_bottomoff2, m_BorderBottomHeight );
	else
		sr = CRect(  m_bottomoff1, m_BorderBottomHeight, m_bottomoff2, m_bmpBottom.Height() );	

	for ( int i = 0; i <= padding; i++, x += m_bottomoff2 - m_bottomoff1 )
	{
		int d = ( x + m_bottomoff2 - m_bottomoff1 - ox - w);
		if ( d > 0 )
			sr.right = sr.right - d;
		m_bmpBottom.Draw( pDC, x, y, &sr );
	}

	x = ox + w - ( m_bmpBottom.Width() - m_bottomoff2 );
	if ( state == 0 )
		sr = CRect(  m_bottomoff2, 0, m_bmpBottom.Width()-1, m_BorderBottomHeight );
	else
		sr = CRect(  m_bottomoff2, m_BorderBottomHeight, m_bmpBottom.Width()-1, m_bmpBottom.Height()  );	
	m_bmpBottom.Draw( pDC, x, y, &sr );
	return TRUE;
}

BOOL CSkinWin::DrawLeft(CDC *pDC, int x, int y, int h, int state)
{
	int padding; 
	int oy = y;
	padding = ( h - m_bmpLeft.Height() )/( m_leftoff2 - m_leftoff1 ) + 1 ;
	if ( padding < 0 ) padding = 0;

	RECT sr;
	if ( state == 0 )
		sr = CRect( 0, 0, m_BorderLeftWidth, m_leftoff1 );
	else
		sr = CRect( m_BorderLeftWidth, 0, m_bmpLeft.Width(), m_leftoff1  );	
	m_bmpLeft.Draw( pDC, x, y, &sr );
	
	y += m_leftoff1;
	if ( state == 0 )
		sr = CRect(  0, m_leftoff1,  m_BorderLeftWidth, m_leftoff2 );
	else
		sr = CRect(  m_BorderLeftWidth, m_leftoff1, m_bmpLeft.Width(), m_leftoff2 );	

	for ( int i = 0; i <= padding; i++, y += m_leftoff2 - m_leftoff1 )
	{
		int d = ( y + m_leftoff2 - m_leftoff1 - oy - h);
		if ( d > 0 )
			sr.bottom = sr.bottom - d;
		m_bmpLeft.Draw( pDC, x, y, &sr );
	}

	y = oy + h - ( m_bmpLeft.Height() - m_leftoff2 ) ;
	if ( state == 0 )
		sr = CRect(  0, m_leftoff2, m_BorderLeftWidth, m_bmpLeft.Height());
	else
		sr = CRect(  m_BorderLeftWidth, m_leftoff2,  m_bmpLeft.Width(), m_bmpLeft.Height()  );	
	m_bmpLeft.Draw( pDC, x, y, &sr );

	return TRUE;
}

BOOL CSkinWin::DrawRight(CDC *pDC, int x, int y, int h, int state)
{
	int padding; 
	int oy = y;
	padding = ( h - m_bmpRight.Height() )/( m_rightoff2 - m_rightoff1 ) + 1 ;
	if ( padding < 0 ) padding = 0;

	RECT sr;
	if ( state == 0 )
		sr = CRect( 0, 0, m_BorderRightWidth, m_rightoff1 );
	else
		sr = CRect( m_BorderRightWidth, 0, m_bmpRight.Width(), m_rightoff1  );	
	m_bmpRight.Draw( pDC, x, y, &sr );
	
	y += m_rightoff1;
	if ( state == 0 )
		sr = CRect(  0, m_rightoff1,  m_BorderRightWidth, m_rightoff2 );
	else
		sr = CRect(  m_BorderRightWidth, m_rightoff1, m_bmpRight.Width(), m_rightoff2 );	

	for ( int i = 0; i <= padding; i++, y += m_rightoff2 - m_rightoff1 )
	{
		int d = ( y + m_rightoff2 - m_rightoff1 - oy - h);
		if ( d > 0 )
			sr.bottom = sr.bottom - d;
		m_bmpRight.Draw( pDC, x, y, &sr );
	}

	y = oy + h - ( m_bmpRight.Height() - m_rightoff2 ) ;
	if ( state == 0 )
		sr = CRect(  0, m_rightoff2, m_BorderRightWidth, m_bmpRight.Height());
	else
		sr = CRect(  m_BorderRightWidth, m_rightoff2,  m_bmpRight.Width(), m_bmpRight.Height()  );	
	m_bmpRight.Draw( pDC, x, y, &sr );

	return TRUE;

}

BOOL CSkinWin::DrawFrame(CDC *pDC, int x, int y, int w, int h, int state, int title )
{
	if ( title )
		DrawTitle( pDC, x + m_BorderLeftWidth , y, 
   			w - m_BorderRightWidth - m_BorderLeftWidth + 1, state );
	DrawLeft( pDC, x, y, h, state );
	DrawRight( pDC, x + w - m_BorderRightWidth , y, h, state );
	DrawBottom( pDC, x + m_BorderLeftWidth, 
		y + h - m_BorderBottomHeight, w - m_BorderRightWidth - m_BorderLeftWidth, state );

⌨️ 快捷键说明

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