欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

colorlistbox.cpp

UsbPort打印例子usbprint的开发的源代码
CPP
字号:
// ColorListBox.cpp : implementation file
//

#include "stdafx.h"
#include "ami.h"
#include "ColorListBox.h"

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

/////////////////////////////////////////////////////////////////////////////
// CColorListBox

CColorListBox::CColorListBox()
{
   m_bInitialized = false;
}

CColorListBox::~CColorListBox()
{
}


BEGIN_MESSAGE_MAP(CColorListBox, CListBox)
	//{{AFX_MSG_MAP(CColorListBox)
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColorListBox message handlers

void CColorListBox::DrawItem(LPDRAWITEMSTRUCT pDIStruct) 
{
   static      CString  sColor;                       // No Need To Reallocate Each Time

   CDC         dcContext;
   CRect    rItemRect( pDIStruct -> rcItem );
   CRect    rBlockRect( rItemRect );
   CRect    rTextRect( rBlockRect );
   CBrush      brFrameBrush;
   int         iFourthWidth = 0;
   int         iItem = pDIStruct -> itemID;
   int         iAction = pDIStruct -> itemAction;
   int         iState = pDIStruct -> itemState;
   COLORREF crColor = 0;
   COLORREF crNormal = GetSysColor( COLOR_WINDOW );
   COLORREF crSelected = GetSysColor( COLOR_HIGHLIGHT );
   COLORREF crText = GetSysColor( COLOR_WINDOWTEXT );

   if( !dcContext.Attach( pDIStruct -> hDC ) )  // Attach CDC Object
      return;                                   // Stop If Attach Failed

   iFourthWidth = ( rBlockRect.Width() / 4 );            // Get 1/4 Of Item Area
   brFrameBrush.CreateStockObject( BLACK_BRUSH );        // Create Black Brush

   if( iState & ODS_SELECTED )                        // If Selected
   {                                         // Set Selected Attributes
      dcContext.SetTextColor(
            ( 0x00FFFFFF & ~( crText ) ) );           // Set Inverted Text Color (With Mask)
      dcContext.SetBkColor( crSelected );             // Set BG To Highlight Color
      dcContext.FillSolidRect( &rBlockRect, crSelected );   // Erase Item
   }
   else                                      // If Not Selected
   {                                         // Set Standard Attributes
      dcContext.SetTextColor( crText );               // Set Text Color
      dcContext.SetBkColor( crNormal );               // Set BG Color
      dcContext.FillSolidRect( &rBlockRect, crNormal );  // Erase Item
   }
   if( iState & ODS_FOCUS )                       // If Item Has The Focus
      dcContext.DrawFocusRect( &rItemRect );      // Draw Focus Rect

   //
   // Calculate Text Area
   //
   rTextRect.left += ( iFourthWidth + 2 );        // Set Start Of Text
   rTextRect.top += 2;                            // Offset A Bit

   //
   // Calculate Color Block Area
   //
   rBlockRect.DeflateRect( CSize( 2, 2 ) );        // Reduce Color Block Size
   rBlockRect.right = iFourthWidth;                // Set Width Of Color Block

   //
   // Draw Color Text And Block
   //
   if( iItem != -1 )                            // If Not An Empty Item
   {
      GetText( iItem, sColor );                     // Get Color Text
      if( iState & ODS_DISABLED )                     // If Disabled
      {
         crColor = GetSysColor( COLOR_INACTIVECAPTIONTEXT );
         dcContext.SetTextColor( crColor );           // Set Text Color
      }
      else                                   // If Normal
         crColor = GetItemData( iItem );              // Get Color Value

      dcContext.SetBkMode( TRANSPARENT );             // Transparent Background
      dcContext.TextOut( rTextRect.left, rTextRect.top,
            sColor );                           // Draw Color Name

      dcContext.FillSolidRect( &rBlockRect, crColor );   // Draw Color
            
      dcContext.FrameRect( &rBlockRect, &brFrameBrush ); // Draw Frame
   }
   dcContext.Detach();                             // Detach DC From Object
}

void CColorListBox::PreSubclassWindow() 
{	
   Initialize();                                // Initialize Contents
   
   CListBox::PreSubclassWindow();               // Subclass Control

   SetCurSel( 0 );                              // Select First Item By Default

   return;                                      // Done!
}

int CColorListBox::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CListBox::OnCreate(lpCreateStruct) == -1)
		return -1;
	
   Initialize();                                // Initialize Contents
   SetCurSel( 0 );                              // Select First Item By Default
	
	return 0;
}

