You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WinForm中为ListView绘制可响应点击的图标按钮(详情/大图标视图)

Nice work getting the icon drawn already! Since this is a custom-drawn element, the ListView doesn't recognize it as a built-in control with click events—so we have to handle the mouse click manually by checking if the cursor landed on our icon's rectangle. Here's how to make it work for both Details and LargeIcon views:

Step 1: Ensure OwnerDraw is enabled

First, make sure your ListView's OwnerDraw property is set to true (either in the designer or in code), since we're using custom drawing events to render the icon.

Step 2: Handle MouseClick to detect icon taps

We'll use the ListView's MouseClick event to check if the click was on our custom icon. We need to handle both view types separately because their item bounds work differently:

private void ls_MouseClick(object sender, MouseEventArgs e)
{
    var listView = sender as ListView;
    if (listView == null) return;

    var hitTest = listView.HitTest(e.Location);
    var clickedItem = hitTest.Item;

    if (clickedItem == null) return;

    // Handle Details View
    if (listView.View == View.Details)
    {
        var clickedSubItem = hitTest.SubItem;
        // Check if we clicked the last column's subitem (where we drew the icon)
        if (clickedSubItem != null && clickedSubItem == clickedItem.SubItems[listView.Columns.Count - 1])
        {
            // Calculate the same icon rectangle we used in DrawSubItem
            Rectangle iconRect = new Rectangle(clickedSubItem.Bounds.X, clickedSubItem.Bounds.Y, 
                                               clickedSubItem.Bounds.Height, clickedSubItem.Bounds.Height);
            if (iconRect.Contains(e.Location))
            {
                // Replace this with your custom click logic
                MessageBox.Show($"Icon clicked for item: {clickedItem.Text}");
            }
        }
    }
    // Handle LargeIcon View
    else if (listView.View == View.LargeIcon)
    {
        // Calculate the icon rectangle matching our DrawItem logic
        Rectangle iconRect = new Rectangle(clickedItem.Bounds.Right - clickedItem.Bounds.Height, 
                                           clickedItem.Bounds.Y, 
                                           clickedItem.Bounds.Height, clickedItem.Bounds.Height);
        if (iconRect.Contains(e.Location))
        {
            // Replace this with your custom click logic
            MessageBox.Show($"Icon clicked for item: {clickedItem.Text}");
        }
    }
}

Step 3: Update drawing for LargeIcon View (if needed)

Your existing DrawSubItem works great for Details View, but for LargeIcon View, we need to use the DrawItem event to render the icon. Add this handler:

private void ls_DrawItem(object sender, DrawListViewItemEventArgs e)
{
    var listView = sender as ListView;
    if (listView == null) return;

    // Let the ListView draw the default item first
    e.DrawDefault = true;

    // Only draw our custom icon in LargeIcon view
    if (listView.View == View.LargeIcon)
    {
        // Position the icon to the right of the large item icon (adjust as needed)
        Rectangle iconRect = new Rectangle(e.Bounds.Right - e.Bounds.Height, 
                                           e.Bounds.Y, 
                                           e.Bounds.Height, e.Bounds.Height);
        e.Graphics.DrawImage(SystemIcons.Information.ToBitmap(), iconRect);
    }
}

Important Notes

  • Make sure you've hooked up all the events either in the designer or in your form's constructor:
    public YourForm()
    {
        InitializeComponent();
        ls.DrawSubItem += ls_DrawSubItem;
        ls.DrawItem += ls_DrawItem;
        ls.MouseClick += ls_MouseClick;
        ls.OwnerDraw = true;
    }
    
  • Adjust the icon position in DrawItem if you want it in a different spot (e.g., below the large icon instead of to the right).
  • For consistency, keep the icon rectangle calculation logic identical between the drawing event and the click event—this ensures we're checking the exact area where the icon was drawn.

内容的提问来源于stack exchange,提问作者amingo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:16:46