本文示例源代码或素材下载
本文代码简单实现了类似CnPack中的一个界面效果,利用TListBox的自画。
演示图片://---------------------------------------------------------------------------
// ListBox自画的另一种效果
// by ccrun(老妖)
// info ccrun.com
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
// ListBox的风格,要自画必须选lbOwnerDrawFixed和lbOwnerDrawVariable
lbxMain->Style = lbOwnerDrawFixed;
// 去掉ListBox的边框,可有可无
lbxMain->Ctl3D = false;
// ListBox的每一项的高度
lbxMain->ItemHeight = 50;
pStrList = new TStringList;
// 往ListBox中添加些数据
for(int i=0; i<10; i++)
{
lbxMain->Items->Add("ListBox Items of " + String(i));
pStrList->Add("Second of " + String(i) + String((char)0x03) + "Third of " + String(i));
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::lbxMainDrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State)
{
// 填充的背景颜色
lbxMain->Canvas->Brush->Color = clWhite;
// 文字颜色
lbxMain->Canvas->Font->Color = clBlack;
// 填充背景
lbxMain->Canvas->FillRect (Rect) ;
// 圆角矩形的背景颜色
lbxMain->Canvas->Brush->Color = TColor(0x00FFF7F7);
// 圆角矩形的边框颜色
lbxMain->Canvas->Pen->Color = TColor(0x00131315);
// 画出圆角矩形
lbxMain->Canvas->RoundRect(Rect.Left + 3, Rect.Top + 3,
Rect.Right - 2, Rect.Bottom - 2, 8, 8);
// 以不同的宽度和高度再画一次,实现立体效果
lbxMain->Canvas->RoundRect(Rect.Left + 3, Rect.Top + 3,
Rect.Right - 3, Rect.Bottom - 3, 5, 5);
// 如果是当前选中项
if(State.Contains(odSelected))
{
// 选中项的背景颜色
lbxMain->Canvas->Brush->Color = TColor(0x00FFB2B5);
// 以不同的背景色画出选中项的圆角矩形
lbxMain->Canvas->RoundRect(Rect.Left + 3, Rect.Top + 3,
Rect.Right - 3, Rect.Bottom - 3, 5, 5);
// 选中项的文字颜色
lbxMain->Canvas->Font->Color = clBlue;
// 如果当前项拥有焦点
if(State.Contains(odFocused))
// 重画焦点虚框,实际上就是擦除了原先的焦点虚框
// 我看到CnPack的设置中好象没有去除那个框. ccrun注
::DrawFocusRect(lbxMain->Canvas->Handle, &Rect);
}
// 画出图标
ImageList1->Draw(lbxMain->Canvas, Rect.Left + 7,
Rect.top + (lbxMain->ItemHeight - ImageList1->Height)/2, Index, true);
// Item的第一行文字
lbxMain->Canvas->TextOutA(Rect.Left + 32 + 10, Rect.Top + 4,
lbxMain->Items->Strings[Index]);
String strTemp = pStrList->Strings[Index];
// Item的第二行文字
lbxMain->Canvas->TextOutA(Rect.Left + 32 + 10, Rect.Top + 18,
strTemp.SubString(1, strTemp.Pos((char)0x03) - 1).c_str());
// Item的第三行文字
lbxMain->Canvas->TextOutA(Rect.Left + 32 + 10, Rect.Top + 32,
strTemp.SubString(strTemp.Pos((char)0x0 3) + 1, strTemp.Length()).c_str());
}
//---------------------------------------------------------------------------
// 点击ListBox以后显示点击的项目
void __fastcall TMainForm::lbxMainClick(TObject *Sender)
{
pnlStatusBar->Caption = " " + lbxMain->Items->Strings[lbxMain->ItemIndex];
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormShow(TObject *Sender)
{
lbxMain->ItemIndex = 0;
lbxMain->Repaint();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormDestroy(TObject *Sender)
{
delete pStrList;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::lbxMainDblClick(TObject *Sender)
{
TEditForm *p = new TEditForm(MainForm);
p->edtFirst->Text = lbxMain->Items->Strings[lbxMain->ItemIndex];
String strTemp = pStrList->Strings[lbxMain->ItemIndex];
p->edtSecond->Text = strTemp.SubString(1, strTemp.Pos((char)0x03) - 1);
p->edtThird->Text = strTemp.SubString(strTemp.Pos((char)0x03) + 1, strTemp.Length());
p->pnlTitle->Caption = " 当前ListBox选中项:" + String(lbxMain->ItemIndex);
p->pnlTitle->Tag = lbxMain->ItemIndex;
p->Left = Left + (Width - p->Width) / 2;
p->Top = Top + (Height - p->Height) / 2;
p->ShowModal();
delete p;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::pnlTitleMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
// 移动没有标题栏的窗体
Refresh_PB_PB_PB_PB_PB_PB_PB();
if(Button == mbLeft)
{
ReleaseCapture();
Perform(WM_SYSCOMMAND, 0xF017, 0);
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btnMenuCloseClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btnMenuUpDownClick(TObject *Sender)
{
if(btnMenuUpDown->Caption == "6")
{
// 还原窗体
btnMenuUpDown->Caption = "5";
Height = 310;
}
else
{
// 上卷窗体
btnMenuUpDown->Caption = "6";
Height = 25;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::imgLogoClick(TObject *Sender)
{
// 打开 C++Builder研究 网站
ShellExecute(Handle, NULL, "http://www.ccrun.com",
NULL, NULL, SW_SHOWNORMAL);
}
//---------------------------------------------------------------------------
版权与免责声明
1、本站所发布的文章仅供技术交流参考,本站不主张将其做为决策的依据,浏览者可自愿选择采信与否,本站不对因采信这些信息所产生的任何问题负责。
2、本站部分文章来源于网络,其版权为原权利人所有。由于来源之故,有的文章未能获得作者姓名,署“未知”或“佚名”。对于这些文章,有知悉作者姓名的请告知本站,以便及时署名。如果作者要求删除,我们将予以删除。除此之外本站不再承担其它责任。
3、本站部分文章来源于本站原创,本站拥有所有权利。
4、如对本站发布的信息有异议,请联系我们,经本站确认后,将在三个工作日内做出修改或删除处理。
请参阅权责声明!