1. 结构定义
struct SColorAndName
{
SColorAndName()
{
ZeroMemory(this, sizeof(SColorAndName)); // Init Structure
};
SColorAndName(COLORREF crColor, LPCTSTR cpColor)
{
ZeroMemory(this, sizeof(SColorAndName)); // Init Structure
m_crColor = crColor; // Set Color RGB Value
strncpy(m_cColor, cpColor, CCB_MAX_COLOR_NAME); // Set Color Name
};
COLORREF m_crColor; // Actual Color RGB Value
char m_cColor[CCB_MAX_COLOR_NAME]; // Actual Name For Color
};
2. 初始化
SColorAndName ms_pColors[16] =
{
SColorAndName( RGB( 0x00, 0x00, 0x00 ), "Black" ),
SColorAndName( RGB( 0xFF, 0xFF, 0xFF ), "White" ),
SColorAndName( RGB( 0x80, 0x00, 0x00 ), "Maroon" ),
SColorAndName( RGB( 0x00, 0x80, 0x00 ), "Green" ),
SColorAndName( RGB( 0x80, 0x80, 0x00 ), "Olive" ),
SColorAndName( RGB( 0x00, 0x00, 0x80 ), "Navy" ),
SColorAndName( RGB( 0x80, 0x00, 0x80 ), "Purple" ),
SColorAndName( RGB( 0x00, 0x80, 0x80 ), "Teal" ),
SColorAndName( RGB( 0xC0, 0xC0, 0xC0 ), "Silver" ),
SColorAndName( RGB( 0x80, 0x80, 0x80 ), "Gray" ),
SColorAndName( RGB( 0xFF, 0x00, 0x00 ), "Red" ),
SColorAndName( RGB( 0x00, 0xFF, 0x00 ), "Lime" ),
SColorAndName( RGB( 0xFF, 0xFF, 0x00 ), "Yellow" ),
SColorAndName( RGB( 0x00, 0x00, 0xFF ), "Blue" ),
SColorAndName( RGB( 0xFF, 0x00, 0xFF ), "Fuchsia" ),
SColorAndName( RGB( 0x00, 0xFF, 0xFF ), "Aqua" )
};
3。加入ComboBox
void CColorPickerCB::Initialize()
{
int iAddedItem = -1;
ResetContent();
for (int iColor = 0; iColor < CCB_MAX_COLORS; iColor++) // For All Colors
{
iAddedItem = AddString( ms_pColors[iColor].m_cColor); // Set Color Name/Text
if (iAddedItem == CB_ERRSPACE) // If Not Added
break; // Stop
else // If Added Successfully
SetItemData(iAddedItem, ms_pColors[iColor].m_crColor); // Set Color RGB Value
}
}
**************************************************************
typedef struct _LIST_ITEM
{
CString strText;
UNIT nLeng;
BOOL bChecked;
void* vpItemData;
CBitmap * pBitmap;
//end added
_LIST_ITEM()
{
strText = _T("");
nLeng = 1;
bChecked = FALSE;
vpItemData = NULL;
pBitmap = NULL;
}
void RemoveBitmap()
{
if( pBitmap != NULL )
{
delete pBitmap;
pBitmap = NULL;
}
}
//end added
BOOL operator <(_LIST_ITEM other)
{
if( strText < other.strText )
return TRUE;
else
return FALSE;
}
} LIST_ITEM, *PLIST_ITEM;
list m_list;
list::iterator m_iter;
m_iter = m_list.begin();
advance( m_iter, nSelect );
m_strEdit = m_iter->strText;
LIST_ITEM item;
item.strText = lpszString;
m_list.push_back( item );
/****************************************************************/
通用的复合数据类型的赋值函数typedef struct _ArgOptionVal
{
//CString name;
TCHAR name[256];
UINT flags;
TCHAR *list_val[MAX_LIST_VAL];
} ArgOptionVal;
void struct_copy(void *p_struct1, /*结构指针1*/
void *p_struct2, /*结构指针2*/
unsigned int struct_size /*结构类型长度,可以用sizeof(STRUCT_TYPE)代入*/
)
{
int count=0;
char *p_char1,*p_char2;
p_char1=(char *)p_struct1;
p_char2=(char *)p_struct2;
while(count!=struct_size)
{
*p_char1=*p_char2;
p_char1++;
p_char2++;
count++;
}
}
/***************************
*** 只是浅COPY,不能用于复杂的结构CString等
/***************************/
int main()
{
ArgOptionVal oldAov, newAov;
sprintf(oldAov.name, "%s", "Hello");
//oldAov.name = "Hello";
oldAov.flags = 5;
for(int i=0; i {
oldAov.list_val[i] = new TCHAR[128];
sprintf(oldAov.list_val[i], "%s_%d", "Test", i);
//newAov.list_val[i] = new TCHAR[128];
}
//struct_copy(&newAov, &oldAov, sizeof(ArgOptionVal));
structCopy(&newAov, &oldAov, sizeof(ArgOptionVal));
printf("Name = %s,Flag = %d\n", newAov.name, newAov.flags);
for(int i=0; i {
if(oldAov.list_val[i])
{
delete oldAov.list_val[i];
oldAov.list_val[i] = NULL;
newAov.list_val[i] = NULL;
}
}
return 0;
}
若结构中包含CString,析构时就会出现异常,可能与CString的有一个"写时复制"和"引用计数"的概念.CString类的用途很广,这样有可能在系统内部产生大量的CString临时对象.这时为了优化效率,就采用在系统软件内部广泛使用的"写时复制"概念.即当从一个CString产生另一个CString并不复制它的字符缓
冲区内容,而只是将字符缓冲区的"引用计数"加1.当需要改写字符缓冲区内的内容时,才分配内存,并复制内容.释放时,在"引用计数"出错.
因此用它时要注意:
1。STRUCT中不能有复杂的变量
2。它是通过指针实现浅COPY