程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> ListBox自畫的另一種效果

ListBox自畫的另一種效果

編輯:關於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);
}
//---------------------------------------------------------------------------

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved