我有两个关于C++Builder的问题?

jianlinlong 2001-12-02 09:07:35
1、怎样用组合框(TListBox)显示位图,比如组合框下拉时显示的是一系图像。
2、如何改变TProgressBar的背景色?
...全文
106 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wyb_45 2001-12-03
  • 打赏
  • 举报
回复
在.cpp加入头文件
#include <registry.hpp>

#if (__BORLANDC__ >= 0x530)
#pragma package(smart_init)
#endif
wyb_45 2001-12-03
  • 打赏
  • 举报
回复
class TMainForm : public TForm
{
__published: // IDE-managed Components
TListBox *ListBox1;
TLabel *Label1;
TComboBox *ComboBox1;
TLabel *Label2;
TLabel *Label3;
TListBox *ListBox2;
void __fastcall ListBox1DrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State);
void __fastcall FormCreate(TObject *Sender);
void __fastcall ComboBox1DrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State);
void __fastcall ListBox2MeasureItem(TWinControl *Control,
int Index, int &Height);
void __fastcall ListBox2DrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State);
private: // User declarations
public: // User declarations
__fastcall TMainForm(TComponent* Owner);
};
wyb_45 2001-12-03
  • 打赏
  • 举报
回复
我这里有一段代码。
加两个ListBox1、ComboBox1
//.cpp
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
// Get the location of the Images\Buttons directory
TRegistry* reg = new TRegistry;
reg->RootKey = HKEY_LOCAL_MACHINE;
bool res = reg->OpenKey("SOFTWARE\\Borland\\Borland Shared\\Image Files", false);
if (!res) {
ShowMessage("Registry key not found");
delete reg;
return;
}
String imageDir = reg->ReadString("RootDir") + "\\buttons";
String fileSpec = imageDir + "\\*.bmp";
delete reg;
// Find the files in the Images\Buttons directory.
WIN32_FIND_DATA fd;
HANDLE h = FindFirstFile(fileSpec.c_str(), &fd);
if (h == INVALID_HANDLE_VALUE) {
ShowMessage("No files found");
return;
}
String S = String(fd.cFileName)
+ "=" + imageDir + "\\" + String(fd.cFileName);
ListBox1->Items->Add(S);
ComboBox1->Items->Add(S);
bool done = false;
do {
bool res = FindNextFile(h, &fd);
if (res) {
// Build a string using the TStringList NAME=VALUE mechanism
S = String(fd.cFileName)
+ "=" + imageDir + "\\" + String(fd.cFileName);
ListBox1->Items->Add(S);
ComboBox1->Items->Add(S);
}
done = !res;
} while (!done);
// Close the find handle.
FindClose(h);
// Fill the variable-height list box with values
ListBox2->Items->Add("Height: 20");
ListBox2->Items->Add("Height: 40");
ListBox2->Items->Add("Height: 15");
ListBox2->Items->Add("Height: 30");
ListBox2->Items->Add("Height: 25");
}

void __fastcall TMainForm::ListBox1DrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State)
{
// Get the name of the file. This will be used in the list box display.
String name = ListBox1->Items->Names[Index];

String filename = ListBox1->Items->Values[name];

TColor backColor;
TColor textColor;

// Set up the colors used to draw the background and the text.
// If the item is selected then use the Windows selection colors.
if (State.Contains(odSelected)) {
backColor = (TColor)GetSysColor(COLOR_HIGHLIGHT);
textColor = (TColor)GetSysColor(COLOR_HIGHLIGHTTEXT);
}
else {
backColor = (TColor)GetSysColor(COLOR_WINDOW);
textColor = (TColor)GetSysColor(COLOR_WINDOWTEXT);
}
// Bold the odd-numbered items just to illustrate.
if (Index % 2 == 1)
ListBox1->Canvas->Font->Style = TFontStyles() << fsBold;
else
ListBox1->Canvas->Font->Style = TFontStyles();

// Fill the drawing rect with the background color.
ListBox1->Canvas->Brush->Color = backColor;
ListBox1->Canvas->FillRect(Rect);

// Get the image.
Graphics::TBitmap* bm = new Graphics::TBitmap;
bm->LoadFromFile(filename);

TRect src;
src.Left = 0;
src.Top = 0;
src.Right = bm->Height;
src.Bottom = bm->Height;

TRect dst;
dst.Left = 5;
dst.Top = Rect.Top + 1;
dst.Right = dst.Left + bm->Height;
dst.Bottom = Rect.Top + bm->Height;
ListBox1->Canvas->BrushCopy(dst, bm, src, bm->TransparentColor);
delete bm;

ListBox1->Canvas->Pen->Color = textColor;
Rect.Left = dst.Right + 5;
DrawText(ListBox1->Canvas->Handle,
name.c_str(), -1, (RECT*)&Rect, DT_SINGLELINE | DT_VCENTER);
return;
}

