Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Разработка Windows Forms > ListView свой рисовальщик и проблемма с первой коо


Автор: interv 20.4.2012, 12:22
Есть ListView, установлены значения
  • AllowColumnReorder=true
  • OwnerDraw=true
  • View=Details
  • HideSelection=false
и переопределены рисовальщики
Код

public static void DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            if (e.ItemIndex < 0) return;
            DrawListViewSubItemEventArgs ne;
            //Если это убрать то вообще будет хлам
            if (e.ColumnIndex <= 0)
            {
                int x = 0;
                for (int i = 1; i < e.Item.ListView.Columns.Count; ++i)
                {
                    //e.Header.DisplayIndex
                    if (e.Item.ListView.Columns[i].DisplayIndex < e.Header.DisplayIndex)
                        x += e.Item.ListView.Columns[i].Width;
                }
                Rectangle rect = new Rectangle(x, e.Bounds.Top,
                                               e.Bounds.Width, e.Bounds.Height);
                ne = new DrawListViewSubItemEventArgs(e.Graphics,
                                                     rect,
                                                     e.Item,
                                                     e.SubItem,
                                                     e.ItemIndex,
                                                     e.ColumnIndex,
                                                     e.Header,
                                                     e.ItemState);
                ne.Graphics.Clip = new Region(rect);
            }
            else
                ne = e;
            ne.SubItem.BackColor = ne.Item.BackColor;
            ne.DrawBackground();
            if (ne.Item.Selected)
                ne.Graphics.FillRectangle(Brushes.Purple, ne.Bounds);
            else
                ne.Graphics.FillRectangle(new SolidBrush(ne.SubItem.BackColor), ne.Bounds);
            ne.DrawText();
        }

        public static void DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            e.DrawBackground();
            e.DrawText();
        }

Теперь меняем первую колонку с другой (неважно какой) и перетаскиваем горизонтальный бегунок чуть в право. Вуаля! Получается бред.
Причем косяк именно только с первой колонкой subitems[0]

Автор: interv 20.4.2012, 13:01
Решено через попу... Кому интересно:
Код

        public static void DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            int x = e.Item.Bounds.X;
            int w = e.Item.ListView.Columns[0].Width;
            for (int i = 1; i < e.Item.ListView.Columns.Count; ++i)
            {
                if (e.Item.ListView.Columns[i].DisplayIndex < e.Item.ListView.Columns[0].DisplayIndex)
                    x += e.Item.ListView.Columns[i].Width;
            }
            string text = e.Item.Text;
            int cx, cy;
            e.Graphics.MeasureString(text, e.Item.Font, new SizeF(w, 10000), StringFormat.GenericDefault, out cx, out cy);
            bool tt = false;
            while (cy>1 && text.Length>0)
            {
                tt = true;
                text = text.Remove(cx - 1);
                e.Graphics.MeasureString(text+"...", e.Item.Font, new SizeF(w, 10000), StringFormat.GenericDefault, out cx, out cy);
                cx = text.Length;
            }
            Rectangle rect = new Rectangle(x, e.Bounds.Top,
                                           e.Bounds.Width, e.Bounds.Height);
            if (e.Item.Selected)
                e.Graphics.FillRectangle(Brushes.Purple, e.Bounds);
            else
                e.Graphics.FillRectangle(new SolidBrush(e.Item.BackColor), e.Bounds);
            e.Graphics.DrawString(text + (tt ? "..." : ""), e.Item.Font, new SolidBrush(e.Item.ForeColor), rect);
        }


        public static void DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            if (e.ItemIndex < 0) return;
            if (e.ColumnIndex == 0)
                return;
            e.SubItem.BackColor = e.Item.BackColor;
            if (e.Item.Selected)
                e.Graphics.FillRectangle(Brushes.Purple, e.Bounds);
            else
                e.Graphics.FillRectangle(new SolidBrush(e.SubItem.BackColor), e.Bounds);
            e.DrawText();
        }

        public static void DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            e.DrawBackground();
            e.DrawText();
        }

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)