我参考其它程序做了一个自画的ComboBox, ComboBox的每项都包含一个图标和文字,现在的问题是:为什么下拉的列表框中图标和文字间的距离与选择完成后ComboBox框中图标和文字间的距离不一样,甚至图标大小都不一样,我实在找不出可以修改的地方。那位大虾帮个忙解决一下,不胜感激!void CColorPickerCB::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
{
GetLBText( 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
}
{
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
{
GetLBText( 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
}
使用CBEM_SETIMAGELIST设置imagelist,然后其条目的设置imagelist索引即可ComboBoxEx automatically uses CBS_OWNERDRAWFIXED
CBS_OWNERDRAWFIXED The owner of the list box is responsible for drawing its contents; the items in the list box are all the same height.