当前位置:七道奇文章资讯编程技术VC/C++编程
日期:2011-03-22 13:55:00  来源:本站整理

<b>ListBox自画的另一种效果</b>[VC/C++编程]

赞助商链接



  本文“<b>ListBox自画的另一种效果</b>[VC/C++编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

本文代码简单实现了近似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)0x03) + 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();
   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);
}
//---------------------------------------------------------------------------


  以上是“<b>ListBox自画的另一种效果</b>[VC/C++编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • <b>hosts是什么 hosts文件在什么位置 若何改正hosts</b>
  • <b>在 Windows 8 中手动安装语言包</b>
  • <b>五个常见 PHP数据库问题</b>
  • Windows中Alt键的12个高效快速的利用本领介绍
  • <b>MySQL ORDER BY 的实现解析</b>
  • <b>详解MySQL存储历程参数有三种范例(in、out、inout)</b>
  • <b>Win8系统恢复出来经典的开始菜单的办法</b>
  • <b>Win8系统花屏怎么办 Win8系统花屏的办理办法</b>
  • <b>Windows 7系统下无线网卡安装</b>
  • <b>为什么 Linux不需求碎片整理</b>
  • <b>Windows 8中删除账户的几种办法(图)</b>
  • <b>教你如安在win7下配置路由器</b>
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .