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

📄 sxbutton.cpp

📁 PW芯片方案Flash ROM烧写程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//---------------------------------------------------------------------------
// Pixelworks Inc. Company Confidential Strictly Private
//
// $Archive: /PIXWARE/SwTools/GUIBuilder/SXBUTTON.CPP $
// $Revision: 1.1.1.1 $
// $Author: KevinM $
// $Date: 2003/09/29 18:19:04 $
//
// --------------------------------------------------------------------------
// >>>>>>>>>>>>>>>>>>>>>>>>> COPYRIGHT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// --------------------------------------------------------------------------
// Copyright 1999-2001(c) Pixelworks Inc.
//
// Pixelworks owns the sole copyright to this software. Under international 
// copyright laws you (1) may not make a copy of this software except for 
// the purposes of maintaining a single archive copy, (2) may not derive
// works herefrom, (3) may not distribute this work to others. These rights 
// are provided for information clarification, other restrictions of rights 
// may apply as well.
//
// This is an unpublished work.
// --------------------------------------------------------------------------
// >>>>>>>>>>>>>>>>>>>>>>>>>>>> WARRANTEE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// --------------------------------------------------------------------------
// Pixelworks Inc. MAKES NO WARRANTY OF ANY KIND WITH REGARD TO THE USE OF
// THIS SOFTWARE, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
// PURPOSE.
// --------------------------------------------------------------------------
//
// The GUIBuilder application has been designed and coded by Jim Becker.
//
/* $Log: SXBUTTON.CPP,v $
/* Revision 1.1.1.1  2003/09/29 18:19:04  KevinM
/* DEV Initial Checkin
/*
/* Revision 1.1.1.1  2002/11/07 22:07:31  mikeh
/* BUILD Initial creation of project
/*
/* Revision 1.1.1.1  2002/10/15 21:00:48  mikeh
/* DEV Initial creation of SDK 2.2 SPC Release
/*
/* Revision 1.1.1.1  2002/03/12 20:45:19  mikeh
/* no message
/*
/* Revision 1.2  2001/08/23 17:38:45  kevinm
/* CR5402 - Fixed pathname default bug
/*
/* Revision 1.1  2001/07/30 23:52:22  kevinm
/* Added self-branding logo
/*
/* Revision 1.1.1.1  2001/07/09 17:28:00  jimb
/*
/* initial checkin
/*
 * 
 * 2     11/05/99 3:17p Jimb
 * Sweep checkin for Comdex demo
 * 
 * 4     6/14/99 5:50p Jimb
 * Sweep checkin with more 1.8 changes. And changes to every header so log
 * directive works as expected.
 * 
 * 3     6/10/99 6:23p Jimb
 * Sweep checkin flush of everything for 1.8 release.
*/



#include "stdafx.h"

//  these behavior values are internal so we can play with them
//  and not recompile the world.

#define FRAME_BORDER	    0       // XXX changing this upsets things right now.
#define	SELECT_SLOP		    4
#define	LINE_WIDTH		    2
#define ARROW_SIZE          5
#define PLACEHOLDER         -999
#define OFFSET              20
#define MOFFSET             30

#define DEF_BORDER_LEFT     20
#define DEF_BORDER_RIGHT    80
#define DEF_BORDER_TOP      20
#define DEF_BORDER_BOTTOM   80

#define DEF_NORMAL_LEFT     55
#define DEF_NORMAL_RIGHT    75
#define DEF_NORMAL_TOP      55
#define DEF_NORMAL_BOTTOM   75

#define DEF_SELECT_LEFT     25
#define DEF_SELECT_RIGHT    45
#define DEF_SELECT_TOP      25
#define DEF_SELECT_BOTTOM   45
#define BULL_WIDTH          10
#define BULL_EYE            6

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

// if we show the boxes for the areas or not..
/*
BOOL CSXButton::m_bShowBoxes        = FALSE;
BOOL CSXButton::m_bShowNumbers      = FALSE;
BOOL CSXButton::m_bShowDimensions   = FALSE;
*/
#include "SXButton.h"

/////////////////////////////////////////////////////////////////////////////
// CSXButton

