📄 ew_listbox.cpp
字号:
#include "EW_ListBox.h"
BEGIN_MESSAGE_MAP(EW_ListBox, EW_Widget)
ON_DRAW (EW_ListBox::OnDraw)
ON_KEYDOWN (EW_ListBox::OnKeyDown)
ON_MOUSE (EW_ListBox::OnMouse)
END_MESSAGE_MAP()
EW_ListBox::EW_ListBox(const EW_Rect & Rect , const WORD wLimit, WORD wID , const WORD wStyle)
:EW_Widget(Rect, wID, FS_NONE)
{
m_nItemHeight = 21;
m_wLimit = wLimit;
m_Rect = Rect;
m_nSelectedIndex = -1;
}
EW_ListBox::~EW_ListBox()
{
ITEM_STRING_LIST::iterator it = m_vItemList.begin();
for(; it != m_vItemList.end();it++)
{
delete[] *it;
}
m_vItemList.clear();
}
void EW_ListBox::OnDraw(EW_OSAdaptor * pAdaptor)
{
pAdaptor->BeginDraw(m_Rect);
//draw background
pAdaptor->FillRect(m_Rect ,RGB(168, 168, 168) , RGB(255, 255, 255));
//draw menu items
ITEM_STRING_LIST::iterator it = m_vItemList.begin();
int i = 0;
for (; it != m_vItemList.end(); it++,i++){
EW_Rect rect(m_Rect.wLeft + 1 , m_Rect.wTop + i *m_nItemHeight + 1,
m_Rect.wRight - 1, m_Rect.wTop + (i + 1) *m_nItemHeight);
if(m_nSelectedIndex == i){
//选择项
//画选择项的背景
pAdaptor->FillRect(rect, RGB(168, 168, 168) , RGB(0, 0, 168));
//画文字
pAdaptor->DrawText(*it , m_Rect.wLeft + 2, m_Rect.wTop + i * m_nItemHeight + 2 , RGB(255, 255, 255));
}else{
//非选择项
//画非选择项的背景
pAdaptor->FillRect(rect, RGB(168, 168, 168) , RGB(250, 250, 250));
pAdaptor->DrawText(*it , m_Rect.wLeft + 2, m_Rect.wTop + i * m_nItemHeight + 2 , RGB(0 , 0 , 0));
}//end if
}//end for
pAdaptor->EndDraw();
}
void EW_ListBox::AddItem(char *szItemText , int index)
{
int length = strlen(szItemText);
char *itemText = new char[length + 1];
strcpy(itemText , szItemText);
if(index == -1){
//在队列最后插入
m_vItemList.push_back(itemText);
}else{
//在索引处插入
ITEM_STRING_LIST::iterator it = m_vItemList.begin();
it += index;
m_vItemList.insert(it , itemText);
}
}
void EW_ListBox::DeleteItem(int index)
{
if(index < 0 || index >= m_vItemList.size())
return;
//删除此项
ITEM_STRING_LIST::iterator it = m_vItemList.begin();
it += index;
delete[] *it;
//列表中删除
m_vItemList.erase(it);
}
void EW_ListBox::MoveUp()
{
if(m_nSelectedIndex == -1)
return ;
//增加当前索引
m_nSelectedIndex--;
//重绘菜单
OnDraw(EW_GetAdaptor());
}
void EW_ListBox::MoveDown()
{
if(m_nSelectedIndex == m_vItemList.size() - 1)
return ;
//增加当前索引
m_nSelectedIndex++;
//重绘菜单
OnDraw(EW_GetAdaptor());
}
char * EW_ListBox::GetText(int nIndex)
{
if(nIndex < 0 || nIndex > m_vItemList.size() - 1)
return NULL;
//删除此项
ITEM_STRING_LIST::iterator it = m_vItemList.begin();
it += nIndex;
return *it;
}
int EW_ListBox::GetCurSel()
{
return m_nSelectedIndex;
}
void EW_ListBox::SetSelectedItem(int xPos , int yPos)
{
if (xPos >= m_Rect.wLeft + 2 && yPos >= m_Rect.wTop && xPos < m_Rect.wRight && yPos < m_Rect.wBottom - 2)
{
m_nSelectedIndex = (yPos - 2 - m_Rect.wTop) / m_nItemHeight;
if(m_nSelectedIndex >= m_vItemList.size())
m_nSelectedIndex = -1;
}else
m_nSelectedIndex = -1;
}
bool EW_ListBox::OnKeyDown(EW_Message * pMsg)
{
UGL_WCHAR key = pMsg->lData;
switch(key)
{
case FVK_UP:
MoveUp();
break;
case FVK_DOWN:
MoveDown();
break;
case FVK_ENTER:
{
m_nSelectedIndex = -1;
OnDraw(EW_GetAdaptor());
}
break;
default:
break;
}//end switch
return true;
}
bool EW_ListBox::OnMouse(EW_Message * pMsg)
{
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -