2011-11-30 40 views

回答

3

請問ListView是否適合您?這是我使用的。更容易,你可以使它看起來就像一個ListBox。此外,大量的MSDN文檔可供您開始使用。

如何爲Windows顯示圖標窗體ListView控件
Windows窗體ListView控件可以從三個圖像 列表中顯示的圖標。 List,Details和SmallIcon視圖顯示來自SmallImageList屬性中指定的 圖像列表中的圖像。 LargeIcon 視圖顯示來自 LargeImageList屬性中指定的圖像列表中的圖像。列表視圖還可以顯示一組額外的圖標,在StateImageList屬性中設置,在大圖標或小圖標旁邊設置。有關圖像列表的詳細信息,請參閱ImageList 組件(Windows窗體)和如何:使用 Windows窗體ImageList組件添加或刪除圖像。

How to: Display Icons for the Windows Forms ListView Control

+0

這也可以工作。我也使用PNG圖形格式。 – James

1

如果你被困在WinForms中,那麼你必須自己繪製你的物品。

請參閱DrawItem event的示例。

1

插入少許不同的方法 - 不要使用一個列表框。 而不是使用該控件將我限制在其有限的一組屬性和方法中,我正在製作一個我自己的列表框。

這並不難,因爲它的聲音:

爲myListBox事件處理程序
int yPos = 0;  
Panel myListBox = new Panel(); 
foreach (Object object in YourObjectList) 
{ 
    Panel line = new Panel(); 
    line.Location = new Point(0, Ypos); 
    line.Size = new Size(myListBox.Width, 20); 
    line.MouseClick += new MouseEventHandler(line_MouseClick); 
    myListBox.Controls.Add(line); 

    // Add and arrange the controls you want in the line 

    yPos += line.Height; 
} 

實施例 - 在選擇的行:

private void line_MouseClick(object sender, MouseEventArgs) 
{ 
    foreach (Control control in myListBox.Controls) 
     if (control is Panel) 
      if (control == sender) 
       control.BackColor = Color.DarkBlue; 
      else 
       control.BackColor = Color.Transparent;  
} 

的代碼示例以上未測試但是使用所描述的方法和發現非常方便和簡單。

0

如果您不想將ListBox更改爲ListView,則可以爲DrawItemEvent編寫一個處理函數。例如:

private void InitializeComponent() 
{ 
    ... 
    this.listBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox_DrawItem); 
    ... 
} 
private void listBox_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     if (e.Index == -1) 
      return; 
     // Draw the background of the ListBox control for each item. 
     e.DrawBackground(); 
     var rect = new Rectangle(e.Bounds.X+10, e.Bounds.Y+8, 12, 14); 
     //assuming the icon is already added to project resources 
     e.Graphics.DrawIconUnstretched(YourProject.Properties.Resources.YouIcon, rect); 
     e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), 
      e.Font, Brushes.Black, new Rectangle(e.Bounds.X + 25, e.Bounds.Y + 10, e.Bounds.Width, e.Bounds.Height), StringFormat.GenericDefault); 
     // If the ListBox has focus, draw a focus rectangle around the selected item. 
     e.DrawFocusRectangle(); 
    } 

你可以玩的矩形,設置圖標的位置正確

相關問題