Исходники
Статьи
Языки программирования
.NET Delphi Visual C++ Borland C++ Builder C/С++ и C# Базы Данных MySQL MSSQL Oracle PostgreSQL Interbase VisualFoxPro Веб-Мастеру PHP HTML Perl Java JavaScript Протоколы AJAX Технология Ajax Освоение Ajax Сети Беспроводные сети Локальные сети Сети хранения данных TCP/IP xDSL ATM Операционные системы Windows Linux Wap Книги и учебники
Скрипты
Магазин программиста
|
Как поместить битмап в List/ComboBox.Возможность размещать графику внутри ListBox-ов и ComboBox-ов улучшает вид Вашего приложения и позволяет сделать Ваш интерфейс легко узнаваемым. Итак, что для этого нужно:
//---------------------------------------------------------- #include <vcl\vcl.h> #pragma hdrstop #include <dir.h> #include "Unit1.h" //---------------------------------------------------------- #pragma resource "*.dfm" TForm1 *Form1; Graphics::TBitmap* bmp1; Graphics::TBitmap* bmp2; Graphics::TBitmap* bmp3; Graphics::TBitmap* bmp4; Graphics::TBitmap* bmp5; //---------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //---------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { using Graphics::TBitmap; chdir("c:\\cbuilder\\Images\\Icons"); bmp1 = new TBitmap; bmp1->LoadFromFile("tech16.bmp"); bmp2 = new TBitmap; bmp2->LoadFromFile("Skylin16.bmp"); bmp3 = new TBitmap; bmp3->LoadFromFile("chip16.bmp"); bmp4 = new TBitmap; bmp4->LoadFromFile("Handsh16.bmp"); bmp5 = new TBitmap; bmp5->LoadFromFile("Earth16.bmp"); ComboBox1->Items->AddObject("Bitmap1: Tech16", bmp1); ComboBox1->Items->AddObject("Bitmap2: Skylin16", bmp2); ComboBox1->Items->AddObject("Bitmap3: Chip16", bmp3); ComboBox1->Items->AddObject("Bitmap4: Handsh16", bmp4); ComboBox1->Items->AddObject("Bitmap5: Earth16", bmp5); ComboBox1->ItemIndex = 0; ListBox1->Items->AddObject("Bitmap1: Tech16", bmp1); ListBox1->Items->AddObject("Bitmap2: Skylin16", bmp2); ListBox1->Items->AddObject("Bitmap3: Chip16", bmp3); ListBox1->Items->AddObject("Bitmap4: Handsh16", bmp4); ListBox1->Items->AddObject("Bitmap5: Earth16", bmp5); } //---------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) { delete bmp1; delete bmp2; delete bmp3; delete bmp4; delete bmp5; } //---------------------------------------------------------- void __fastcall TForm1::ComboBox1MeasureItem(TWinControl *Control, int Index, int &Height) { Height = 20; } //---------------------------------------------------------- void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Index, TRect &Rect, TOwnerDrawState State) { using Graphics::TBitmap; TListBox *lb = dynamic_cast<TListBox*>(Control); TBitmap *Bitmap = dynamic_cast<TBitmap*>(lb->Items->Objects[Index]); int Offset(0); lb->Canvas->FillRect(Rect); if (Bitmap) { lb->Canvas->BrushCopy( Bounds(Rect.Left + 2, Rect.Top + 2, Bitmap->Width, Bitmap->Height), Bitmap, Bounds(0, 0, Bitmap->Width, Bitmap->Height), clRed ); Offset = Bitmap->Width + 8; }; /* отображаем текст */ lb->Canvas->TextOut(Rect.Left + Offset, Rect.Top, ListBox1-> Items->Strings[Index] ); } //---------------------------------------------------------- void __fastcall TForm1::ComboBox1DrawItem(TWinControl *Control, int Index, TRect &Rect, TOwnerDrawState State) { using Graphics::TBitmap; TComboBox *cb = dynamic_cast<TComboBox *>(Control); TBitmap *Bitmap= dynamic_cast<TBitmap*>(cb->Items->Objects[Index]); int Offset(0); cb->Canvas->FillRect(Rect); if (Bitmap) { cb->Canvas->BrushCopy( Bounds(Rect.Left + 2, Rect.Top + 2, Bitmap->Width, Bitmap->Height), Bitmap, Bounds(0, 0, Bitmap->Width, Bitmap->Height), clRed ); Offset = Bitmap->Width + 8; }; /* отображаем текст */ cb->Canvas->TextOut( Rect.Left + Offset, Rect.Top, ComboBox1->Items->Strings[Index] ); } |
Форум Программиста
Новости Обзоры Магазин Программиста Каталог ссылок Поиск Добавить файл Обратная связь Рейтинги
|