Files
AC_Pro/Component/ComboBoxComponent/ComboBoxComponent.cpp
T
2026-06-13 00:05:19 +08:00

242 lines
5.1 KiB
C++

//---------------------------------------------------------------------------
#pragma hdrstop
#include "ComboBoxComponent.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#include "DropListScrollBar.cpp"
#include "DropListView.cpp"
#include "CapButton.cpp"
static inline void ValidCtrCheck(TComboBoxComponent *)
{
new TComboBoxComponent(NULL);
}
namespace Comboboxcomponent
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TComboBoxComponent)};
RegisterComponents(L"Samples", classes, 0);
}
}
#define MIN(x, y) ((x)<(y)?(x):(y))
__fastcall TComboBoxComponent::TComboBoxComponent(TComponent *Owner)
: CBCapButton(Owner)
{
m_ClickStat = 0;
m_IsMouseOver = false;
ItemHeight = 20;
ShowItemCount = 5;
FShowMode = TComboBoxMode(cbShowDown);
// 初始化下拉列表框
m_DropListView = new TDropListView(this);
m_DropListView->Parent = (TWinControl *)Owner;
m_DropListView->SetDesignVisible(false);
m_DropListView->Visible = false;
m_DropListView->OnHide = DoDropListHide;
m_DropListView->OnShow = DoDropListShow;
OnMouseEnter = DoMouseEnter;
OnMouseLeave = DoMouseLeave;
}
__fastcall TComboBoxComponent::~TComboBoxComponent()
{
}
void __fastcall TComboBoxComponent::SetParent(TWinControl* AParent)
{
m_DropListView->Parent = AParent;
CBCapButton::Parent = AParent;
}
void __fastcall TComboBoxComponent::SetFontSize( int value )
{
m_DropListView->SetFontSize(value);
CBCapButton::SetFontSize(value);
}
void __fastcall TComboBoxComponent::SetFont( String name, int size )
{
CBCapButton::SetFont(name, size);
m_DropListView->SetFont(name, size);
}
TComboBoxItem * __fastcall TComboBoxComponent::AddItem(String Caption)
{
TComboBoxItem * pNewItem = new TComboBoxItem(Caption, DoItemClick);
m_DropListView->AddItem(pNewItem);
return pNewItem;
}
TComboBoxItem * __fastcall TComboBoxComponent::GetItem(int Index)
{
return m_DropListView->GetItem( Index );
}
void __fastcall TComboBoxComponent::Clear()
{
if(m_DropListView->ItemsCount != 0)
{
m_DropListView->Clear();
}
}
void __fastcall TComboBoxComponent::DoDropListShow(TObject *Sender)
{
Down = true;
m_ClickStat = 1;
}
void __fastcall TComboBoxComponent::DoDropListHide(TObject *Sender)
{
Down = false;
if(!m_IsMouseOver)
{
m_ClickStat = 2;
}
else
{
m_ClickStat = 3;
}
}
void __fastcall TComboBoxComponent::DoItemClick(TComboBoxItem* Sender, int Index)
{
Caption = Sender->Caption;
if(FOnChange) FOnChange(Sender, Index);
}
void __fastcall TComboBoxComponent::DoMouseEnter(TObject *Sender)
{
m_IsMouseOver = true;
}
void __fastcall TComboBoxComponent::DoMouseLeave(TObject *Sender)
{
m_IsMouseOver = false;
}
/*
void __fastcall TComboBoxComponent::CMMouseEnter(TMessage &Message)
{
m_IsMouseOver = true;
//TGraphicControl::Dispatch(&Message);
CBCapButton::DefaultHandler(&Message);
}
void __fastcall TComboBoxComponent::CMMouseLeave(TMessage &Message)
{
m_IsMouseOver = false;
//TGraphicControl::Dispatch(&Message);
}
*/
void __fastcall TComboBoxComponent::MouseDown(TMouseButton Button, TShiftState Shift, int X, int Y)
{
TGraphicControl::MouseDown(Button, Shift, X, Y);
if(Button != 0) return;
if(m_ClickStat == 3) return;
m_DropListView->ItemHeight = FItemHeight;
m_DropListView->Width = Width;
TPoint var_Client(0, Height);
TPoint var_Screen = ClientToScreen(var_Client);
m_DropListView->Left = var_Screen.X;
m_DropListView->ShowItemsCount = FShowItemCount;
if(ShowMode == TComboBoxMode(cbShowUp))
{
// 上方显示
m_DropListView->Top = var_Screen.Y - Height - m_DropListView->ListHeight;
}
else
{
// 下方显示
m_DropListView->Top = var_Screen.Y;
}
m_DropListView->Show();
}
void __fastcall TComboBoxComponent::MouseUp(TMouseButton Button, TShiftState Shift, int X, int Y)
{
TGraphicControl::MouseUp(Button, Shift, X, Y);
if(Button != 0) return;
if(m_ClickStat == 3) m_ClickStat = 0;
}
int __fastcall TComboBoxComponent::GetDefaultItem()
{
return m_DropListView->DefItemIndex;
}
int __fastcall TComboBoxComponent::GetItemsCount()
{
return m_DropListView->ItemsCount;
}
void __fastcall TComboBoxComponent::RemoveItem(int Index)
{
int var_Count = m_DropListView->ItemsCount;
if(Index < var_Count && Index >= 0)
{
m_DropListView->RemoveItem(Index);
}
}
void __fastcall TComboBoxComponent::SetDefaultItem(int Value)
{
int var_Count = m_DropListView->ItemsCount;
if(Value >= var_Count || Value < 0)
{
Value = -1;
Caption = "";
}
else if(var_Count > 0)
{
TComboBoxItem *var_Item = m_DropListView->GetItem(Value);
if(var_Item != NULL)
{
Caption = var_Item->Caption;
}
}
m_DropListView->DefItemIndex = Value;
}
void __fastcall TComboBoxComponent::SetItemHeight(int Value)
{
FItemHeight = Value;
}
void __fastcall TComboBoxComponent::SetShowMode(TComboBoxMode Value)
{
FShowMode = Value;
}
void __fastcall TComboBoxComponent::SetShowItemCount(int Value)
{
FShowItemCount = Value;
}