CSXButton::CSXButton()
{
	m_bColorTab = FALSE;

	m_hIcon = 0;
	m_hBitmap = 0;

	m_bDefault = FALSE;
	m_nOldState = 0;
	m_nOldAction = 0;

	m_nImageOffsetFromBorder = 4;
	m_nTextOffsetFromImage = 8;
	m_bUseOffset = TRUE;

	m_bFontRender	= FALSE;
	m_bFontExists	= FALSE;
	m_bFontHRender	= FALSE;
    m_bPlain 		= FALSE;
    m_bPlain 		= FALSE;

	m_bOwnBitmap	= TRUE;
	m_nOffset		= 1;

	m_bHotSpot		= FALSE;
	m_nHotX = m_nHotY = 0;

    m_bGraphic 		= FALSE;
    m_bEdit 		= FALSE;
    m_bAllowScale   = TRUE;
    m_bFitScale     = FALSE;

    m_nScale        = 1;

    m_pCurPen       = NULL;
    m_pOldPen       = NULL;
    m_pDC           = NULL;

    m_bCurLineInfoOk= FALSE;
    m_nLines        = 0;

    m_bIsCaptured 	    = FALSE;
    m_bIsCaptureLine    = FALSE;
    m_bShowLines 	    = FALSE;
    m_bShowBoxes        = FALSE;
    m_bShowNumbers      = FALSE;
    m_bShowDimensions   = FALSE;
    m_bShowMagnets      = FALSE;

	CRect	r(0,0,0,0);

	for( int i = 0; i < MAX_LINES; i++ )
		m_rectHandles[i]	= r;

	m_pWnd				= NULL;
	m_bWantBackground 	= FALSE;
	m_clrBackground		= 0;
}

CSXButton::~CSXButton()
{
    if (m_hBitmap && m_bOwnBitmap)
        DeleteObject(m_hBitmap);
}


BEGIN_MESSAGE_MAP(CSXButton, CButton)
	//{{AFX_MSG_MAP(CSXButton)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_ERASEBKGND()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


inline void CSXButton::Redraw()
{
	if( m_hWnd != NULL )
		Invalidate();
}

//
//	Various Attribute setting functions
//

//	Image functions
BOOL CSXButton::SetIcon( UINT nID, int nWidth, int nHeight )
{
	m_hIcon = (HICON)::LoadImage(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(nID), IMAGE_ICON, nWidth, nHeight, 0);

	if( m_hIcon == 0 )
		return FALSE;

	m_nImageWidth = nWidth;
	m_nImageHeight = nHeight;

	m_hBitmap = 0;

	Redraw();

	return TRUE;
}

BOOL CSXButton::SetBitmap( UINT nID, int nWidth, int nHeight )
{
	return SetBitmapCommon( nID, nWidth, nHeight, 0, FALSE /* no mask */ );
}

BOOL CSXButton::SetMaskedBitmap( UINT nID, int nWidth, int nHeight, COLORREF crTransparentMask )
{
	return SetBitmapCommon( nID, nWidth, nHeight, crTransparentMask, TRUE /* mask */ );
}

BOOL CSXButton::SetBitmapCommon( UINT nID, int nWidth, int nHeight, COLORREF crTransparentMask, BOOL bUseMask )
{
	// If it is not a masked bitmap, just go through the
	// motions as if it was, but set then number of color mappings to 0
	COLORMAP	mapColor;
	mapColor.from = crTransparentMask;
	mapColor.to  = ::GetSysColor( COLOR_BTNFACE );
	HBITMAP bmTemp = (HBITMAP)::CreateMappedBitmap(AfxGetApp()->m_hInstance, nID, IMAGE_BITMAP, &mapColor, bUseMask ? 1 : 0 );
	m_hBitmap = (HBITMAP)::CopyImage( bmTemp, IMAGE_BITMAP, nWidth, nHeight, 0 );

	// Create disabled bitmap.  We need to make the masked area white so
	// it will appear transparent when the bitmap is rendered as an
	// 'embossed' (disabled) image in DrawItem() below.  Since DrawState 
	// converts the image to monochrome, white is transparent, black is 
	// graphics data.
	mapColor.to  = RGB( 255, 255, 255 );
	bmTemp = (HBITMAP)::CreateMappedBitmap(AfxGetApp()->m_hInstance, nID, IMAGE_BITMAP, &mapColor, bUseMask ? 1 : 0 );
	m_hBitmapDisabled = (HBITMAP)::CopyImage( bmTemp, IMAGE_BITMAP, nWidth, nHeight, 0 );
  
	if( m_hBitmap == 0 || m_hBitmapDisabled == 0 )
		return FALSE; 

	m_nImageWidth = nWidth;
	m_nImageHeight = nHeight;

	m_hIcon = 0;

	Redraw();

	return TRUE;
}

