导航
  • 主页
  • Windows SDK/API
  • 基础类
  • ActiveX
  • 数据库及相关技术
  • 网络及通讯开发
  • VCL组件使用和开发
  • 问答

发现一片文章寻求翻译 Delphi->c++ 200分赠送。除了翻译好贴代码在这里外,请发送工程到我的信箱。谢谢。

CPerlAsm_Lx 2003-11-16 02:08:52
文章地址 http://www.yesky.com/20010611/183846_1.shtml


我的信箱 forstu@21cn.com 谢谢。快来
...全文
47 点赞 收藏 11
写回复
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
CPerlAsm_Lx 2003-11-16
是说按图象原来的大小平铺吗?

是不是可以考虑宽度小于图象就裁减。高度然后怎么平铺,不知道有办法吗?
回复
CPerlAsm_Lx 2003-11-16
chifengwatch(chifengwatch)
可以把整体一个图片作为背景吗?
回复
chifengwatch 2003-11-16
//以上是全部代码
//delphi有些函数我不是很清楚比如Inc Dec 估计问题不大。
//有错误请指正
回复
chifengwatch 2003-11-16
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <FileCtrl.hpp>

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
ListBox1->Style = lbOwnerDrawVariable ;
ListBox3->Style = lbOwnerDrawVariable ;
ListBox4->Style = lbOwnerDrawVariable ;
ListBox5->Style = lbOwnerDrawVariable ;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
SendMessage (ListBox2->Handle,LB_SETHORIZONTALEXTENT,ListBox2->Width + 32,0) ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State)
{
TIcon *AIcon ;
TIcon *BIcon ;
try
{
AIcon = new TIcon();
BIcon = new TIcon();
//装入图标到AIcon, BIcon
ImageList1->GetIcon(0, AIcon);
ImageList1->GetIcon(1, BIcon);
//填充绘图区
ListBox1->Canvas->FillRect(Rect);
//判断ListBox1中的当前重绘项是否被选中,根据状态装入不同的图标

if ( State.Contains(odSelected) )
ListBox1->Canvas->Draw(Rect.Left, Rect.Top, AIcon);
else
ListBox1->Canvas->Draw(Rect.Left, Rect.Top, BIcon);
//输出文字
ListBox1->Canvas->TextOut(Rect.Left + AIcon->Width / 2, Rect.Top + 2, ListBox1->Items->Strings[Index]);
}
__finally
{
delete AIcon ;
delete BIcon ;
}
}
//---------------------------------------------------------------------------