void __fastcall TMainForm::ComboBox1DrawItem(TWinControl *Control,
int Index, TRect &Rect, TOwnerDrawState State)
{
String name = ComboBox1->Items->Names[Index];
String filename = ComboBox1->Items->Values[name];
TColor backColor;
TColor textColor;
if (State.Contains(odSelected)) {
backColor = (TColor)GetSysColor(COLOR_HIGHLIGHT);
textColor = (TColor)GetSysColor(COLOR_HIGHLIGHTTEXT);
}
else {
backColor = (TColor)GetSysColor(COLOR_WINDOW);
textColor = (TColor)GetSysColor(COLOR_WINDOWTEXT);
}
if (Index % 2 == 1)
ComboBox1->Canvas->Font->Style = TFontStyles() << fsBold;
else
ComboBox1->Canvas->Font->Style = TFontStyles();
ComboBox1->Canvas->Brush->Color = backColor;
ComboBox1->Canvas->FillRect(Rect);
Graphics::TBitmap* bm = new Graphics::TBitmap;
bm->LoadFromFile(filename);
TRect src;
src.Left = 0;
src.Top = 0;
src.Right = bm->Height;
src.Bottom = bm->Height;
TRect dst;
dst.Left = Rect.Left + 2;
dst.Top = Rect.Top + 1;
dst.Right = dst.Left + bm->Height;
dst.Bottom = Rect.Top + bm->Height;
ComboBox1->Canvas->BrushCopy(dst, bm, src, bm->TransparentColor);
Rect.Left = bm->Height;
delete bm;
ComboBox1->Canvas->Pen->Color = textColor;
Rect.Left = dst.Right + 5;
DrawText(ComboBox1->Canvas->Handle,
name.c_str(), -1, (RECT*)&Rect, DT_SINGLELINE | DT_VCENTER);
return;
}

void __fastcall TMainForm::ListBox2MeasureItem(TWinControl *Control,
int Index, int &Height)
{
switch (Index) {
case 0 : Height = 20; break;
case 1 : Height = 40; break;
case 2 : Height = 15; break;
case 3 : Height = 30; break;
case 4 : Height = 25; break;
}
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::ListBox2DrawItem(TWinControl *Control,
int Index, TRect &Rect, TOwnerDrawState State)
{
TColor backColor;
TColor textColor;
if (State.Contains(odSelected)) {
backColor = (TColor)GetSysColor(COLOR_HIGHLIGHT);
textColor = (TColor)GetSysColor(COLOR_HIGHLIGHTTEXT);
}
else {
backColor = (TColor)GetSysColor(COLOR_WINDOW);
textColor = (TColor)GetSysColor(COLOR_WINDOWTEXT);
}
// Fill the drawing rect with the background color.
ListBox2->Canvas->Brush->Color = backColor;
ListBox2->Canvas->FillRect(Rect);
ListBox2->Canvas->Pen->Color = textColor;
DrawText(ListBox2->Canvas->Handle,
ListBox2->Items->Strings[Index].c_str(),
-1, (RECT*)&Rect, DT_SINGLELINE | DT_VCENTER);
return;
}
//.h
windindance 2001-12-02
  • 打赏
  • 举报
回复
1 在OnDrawItem事件中画。
bcboy 2001-12-02
  • 打赏
  • 举报
回复
1.lmd中有这个listbox。
2.samples页中有一个CGauge,变化无穷。
mengxianbao1521 2001-12-02
  • 打赏
  • 举报
回复
DBGrid在其设置静态列对象后,其每一个列对象TColum有一个ButtonStyle属性。把他设成cbsAuto就可以有组和框下拉菜单表了 对于TColum对象的设置是:
DBGrid1->Columns->Item[0]->pickList->Add("sdfsafd")//假如是0行

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