void CColorListBox::Initialize( void )
{
   int      iAddedItem = -1;

   if( m_bInitialized )                         // If Already Initialized
      return;                                   // Return

   ResetContent();
/*
   AddColor("Black",     RGB(0x00, 0x00, 0x00));
   AddColor("Dark Grey", RGB( 0x40, 0x40, 0x40 ));
   AddColor("Grey",      RGB( 0x80, 0x80, 0x80 ));
   AddColor("Light Grey", RGB( 0xC0, 0xC0, 0xC0 ));
   AddColor("White",  RGB( 0xFF, 0xFF, 0xFF ));
   AddColor("Navy",   RGB( 0x00, 0x00, 0x80 ));
   AddColor("Blue",   RGB( 0x00, 0x00, 0xFF ));
   AddColor("Aqua",   RGB( 0x00, 0xDD, 0xDD ));
   AddColor("Cyan",   RGB( 0x00, 0xFF, 0xFF ));
   AddColor("Teal",   RGB( 0x40, 0x80, 0x80 ));
   AddColor("Dark Green", RGB( 0x00, 0x40, 0x00 ));
   AddColor("Green",   RGB( 0x00, 0x80, 0x00 ));
   AddColor("Lime",    RGB( 0x00, 0xFF, 0x00 ));
   AddColor("Olive" ,  RGB( 0x66, 0x66, 0x00 ));
   AddColor("Khaki",   RGB( 0xCC, 0xCC, 0x00 ));
   AddColor("Brown",   RGB( 0x80, 0x40, 0x40 ));
   AddColor("Purple",  RGB( 0x99, 0x66, 0xCC ));
   AddColor("Red",     RGB( 0xFF, 0x00, 0x00 ));
   AddColor("Magenta", RGB( 0xFF, 0x00, 0xFF ));
   AddColor("Maroon",  RGB( 0x80, 0x00, 0x00 ));
   AddColor("Fushcia", RGB( 0xFF, 0x00, 0xFF ));
   AddColor("Yellow",  RGB( 0xFF, 0xFF, 0x00 ));
   AddColor("Light Yellow", RGB( 0xFF, 0xFF, 0x40 ));
   AddColor("Pale Yellow",  RGB( 0xFF, 0xFF, 0x80 ));
*/
   m_bInitialized = true;                          // Set Initialized Flag
}

COLORREF CColorListBox::GetSelectedColorValue( void )
{
   int      iSelectedItem = GetCurSel();              // Get Selected Item

   if( iSelectedItem == CB_ERR )                   // If Nothing Selected
      return( RGB( 0, 0, 0 ) );                    // Return Black

   return( GetItemData( iSelectedItem ) );               // Return Selected Color
}


CString CColorListBox::GetSelectedColorName( void )
{
   int      iSelectedItem = GetCurSel();           // Get Selected Item

   if( iSelectedItem == CB_ERR )                   // If Nothing Selected
      return( m_sColorName = afxEmptyString );     // Return Nothing (Not "Black!")

   GetText( iSelectedItem, m_sColorName );          // Store Name Of Color

   return( m_sColorName );                         // Return Selected Color Name
}


void CColorListBox::SetSelectedColorValue( COLORREF crClr )
{
   int      iItems = GetCount();
   
   for( int iItem = 0; iItem < iItems; iItem++ )
   {
      if( crClr == GetItemData( iItem ) )         // If Match Found
         SetCurSel( iItem );                      // Select It
   }
   return;                                      // Done!
}


void CColorListBox::SetSelectedColorName( PCSTR cpColor )
{
   int      iItems = GetCount();
   CString  sColorName;

   for( int iItem = 0; iItem < iItems; iItem++ )
   {
      GetText( iItem, sColorName );                 // Get Color Name

      if( !sColorName.CompareNoCase( cpColor ) )   // If Match Found
         SetCurSel( iItem );                       // Select It
   }
   return;                                      // Done!
}


bool CColorListBox::RemoveColor( PCSTR cpColor )
{
   int      iItems = GetCount();
   CString  sColor;
   bool  bRemoved = false;

   for( int iItem = 0; iItem < iItems; iItem++ )
   {
      GetText( iItem, sColor );                     // Get Color Name
      if( !sColor.CompareNoCase( cpColor ) )          // If Match Found
      {
         DeleteString( iItem );                    // Remove It
         bRemoved = true;                       // Set Flag
         break;                                 // Stop Checking
      }
   }
   return( bRemoved );                             // Done!
}


bool CColorListBox::RemoveColor( COLORREF crClr )
{
   int      iItems = GetCount();
   bool  bRemoved = false;

   for( int iItem = 0; iItem < iItems; iItem++ )
   {
      if( crClr == GetItemData( iItem ) )       // If Desired Color Found
      {
         DeleteString( iItem );                 // Remove It
         bRemoved = true;                       // Set Flag
         break;                                 // Stop Checking
      }
   }
   return( bRemoved );                             // Done!
}


int CColorListBox::AddColor( PCSTR cpName, COLORREF crColor )
{
   int      iItem = -1;

   iItem = InsertString( -1, cpName );             // Insert String
   if( iItem != LB_ERR )                           // If Insert Good
      SetItemData( iItem, crColor );               // Set Color Value 

   return( iItem );                             // Done! Return Location
}


   

void CColorListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
{
	// TODO: Add your code to determine the size of specified item
	
}

⌨️ 快捷键说明

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