void __fastcall TForm1::ListBox3DrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State)
{
TIcon *AIcon ;
int I, K ;
TRect ARect, BRect ;
int H ;
TBrushStyle AStyle;
try
{
//计算Item数量
I = ListBox3->Items->Count-1;
AIcon = new TIcon();
//装入图标
ImageList1->GetIcon(0, AIcon);
//填充区域
ListBox3->Canvas->FillRect(Rect);
//计算Rect绘图区的高度
H = Rect.Bottom - Rect.Top;
//如果当前项是Item的最后一项,则在Canvas上没有Item的空白区绘制背景
if ( Index == I )
{
K = 1;
ARect = Rect;
//如果当前绘图项的底部小于ListBox2的Canvas的底部,有空白区域
while ( ARect.Bottom < ListBox3->Canvas->ClipRect.Bottom )
{
//一次计算下一个绘图区域
ARect.Top = Rect.Top + K * H;
ARect.Bottom = ARect.Top + H;

ListBox3->Canvas->StretchDraw(ARect, Image1->Picture->Bitmap);
K++;
//Inc(K);
}
}
//绘制当前项
ListBox3->Canvas->StretchDraw(Rect, Image1->Picture->Bitmap);
//绘制图标
ListBox3->Canvas->Draw(Rect.Left, Rect.Top, AIcon);
ARect = Rect;
ARect.Left = Rect.Left + AIcon->Width / 2;
ARect.Top = ARect.Top + 2;
//保存当前画笔的风格
AStyle = ListBox3->Canvas->Brush->Style;
//当前选中的Item要填充蓝色背景
if (State.Contains(odSelected))
{
ListBox3->Canvas->Brush->Style = bsSolid;
ListBox3->Canvas->Brush->Color = clBlue;
}
else
{
//未选中项透明背景,前景色为黑色
ListBox3->Canvas->Brush->Style = bsClear;
ListBox3->Font->Color = clBlack;
}
//输出文字
ListBox3->Canvas->TextOut(ARect.Left, ARect.top, ListBox3->Items->Strings[Index]);
//恢复当前画笔的风格
ListBox3->Canvas->Brush->Style = AStyle;
}
__finally
{
delete AIcon ;
}
/*
  以上方法实现了TListBox即具有背景图片,又具有图标和透明文字效果,极大的改善了TListBox的显示效果。

  4. 具有图标,背景图片,透明文字及文字对齐方式效果的列表框

  要实现文字对齐效果,可通过Windows Api函数:DrawText实现。

  操作:

  将ListBox2的OnDrawItem事件中的代码复制到ListBox3的OnDrawItem事件中,并将复制代码中所有的ListBox2改为ListBox3。

  将上述修改后代码中的ListBox3.Canvas.TextOut(Rect.Left + AIcon.Width div 2, Rect.Top + 2, ListBox3.Items[Index]); 语句删除,并在该处添加以下语句:

  file://计算除掉图标所占区域后的区域,用于确定绘制文字的区域范围

ARect := Rect;
ARect.Left := Rect.Left + AIcon.Width div 2;
ARect.Top := ARect.top + 2;
file://Windows Api函数调用
DrawText(ListBox3.Canvas.Handle, PChar(ListBox3.Items[Index]), Length(ListBox3.Items[Index]), ARect, 0); file://0-左对齐, 1---居中, 2--右对齐

*/
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//Image1->Picture->Graphic=NULL;
TSearchRec Sr;
String Dir;

Dir = "";

if ( SelectDirectory("选择图片目录", "", Dir) )
{
ListBox5->Items->Clear();

//搜索该目录下的所有bmp文件
if ( FindFirst(Dir + "\\*.bmp", faReadOnly, Sr) == 0 )
{
ListBox5->Items->Add(Dir + "\\" + Sr.Name);
while ( FindNext(Sr) == 0 )
{
ListBox5->Items->Add(Dir + "\\" + Sr.Name);
}
FindClose(Sr);
}
}
SendMessage (ListBox5->Handle,LB_SETHORIZONTALEXTENT,ListBox5->Width + 32,0) ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox4MeasureItem(TWinControl *Control,
int Index, int &Height)
{
Height = 100 ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox4DrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State)
{
Graphics::TBitmap *ABmp = new Graphics::TBitmap();
try
{
ImageList2->GetBitmap(Index, ABmp);
ListBox4->Canvas->FillRect(Rect);
ListBox4->Canvas->Draw(Rect.Left, Rect.Top, ABmp);
}
__finally
{
delete ABmp ;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::ListBox5MeasureItem(TWinControl *Control,
int Index, int &Height)
{
Height = 100 ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox5DrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State)
{
//图片文件名
String Fn;
Graphics::TBitmap *ABmp = new Graphics::TBitmap();

try
{
Fn = ListBox5->Items->Strings[Index];
ABmp->LoadFromFile(ListBox5->Items->Strings[Index]);
Rect.Bottom--;
ListBox5->Canvas->FillRect(Rect);
ListBox5->Canvas->StretchDraw(Rect, ABmp);
}
__finally
{
delete ABmp;
}
}
//---------------------------------------------------------------------------
回复
CPerlAsm_Lx 2003-11-16
另,每一行的宽度怎么设置?好象字体都重复了。
回复
CPerlAsm_Lx 2003-11-16
为什么启动以后,LISTBOX不自动绘图呢?添加了项目才绘图的。奇怪。请给个解释?
回复
chifengwatch 2003-11-16
//我不是高手。不知道翻译的对不对
TIcon *AIcon ;
int I, K ;
TRect ARect, BRect ;
int H ;
TBrushStyle AStyle;
try
{
//计算Item数量
I = ListBox3->Items->Count-1;
AIcon = new TIcon();
//装入图标
ImageList1->GetIcon(0, AIcon);
//填充区域
ListBox3->Canvas->FillRect(Rect);
//计算Rect绘图区的高度
H = Rect.Bottom - Rect.Top;
//如果当前项是Item的最后一项,则在Canvas上没有Item的空白区绘制背景
if ( Index == I )
{
K = 1;
ARect = Rect;
//如果当前绘图项的底部小于ListBox2的Canvas的底部,有空白区域
while ( ARect.Bottom < ListBox3->Canvas->ClipRect.Bottom )
{
//一次计算下一个绘图区域
ARect.Top = Rect.Top + K * H;
ARect.Bottom = ARect.Top + H;

ListBox3->Canvas->StretchDraw(ARect, Image1->Picture->Bitmap);
K++;
//Inc(K);
}
}
//绘制当前项
ListBox3->Canvas->StretchDraw(Rect, Image1->Picture->Bitmap);
//绘制图标
ListBox3->Canvas->Draw(Rect.Left, Rect.Top, AIcon);
ARect = Rect;
ARect.Left = Rect.Left + AIcon->Width / 2;
ARect.Top = ARect.Top + 2;
//保存当前画笔的风格
AStyle = ListBox3->Canvas->Brush->Style;
//当前选中的Item要填充蓝色背景
if (State.Contains(odSelected))
{
ListBox3->Canvas->Brush->Style = bsSolid;
ListBox3->Canvas->Brush->Color = clBlue;
}
else
{
//未选中项透明背景,前景色为黑色
ListBox3->Canvas->Brush->Style = bsClear;
ListBox3->Font->Color = clBlack;
}
//输出文字
ListBox3->Canvas->TextOut(ARect.Left, ARect.top, ListBox3->Items->Strings[Index]);
//恢复当前画笔的风格
ListBox3->Canvas->Brush->Style = AStyle;
}
__finally
{
delete AIcon ;
}
回复
CPerlAsm_Lx 2003-11-16
希望高手过来翻译。
回复
CPerlAsm_Lx 2003-11-16
楼上的。你铁的这个只是部分简单的。

我希望翻译的是。设置背景。并且可以显示透文字(就是没有块状现象的)
回复
chifengwatch 2003-11-16
//这是CCRUN老妖网站上的例子

1。 为ListBox添加水平方向的滚动条

SendMessage (ListBox1->Handle,LB_SETHORIZONTALEXTENT,ListBox1->Width + 32,0) ;

2。 为Listbox添加图标

首先需要设置Listbox的Style属性为lbOwnerDrawVariable;

const margin = 2 ; // Margin around each bitmap

//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1MeasureItem(TWinControl *Control, int Index,
int &Height)
{
Height = ImageList1->Height + margin * 2 ;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1DrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State)
{
// 填充背景.
ListBox1->Canvas->FillRect (Rect) ;
// 画出图像.
ImageList1->Draw (ListBox1->Canvas, Rect.Left + margin, Rect.Top + margin, Index) ;

// 画出文字.
String text = ListBox1->Items->Strings [Index] ;
// Center the text vertically in relation to the bitmap.
int off = (Rect.Bottom - Rect.Top - ListBox1->Canvas->TextHeight (text)) / 2 ;
ListBox1->Canvas->TextOut (Rect.Left + ImageList1->Width + 2 * margin,
Rect.Top + off,text) ;
}


回复
chifengwatch 2003-11-16
看看
回复
发动态
发帖子
C++ Builder
创建于2007-08-02

1.3w+

社区成员

C++ Builder相关内容讨论区
申请成为版主
社区公告
暂无公告