2009-08-22 22 views
7

Tôi đang sử dụng OwnerDrawFixed làm một DrawMode cho điều khiển ListBox tùy chỉnh trong ứng dụng WinForms của tôi.ListBox DrawItem HotLight State trong chế độ OwnerDraw?

Tôi muốn sơn lại nền (hoặc làm một số hành động khác) của ListBoxItem khi người dùng di chuột qua mục hộp danh sách, có nghĩa là, tại MouseMove ...

DrawItemState.HotLight không bao giờ làm việc cho ListBox, vì vậy tôi tự hỏi làm thế nào để thi đua nó, làm thế nào để giải quyết vấn đề này.

Trả lời

11

Nó đã cho tôi chỉ có hai năm để tìm ra câu trả lời cho bạn, nhưng ở đây nó là:

Các DrawItemState.HotLight chỉ áp dụng cho chủ sở hữu rút ra menu, không ListBox. Đối với ListBox, bạn phải theo dõi mục đó:

public partial class Form1 : Form 
{ 
    private int _MouseIndex = -1; 

    public Form1() 
    { InitializeComponent(); } 

    private void listBox1_DrawItem(object sender, DrawItemEventArgs e) 
    { 
    Brush textBrush = SystemBrushes.WindowText; 

    if (e.Index > -1) 
    { 
     if (e.Index == _MouseIndex) 
     { 
     e.Graphics.FillRectangle(SystemBrushes.HotTrack, e.Bounds); 
     textBrush = SystemBrushes.HighlightText; 
     } 
     else 
     { 
     if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) 
     { 
      e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds); 
      textBrush = SystemBrushes.HighlightText; 
     } 
     else 
      e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds); 
     } 
     e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, textBrush, e.Bounds.Left + 2, e.Bounds.Top); 
    } 
    } 

    private void listBox1_MouseMove(object sender, MouseEventArgs e) 
    { 
    int index = listBox1.IndexFromPoint(e.Location); 
    if (index != _MouseIndex) 
    { 
     _MouseIndex = index; 
     listBox1.Invalidate(); 
    } 
    } 

    private void listBox1_MouseLeave(object sender, EventArgs e) 
    { 
    if (_MouseIndex > -1) 
    { 
     _MouseIndex = -1; 
     listBox1.Invalidate(); 
    } 
    } 
} 
+2

Vâng, 2 năm sau, bạn cũng đã giúp tôi! ;) Nice, cách đơn giản để 'theo dõi nóng' trong một ListBox ... cảm ơn! – ChandlerPelhams

+1

Và LarsTech sẽ giúp nhiều người trong nhiều năm tới ... Tôi đang tìm kiếm một câu trả lời tương tự và tìm thấy nó ở đây. Cảm ơn. – ThN

+0

Làm thế nào điều này có thể được thực hiện cho một combobox? Combobox không có IndexFromPoint. –

0

Giải pháp này sẽ chỉ cân nhắc mã của bạn; chỉ cần thử điều này:

If e.State And DrawItemState.Selected Then 
        e.Graphics.FillRectangle(SystemBrushes.HotTrack, e.Bounds) 
        e.Graphics.DrawString(drv, Me.Font, SystemBrushes.HighlightText, e.Bounds.X + 18, e.Bounds.Y + 1) 
       Else 
        e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds) 
        e.Graphics.DrawString(drv, Me.Font, SystemBrushes.ControlText, e.Bounds.X + 18, e.Bounds.Y + 1) 
End If 

Thao tác này: e.State And DrawItemState.Selected xác minh mục được di chuột. Không cần phải đặt một gói toàn bộ mã chỉ để biết mục nào được lơ lửng.

+0

Giải pháp của bạn không thực sự giải quyết vấn đề mà OP đã hỏi. – LarsTech

Các vấn đề liên quan