線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1852
推到 Plurk!
推到 Facebook!

幾個有關ListView的疑問

尚未結案
aredfish
一般會員


發表:47
回覆:41
積分:17
註冊:2002-12-04

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-01-06 19:15:19 IP:210.241.xxx.xxx 未訂閱
大家好,請問一下各位大大有關ListView的幾個問題: 1. 如果我要新增一個 "列" 的資料那我會用 ListItem = ListView1->Items->Add();那如果要增加一個SubItem 那我應該要怎麼做呢?? 2. 如果我要把資料存到某一個特定的SubItem裡面的話,那我應該要 怎麼做呢?? 3. 我用ListView1->Items->Item[1]->SubItems[0];來取出特定 的SubItem裡面的資料,可是我用ShowMessage()都會編譯錯誤 我想是因為型別錯誤的關係,請問一下大家我應該要如何修改呢?? 4. 如果我想要修改特定某一列的顏色,那我應該要怎麼修改呢??
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-01-06 19:52:51 IP:61.218.xxx.xxx 未訂閱
ListView with Color Items     
ListViewColors.cpp     #include 
#pragma hdrstop    #include "ListViewColors.h"
//---------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;    const int dX = 10;
const int NUMBER_LINES = 10;    TListColumn *Column1;
TListColumn *Column2;
TListColumn *Column3;    TListItem   *ListItem;
TListView   *ListView;    String S[] = {"I see trees of green, red roses too",
              "I see them bloom for me and you",
              "And I think to myself, what a wonderful world",
              "I see skies of blue and clouds of white",
              " The bright blessed day, the dark sacred night",
              " And I think to myself, what a wonderful world",
              "The colours of the rainbow, so pretty in the sky",
              "Are also on the faces of people going by",
              "I see friends shakin' hands, sayin 'How do you do?'",
              "They're really saying 'I love you'"};    TColor Colors[] = { clYellow, clWhite,           (TColor)0xefabc1,
                    clAqua,   (TColor)0x676feda,  clBlue,
                    clRed,    (TColor)0x654321,  (TColor)0xcceabc,
                    clBlack };                        
//-----------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    randomize();
    Form1->Color = clPurple;
    Position = poScreenCenter;        //Create a TListView Component
    ListView = new TListView( Form1 );
    ListView->Parent = Form1;
    ListView->Top    = 10;
    ListView->Height = 500;
    ListView->Width  = 600;
    ListView->Left   = 10;
    ListView->Align  = alNone;
    ListView->Color = clGray;
    ListView->Cursor      = crHandPoint;
    ListView->MultiSelect = true;
    ListView->Checkboxes  = false;
    ListView->SortType    = stNone; 
    ListView->GridLines   = false;        // Select the Report View Style
    ListView->ViewStyle   = vsReport;        //------------------------------------------------
    // Write code in an OnDrawItem handler to manually
    // draw items in the list view. This event occurs
    // only if OwnerDraw is true.
    ListView->OwnerDraw  = true;
    ListView->OnDrawItem = ListViewDrawItem;
    //------------------------------------------------        if (ListView->ViewStyle == vsReport)
    {
        // Add Column
        Column1 = ListView->Columns->Add();
        Column1->Caption = "Column 1";
        Column1->Width   = 350;            // Add Column
        Column2 = ListView->Columns->Add();
        Column2->Caption = "Column 2";
        Column2->Width   = 100;            // Add Column
        Column3 = ListView->Columns->Add();
        Column3->Caption = "Column 3";
        Column3->Width   = 100;
    }        // Add Items:
    for (int i = 0; i < NUMBER_LINES; i  )
    {
        ListItem = ListView->Items->Add();
        ListItem->Caption = S[i];
    }
}
//------------------------------------------------------    void __fastcall TForm1::ListViewDrawItem(TCustomListView *Sender,
                                         TListItem *Item,
                                         const TRect &Rect,
                                         TOwnerDrawState State)
{
    //  The OwnerDraw property must be set to true        ListView->Canvas->FillRect(Rect);        for (int i=0; i < NUMBER_LINES; i  )
    {
        if (Item->Index == i)
        {
            ListView->Canvas->Font->Color = Colors[rand()];
            ListView->Canvas->TextOut(Rect.Left   dX, Rect.Top, S[i]);            }
    }
}    ListViewColors.h     #ifndef ListViewColorsH
#define ListViewColorsH
//----------------------------------------------------------------------
#include 
#include 
#include 
#include <Forms.hpp>
#include 
#include 
#include 
//----------------------------------------------------------------------    class TForm1 : public TForm
{
__published:        // IDE-managed Components            void __fastcall ListViewDrawItem( TCustomListView *Sender,
                                          TListItem *Item,
                                          const TRect &Rect,
                                          TOwnerDrawState State );    private:        // User declarations