BOOL CSXButton::SetBitmapFile( const CString& strFileName, BOOL bClearNew, BOOL bReadBMI )
{
	BITMAP	    bm;
    BOOL        bRet;

    if (m_hBitmap && m_bOwnBitmap)
        DeleteObject(m_hBitmap);
               
    // we haven't changed it yet! 
    m_bIsChanged = FALSE;

    m_hBitmap = (HBITMAP)LoadImage(AfxGetInstanceHandle(),(LPCTSTR)strFileName,
    				IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
	// late night hack
	if( m_hBitmap == NULL ) {
		// look in the media directory..

//		SetDir( dirPictures );
		char str[200];
		GetCurrentDirectory( 200, str );
		strcat( str, "\\" );
		strcat( str, (LPCSTR)strFileName);

        m_strLocalFileName    = str;

	    m_hBitmap = (HBITMAP)LoadImage(AfxGetInstanceHandle(),(LPCTSTR)str,
    				IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
	} else
        m_strLocalFileName = strFileName;

	if( m_hBitmap && bReadBMI ) {

	 	bRet= TRUE;
	} else
	 	bRet= FALSE;

	if( bRet ) {
        GetObject(m_hBitmap,sizeof(BITMAP),&bm);
		m_nImageWidth = bm.bmWidth;
		m_nImageHeight = bm.bmHeight;
	} else {
		m_nImageWidth = 0;
		m_nImageHeight = 0;
	}

    Redraw();

	return bRet;
}

void CSXButton::SetHBITMAP( const HBITMAP hBitmap, BOOL bOwner )
{
    if (m_hBitmap && m_bOwnBitmap )
        DeleteObject(m_hBitmap);

	m_hBitmap 	= hBitmap;
	m_bOwnBitmap= bOwner;

	if( m_hBitmap ) {
		BITMAP	bm;

        GetObject(m_hBitmap,sizeof(BITMAP),&bm);
				
		m_nImageWidth  = bm.bmWidth;
		m_nImageHeight = bm.bmHeight;

		Redraw();
	 }
}

void CSXButton::SetFontRender(  const CString& strFont, const CString& strText, 
							const COLORREF fore, const COLORREF back )
{
	 // because i'm currently only generating the bitmap when there's a DC
	 // available, we have to wait on it until paint time. so setup for 
	 // this event by storing all that's needed.
	 m_bFontRender	= TRUE;
	 m_bFontExists	= TRUE;

	 m_strFont		= strFont;
	 m_strText		= strText;
	 m_clrFore		= fore;
	 m_clrBack		= back;

     if (m_hBitmap && m_bOwnBitmap)
        DeleteObject(m_hBitmap);

	 m_hBitmap = NULL;

	 Redraw();
}

void CSXButton::SetColorTab(COLORREF crTab)
{	
	m_bColorTab = TRUE;
	m_crColorTab = crTab;

	Redraw();
}

BOOL CSXButton::SetDefaultButton( BOOL bState )
{
	CDialog *pDialog = (CDialog *)GetOwner();
	ASSERT( pDialog->IsKindOf( RUNTIME_CLASS( CDialog ) ) );

	pDialog->SetDefID( GetDlgCtrlID() );

	BOOL bPrevious = m_bDefault;
	m_bDefault = bState;

	Redraw();

	// Return previous state
	return bPrevious;
}

//	Positioning Functions
int CSXButton::SetImageOffset( int nPixels )
{
	int nPrevious = m_nImageOffsetFromBorder;

	m_bUseOffset = TRUE; 
	m_nImageOffsetFromBorder = nPixels; 

	Redraw();

	return nPrevious;
}

int CSXButton::SetTextOffset( int nPixels ) 
{ 
	int nPrevious = m_nTextOffsetFromImage;

	m_bUseOffset = TRUE; 
	m_nTextOffsetFromImage = nPixels; 

	Redraw();

	return nPrevious;
}

CPoint CSXButton::SetImagePos( CPoint p ) 
{ 
	CPoint pointPrevious = m_pointImage;

	m_bUseOffset = FALSE; 
	m_pointImage = p; 

	Redraw();

	return pointPrevious;
}
CPoint CSXButton::SetTextPos( CPoint p ) 
{ 
	CPoint pointPrevious = m_pointText;

	m_bUseOffset = FALSE; 
	m_pointText = p; 

	Redraw();

	return pointPrevious;
}

//	Centering a point helper function
void CSXButton::CheckPointForCentering( CPoint &p, int nWidth, int nHeight )
{
	CRect	rectControl;
	GetClientRect( rectControl );

	if( p.x == SXBUTTON_CENTER )
		p.x = ( ( rectControl.Width() - nWidth ) >> 1 );
	if( p.y == SXBUTTON_CENTER )
		p.y = ( ( rectControl.Height() - nHeight ) >> 1 );
}


//
//	Owner Draw function, the grand-daddy
//
void CSXButton::DrawItem(LPDRAWITEMSTRUCT lpDIS) 
{	
    CMemDC      memDC(CDC::FromHandle(lpDIS->hDC));

    m_pDC       = &memDC;
    m_rcItem    = lpDIS->rcItem;

	if( m_bPlain ) {
		DrawPlainBitmap();
        m_pDC   = NULL;
		return;
	}

	if( m_bGraphic ) {
	    DrawGraphicBitmap();
        m_pDC   = NULL;
		return;
	}

	if( m_bFontRender ) {
		DrawFontText();
        m_pDC   = NULL;
		return;
	}

	if( m_bFontHRender ) {
		DrawFontHelper();
        m_pDC   = NULL;
		return;
	}

	CRect	rectControl( m_rcItem );
	
	UINT	nOffset = 0;			// For position adjustment of a pushed button
	UINT	nFrameStyle=0;
	BOOL	bDRAWFOCUSONLY = FALSE;	// Optimize a bit
	int		nStateFlag;				// Normal or Disabled
	HBITMAP	hBitmapToDraw;			// Normal or Disabled bitmap (not used if uses icon)
	
	UINT	nNewState = lpDIS->itemState;
	UINT	nNewAction = lpDIS->itemAction;
	
	if ( (nNewState & ODS_SELECTED) && m_nOffset == 0 ) {
        m_pDC   = NULL;
	    return; // hack to prevent flashing in font helper
    }

	//	Find out what state the control and set some drawing flags 
	//	according to the state.
	if ( nNewState & ODS_SELECTED)
	{
		nFrameStyle = DFCS_PUSHED;
		nOffset += m_nOffset;
	}

	if( nNewState & ODS_DISABLED )
	{
		nStateFlag = DSS_DISABLED;
		hBitmapToDraw = m_hBitmapDisabled;
	}
	else
	{
		nStateFlag = DSS_NORMAL;
		hBitmapToDraw = m_hBitmap;
	}

	// If only the focus is changing, don't redraw the whole control
	if (nNewAction == ODA_FOCUS )
		bDRAWFOCUSONLY = TRUE;

	// If this is the default button, deflate the control so everything 
	// we do below (icon, text, focus ) is adjusted properly
	if( m_bDefault )
		rectControl.DeflateRect( 1, 1 );

	if( !bDRAWFOCUSONLY )
	{
		//
		// Draw 'default button' rectangle
		//
		if( m_bDefault ) // Can't use ODS_DEFAULT w/owner draw!!
		{
			CPen *pOldPen = (CPen*)m_pDC->SelectStockObject(BLACK_PEN);
							
			m_pDC->Rectangle( &m_rcItem ); // don't use deflated rectangle
			m_pDC->SelectObject( pOldPen );
		}

		//
		//	Draw button frame
		//
		m_pDC->DrawFrameControl(&rectControl, DFC_BUTTON, DFCS_BUTTONPUSH | nFrameStyle);

		//
		//	Draw color tab
		//
		if (m_bColorTab)
		{
			CPen penTab; 
			
			#define COLORTABSIZE 8
			if( penTab.CreatePen( PS_SOLID, 1, m_crColorTab) )
			{
				CPen* pOldPen = m_pDC->SelectObject( &penTab );
							
				int nXOffset = rectControl.left+1 + nOffset;
				int nYOffset = rectControl.top+1 + nOffset;
				for (UINT nStep = 0; nStep < COLORTABSIZE; nStep++)
				{
					m_pDC->MoveTo( nXOffset, nYOffset + nStep );
					m_pDC->LineTo( nXOffset + (COLORTABSIZE-nStep)-1, nYOffset + nStep );			
				}
        
				m_pDC->SelectObject( pOldPen );
			}

⌨️ 快捷键说明

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