2011-10-28 35 views
1

我已經創建了自定義comboBox,支持項目文本前面的圖像。下面是它的外觀: ImageComboBox自定義ImageComboBox不總是以相同的方式

對於這一點,我創建了一個名爲ImageComboBox新的控制存儲在我的WinForms項目引用的dll
ImageComboBox不過是一個ComboBoxDrawMode設置爲DrawMode.OwnerDrawFixed和持有ImageList包含所有圖像繪製。有一個DrawItemEventHandler負責繪製圖像和每個項目的文字。

我遇到了一個兩像素的問題,但令人困惑的部分是問題並不總是發生。當我創建一個新的winforms項目並簡單地添加一個新的ImageComboBox時,我沒有問題。當我在winforms項目中添加一個新的ImageComboBox時,問題就會出現 - 10次中的9次(或類似的情況)。


這裏重現我的兩個像素爲題步驟:

  1. 當我打開窗體,一切都很好:
    First step
  2. 當我下拉imageComboBox,一切都很好:
    Second step
  3. 當我懸停一個項目來選擇它,一切都很好:
    Third step
  4. 當我選擇一個項目,一切都很好:
    Fourth step
  5. 當我下拉imageComboBox時,有一個項目被選中,出現問題:在選定的項目面前的形象由兩個轉變右側和文字像素一個像素移位左:
    Fifth step
    讓我們變焦: Fifth step - zoomed
  6. 在這裏證明我沒有有時錯誤:
    Fifth step - no bug
    讓我們再次放大:
    Fifth step - no bug - zoomed

這裏是我的ImageComboBoxDrawItemEvent
this._imageList是我ImageList對象)

private void OnDrawItem(object sender, DrawItemEventArgs e) { 
    if (e.Index >= 0) { 
     // If the current item is one in the comboBox 

     // Compute the X location of the text to drawn 
     int strLocationX = this._imageList.Images.Count > e.Index ? 
       this._imageList.Images[e.Index].Width + 1 : 
       e.Bounds.X + 1; 

     // Get the displayed text of the current item 
     String itemText = this.Items[e.Index].ToString(); 

     if (this.DroppedDown) { 
      // If the comboBox is dropped down 

      // Draw the blue rectangle 
      e.DrawBackground(); 

      if (e.State == DrawItemState.ComboBoxEdit) { 
       // If we are drawing the selected item 

       // Draw the text 
       e.Graphics.DrawString(itemText, this.Font, Brushes.Black, 
        new Point(strLocationX + 1, e.Bounds.Y + 1)); 

       if (this._imageList.Images.Count > e.Index) { 
        // If we have an image available 

        // Draw the image 
        e.Graphics.DrawImage(this._imageList.Images[e.Index], 
         new Point(e.Bounds.X, e.Bounds.Y - 1)); 
       } 

      } else { 
       // If we are drawing one of the item in the drop down 

       // Check if the item is being highlighted 
       if (e.State.ToString().Contains(DrawItemState.Focus.ToString()) && 
        e.State.ToString().Contains(DrawItemState.Selected.ToString())) { 
        // Draw the text in White 
        e.Graphics.DrawString(itemText, this.Font, Brushes.White, 
         new Point(strLocationX, e.Bounds.Y + 1)); 
       } else { 
        // Draw the text in Black 
        e.Graphics.DrawString(itemText, this.Font, Brushes.Black, 
         new Point(strLocationX, e.Bounds.Y + 1)); 
       } 

       if (this._imageList.Images.Count > e.Index) { 
        // If we have an image available 

        // Draw the image 
        e.Graphics.DrawImage(this._imageList.Images[e.Index], 
         new Point(e.Bounds.X + 2, e.Bounds.Y - 1)); 
       } 
      } 
     } else { 
      // If the comboBox is not dropped down 

      // Draw the text 
      e.Graphics.DrawString(itemText, this.Font, Brushes.Black, 
       new Point(strLocationX + 1, e.Bounds.Y + 1)); 

      if (this._imageList.Images.Count > e.Index) { 
       // If we have an image available 

       // Draw the image 
       e.Graphics.DrawImage(this._imageList.Images[e.Index], 
        new Point(e.Bounds.X, e.Bounds.Y - 1)); 
      } 
     } 
    } 
} 

對於我的觀點,代碼應該是對的,但似乎有時候條件不成立,噸返回相同的結果,而我認爲它應該做的。

任何關於這個問題可能來自哪裏的線索?

回答

0

找到答案:代碼是不正確的,我在代碼片段的開頭有一個錯誤的if條件(但我仍然不明白爲什麼這個錯誤有時會發生,有時不會發生)。

我必須使用:的

if (this.DroppedDown) { 
    // If the comboBox is dropped down 

    // Draw the blue rectangle 
    e.DrawBackground(); 

    if ((e.State & DrawItemState.ComboBoxEdit) == DrawItemState.ComboBoxEdit) { 
     // If we are drawing the selected item 

     // ... 

代替:

if (this.DroppedDown) { 
    // If the comboBox is dropped down 

    // Draw the blue rectangle 
    e.DrawBackground(); 

    if (e.State == DrawItemState.ComboBoxEdit) { 
     // If we are drawing the selected item 

     // ... 
相關問題