public:                // User declarations
        __fastcall TForm1(TComponent* Owner);    };
//----------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//----------------------------------------------------------------------
#endif    
/*生活是一種藝術,用心生活才能享受生活*/
aredfish
一般會員


發表:47
回覆:41
積分:17
註冊:2002-12-04

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-01-07 10:23:22 IP:210.241.xxx.xxx 未訂閱
謝謝您的回答,不過我對於這個範例有幾個疑問: 1. 其中ListView->OnDrawItem = ListViewDrawItem; 是不是說我必須要在 OnDrawItem裡面,寫下ListViewDrawItem這個函式?? 2. 在ListViewDrawItem函式中的TextOut(Rect.Left, Rect.Top, S[i]); 裡面的前兩項引數(X,Y)我應該要怎麼設呢?? 因為我如果如上的程式 下去編譯的話會出現Structure required on left side of.or.* 的訊息,但我不了解這是什麼意思以及應該如何修改!! 謝謝大家!!
aredfish
一般會員


發表:47
回覆:41
積分:17
註冊:2002-12-04

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-01-14 17:11:30 IP:220.130.xxx.xxx 未訂閱
大家好,我嘗試了一下上面的範例,發現一個問題, 想請問一下大家 "ListView是否無法對單獨的列進行顏色修改的動作" 謝謝大家!!!
HomeSound
中階會員


發表:44
回覆:178
積分:94
註冊:2002-08-31

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-01-24 22:59:26 IP:61.30.xxx.xxx 未訂閱
改變ListView背景.字型.顏色
void __fastcall TSimu::ListViewCustomDrawItem(TCustomListView *Sender,
      TListItem *Item, TCustomDrawState State, bool &DefaultDraw)
{
  ((TListView*)Sender)->Canvas->Brush->Color = clRed;   //第一列的背景顏色
  ((TListView*)Sender)->Canvas->Font->Name = "新細明體";//第一列的字型及其顏色
  ((TListView*)Sender)->Canvas->Font->Color = clWhite;
}
//---------------------------------------------------------------------------
void __fastcall TSimu::ListViewCustomDrawSubItem(TCustomListView *Sender,
      TListItem *Item, int SubItem, TCustomDrawState State,
      bool &DefaultDraw)
{ if(SubItem == 1)
  { ((TListView*)Sender)->Canvas->Brush->Color = clBlue;  //第二列的背景顏色
    ((TListView*)Sender)->Canvas->Font->Name = "新細明體";//第二列的字型及其顏色
    ((TListView*)Sender)->Canvas->Font->Color = clWhite;
  }
  else if(SubItem == 2)
  { ((TListView*)Sender)->Canvas->Brush->Color = clBlack;//第三列的背景顏色
    ((TListView*)Sender)->Canvas->Font->Name = "標楷體"; //第三列的字型及其顏色
    ((TListView*)Sender)->Canvas->Font->Color = clWhite;
  }
  else if(SubItem == 3)
  { ((TListView*)Sender)->Canvas->Brush->Color = clPurple;//第四列的背景顏色
    ((TListView*)Sender)->Canvas->Font->Name = "標楷體";  //第四列的字型及其顏色
    ((TListView*)Sender)->Canvas->Font->Color = clFuchsia;
  }
}
希望是您要的 --==多看.多學.多聽==--
------
--==多看.多學.多聽==--
系統時間:2024-05-20 23:38:53
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!