protected override int DoDefaultPaint(ref DRAWITEMSTRUCT ds) { //Default paint using (Graphics g = Graphics.FromHdc(ds.hDC)) using (Pen p = new Pen((ds.itemState & ODS.SELECTED) != 0 ? ColorInv(ForeColor) : ForeColor)) using (SolidBrush br = new SolidBrush((ds.itemState & ODS.SELECTED) != 0 ? ColorInv(ForeColor) : ForeColor)) { if ((ds.itemState & ODS.SELECTED) != 0) { g.Clear(Color.FromArgb(BackColor.ToArgb() ^ 0xffffff)); } else { g.Clear(BackColor); } Rectangle rc = new Rectangle(ds.rcItem.left, ds.rcItem.top, ds.rcItem.right - ds.rcItem.left, ds.rcItem.bottom - ds.rcItem.top); StringFormat fmt = new StringFormat(); fmt.Alignment = fmt.LineAlignment = System.Drawing.StringAlignment.Center; g.DrawString(Text, Font, br, rc, fmt); rc.Inflate(-1, -1); g.DrawRectangle(p, rc); return(-1); } }
protected virtual int OnDrawItem(ref DRAWITEMSTRUCT ds) { if (DrawItemDelegate == null) { return(0); } return(DrawItemDelegate(this, ref ds)); }
protected override int OnDrawItem(IntPtr hWnd, WM msg, IntPtr wParam, IntPtr lParam) { DRAWITEMSTRUCT ds = (DRAWITEMSTRUCT)Marshal.PtrToStructure(lParam, typeof(DRAWITEMSTRUCT)); if (PaintDelegate != null) { using (Graphics g = Graphics.FromHdc(ds.hDC)) { Rectangle rcPaint = (Rectangle)ds.rcItem; PaintDelegate(this, g, rcPaint, ds.itemAction, ds.itemState); return(0); } } //Default paint //System.Diagnostics.Debug.WriteLine(ds.itemState); //SetTextMode(cd.hdc, TRANSPARENT); //cd.clrText = 0xffffff; return(DoDefaultPaint(ref ds)); }
/// <summary> /// The messages WM_MEASUREITEM and WM_DRAWITEM are sent to the parent control rather than to the ListView itself. /// They come here as WM_REFLECT + WM_MEASUREITEM and WM_REFLECT + WM_DRAWITEM /// They are sent from Control.WmOwnerDraw() --> Control.ReflectMessageInternal() /// </summary> protected override void WndProc(ref Message k_Msg) { base.WndProc(ref k_Msg); // FIRST switch (k_Msg.Msg) { case WM_SHOWWINDOW: // called when the ListView becomes visible { Debug.Assert(View == View.Details, "ListViewEx supports only Details view"); Debug.Assert(OwnerDraw == false, "In ListViewEx do not set OwnerDraw = true"); break; } case WM_REFLECT + WM_MEASUREITEM: // called once when the ListView is created, but only in Details view { mb_Measured = true; // Overwrite itemHeight, which is the fifth integer in MEASUREITEMSTRUCT Marshal.WriteInt32(k_Msg.LParam + 4 * sizeof(int), ms32_RowHeight); k_Msg.Result = (IntPtr)1; break; } case WM_REFLECT + WM_DRAWITEM: // called for each ListViewItem to be drawn { DRAWITEMSTRUCT k_Draw = (DRAWITEMSTRUCT)k_Msg.GetLParam(typeof(DRAWITEMSTRUCT)); using (Graphics i_Graph = Graphics.FromHdc(k_Draw.hDC)) { ListViewItem i_Item = Items[k_Draw.itemID]; Color c_BackColor = i_Item.BackColor; if (i_Item.Selected) { c_BackColor = SystemColors.Highlight; } if (!Enabled) { c_BackColor = SystemColors.Control; } using (SolidBrush i_BackBrush = new SolidBrush(c_BackColor)) { // Erase the background of the entire row i_Graph.FillRectangle(i_BackBrush, i_Item.Bounds); } for (int S = 0; S < i_Item.SubItems.Count; S++) { ListViewItem.ListViewSubItem i_SubItem = i_Item.SubItems[S]; // i_Item.SubItems[0].Bounds contains the entire row, rather than the first column only. Rectangle k_Bounds = (S > 0) ? i_SubItem.Bounds : i_Item.GetBounds(ItemBoundsPortion.Label); // You can use i_Item.ForeColor instead of i_SubItem.ForeColor to get the same behaviour as without OwnerDraw Color c_ForeColor = i_SubItem.ForeColor; if (i_Item.Selected) { c_ForeColor = SystemColors.HighlightText; } if (!Enabled) { c_ForeColor = SystemColors.ControlText; } TextFormatFlags e_Flags = TextFormatFlags.NoPrefix | TextFormatFlags.EndEllipsis | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine; switch (Columns[S].TextAlign) { case HorizontalAlignment.Center: e_Flags |= TextFormatFlags.HorizontalCenter; break; case HorizontalAlignment.Right: e_Flags |= TextFormatFlags.Right; break; } TextRenderer.DrawText(i_Graph, i_SubItem.Text, i_SubItem.Font, k_Bounds, c_ForeColor, e_Flags); } } break; } } }
protected override int DoDefaultPaint(ref DRAWITEMSTRUCT ds) { return(base.DoDefaultPaint(ref ds)); }
protected override int OnDrawItem(IntPtr hWnd, WM msg, IntPtr wParam, IntPtr lParam) { DRAWITEMSTRUCT ds = (DRAWITEMSTRUCT)Marshal.PtrToStructure(lParam, typeof(DRAWITEMSTRUCT)); return(OnDrawItem(ref ds)); }
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref DRAWITEMSTRUCT dis);
/// <summary> /// Empty default paint routine /// </summary> /// <param name="ds"></param> /// <returns></returns> protected virtual int DoDefaultPaint(ref DRAWITEMSTRUCT ds) { return(0); // Not handled }