ListBox.cpp
资源名称:VIS.zip [点击查看]
上传用户:sdgangtie
上传日期:2020-03-07
资源大小:7324k
文件大小:4k
源码类别:
数值算法/人工智能
开发平台:
Visual C++
- // MyListBox.cpp : implementation file
- //
- #include "stdafx.h"
- #include "VIS.h"
- #include "ListBox.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- CImageList g_Image_List;
- /////////////////////////////////////////////////////////////////////////////
- // MyListBox
- ListBox::ListBox()
- {
- BOOL bRet = FALSE;
- HICON hIcon = NULL;
- // Create image list
- bRet = g_Image_List.Create(16, 16, ILC_COLOR32 | ILC_MASK, 5, 1);
- ASSERT(bRet == TRUE);
- // Add some icons
- hIcon = AfxGetApp()->LoadIcon(IDI_ICON_LAB);
- g_Image_List.Add(hIcon);
- // Add some icons
- hIcon = AfxGetApp()->LoadIcon(IDI_ICON_LAB);
- g_Image_List.Add(hIcon);
- }
- ListBox::~ListBox()
- {
- }
- BEGIN_MESSAGE_MAP(ListBox, CListBox)
- //{{AFX_MSG_MAP(MyListBox)
- // NOTE - the ClassWizard will add and remove mapping macros here.
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // MyListBox message handlers
- void ListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
- {
- //CRect members to store the position of the items
- CRect rItem;
- CRect rText;
- CRect rIcon;
- CDC* dc = CDC::FromHandle(lpDrawItemStruct->hDC);
- if ((int)lpDrawItemStruct->itemID < 0)
- {
- // If there are no elements in the List Box
- // based on whether the list box has Focus or not
- // draw the Focus Rect or Erase it,
- if ((lpDrawItemStruct->itemAction & ODA_FOCUS) &&
- (lpDrawItemStruct->itemState & ODS_FOCUS))
- {
- dc->DrawFocusRect(&lpDrawItemStruct->rcItem);
- }
- else if ((lpDrawItemStruct->itemAction & ODA_FOCUS) &&
- !(lpDrawItemStruct->itemState & ODS_FOCUS))
- {
- dc->DrawFocusRect(&lpDrawItemStruct->rcItem);
- }
- return;
- }
- // String to store the text
- CString strText;
- // Get the item text.
- GetText(lpDrawItemStruct->itemID, strText);
- //Initialize the Item's row
- rItem = lpDrawItemStruct->rcItem;
- rIcon = lpDrawItemStruct->rcItem;
- rIcon.bottom = rIcon.top + 16;
- rIcon.right = rIcon.left + 16;
- //Start drawing the text 2 pixels after the icon
- rText.left = rIcon.right + 2;
- rText.top = rIcon.top;
- UINT nFormat = DT_LEFT | DT_SINGLELINE | DT_VCENTER;
- if (GetStyle() & LBS_USETABSTOPS)
- nFormat |= DT_EXPANDTABS;
- // If item selected, draw the highlight rectangle.
- // Or if item deselected, draw the rectangle using the window color.
- if ((lpDrawItemStruct->itemState & ODS_SELECTED) &&
- (lpDrawItemStruct->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
- {
- CBrush br(::GetSysColor(COLOR_HIGHLIGHT));
- dc->FillRect(&rItem, &br);
- }
- else if (!(lpDrawItemStruct->itemState & ODS_SELECTED) &&
- (lpDrawItemStruct->itemAction & ODA_SELECT))
- {
- CBrush br(::GetSysColor(COLOR_WINDOW));
- dc->FillRect(&rItem, &br);
- }
- // If the item has focus, draw the focus rect.
- // If the item does not have focus, erase the focus rect.
- if ((lpDrawItemStruct->itemAction & ODA_FOCUS) &&
- (lpDrawItemStruct->itemState & ODS_FOCUS))
- {
- dc->DrawFocusRect(&rItem);
- }
- else if ((lpDrawItemStruct->itemAction & ODA_FOCUS) &&
- !(lpDrawItemStruct->itemState & ODS_FOCUS))
- {
- dc->DrawFocusRect(&rItem);
- }
- // To draw the Text set the background mode to Transparent.
- int iBkMode = dc->SetBkMode(TRANSPARENT);
- COLORREF crText;
- if (lpDrawItemStruct->itemState & ODS_SELECTED)
- crText = dc->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
- else if (lpDrawItemStruct->itemState & ODS_DISABLED)
- crText = dc->SetTextColor(::GetSysColor(COLOR_GRAYTEXT));
- else
- crText = dc->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT));
- CPoint pt(rIcon.left,rIcon.top);
- g_Image_List.Draw(dc,0,pt,ILD_NORMAL);
- dc->TextOut(rText.left,rText.top,strText);
- //Draw the Text
- dc->SetTextColor(crText);
- }
- void ListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
- {
- // TODO: Add your code to determine the size of specified item
- lpMeasureItemStruct->itemHeight = 32;
- }