Skip to content

Commit

Permalink
Move the owner draw code to the custom control from the form.
Browse files Browse the repository at this point in the history
  • Loading branch information
TsudaKageyu committed Aug 20, 2015
1 parent adf254b commit 8e53a03
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 53 deletions.
40 changes: 19 additions & 21 deletions SampleApp/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 0 additions & 32 deletions SampleApp/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,37 +128,5 @@ private void btnSaveAsPng_Click(object sender, EventArgs e)
}
}
}

private void lvwIcons_DrawItem(object sender, DrawListViewItemEventArgs e)
{
var item = e.Item as IconListViewItem;

// Draw item

e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.Clip = new Region(e.Bounds);

if (e.Item.Selected)
e.Graphics.FillRectangle(SystemBrushes.MenuHighlight, e.Bounds);

int w = Math.Min(128, item.Bitmap.Width);
int h = Math.Min(128, item.Bitmap.Height);

int x = e.Bounds.X + (e.Bounds.Width - w) / 2;
int y = e.Bounds.Y + (e.Bounds.Height - h) / 2;
var dstRect = new Rectangle(x, y, w, h);
var srcRect = new Rectangle(Point.Empty, item.Bitmap.Size);

e.Graphics.DrawImage(item.Bitmap, dstRect, srcRect, GraphicsUnit.Pixel);

var textRect = new Rectangle(
e.Bounds.Left, e.Bounds.Bottom - Font.Height - 4,
e.Bounds.Width, Font.Height + 2);
TextRenderer.DrawText(e.Graphics, item.ToolTipText, Font, textRect, ForeColor);

e.Graphics.Clip = new Region();
e.Graphics.DrawRectangle(SystemPens.ControlLight, e.Bounds);
}
}
}
36 changes: 36 additions & 0 deletions SampleApp/IconListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,42 @@ public IconListView() : base()
SetStyle(ControlStyles.AllPaintingInWmPaint
| ControlStyles.OptimizedDoubleBuffer
| ControlStyles.ResizeRedraw, true);

OwnerDraw = true;
}

protected override void OnDrawItem(DrawListViewItemEventArgs e)
{
base.OnDrawItem(e);

var item = e.Item as IconListViewItem;

// Draw item

e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.Clip = new Region(e.Bounds);

if (e.Item.Selected)
e.Graphics.FillRectangle(SystemBrushes.MenuHighlight, e.Bounds);

int w = Math.Min(128, item.Bitmap.Width);
int h = Math.Min(128, item.Bitmap.Height);

int x = e.Bounds.X + (e.Bounds.Width - w) / 2;
int y = e.Bounds.Y + (e.Bounds.Height - h) / 2;
var dstRect = new Rectangle(x, y, w, h);
var srcRect = new Rectangle(Point.Empty, item.Bitmap.Size);

e.Graphics.DrawImage(item.Bitmap, dstRect, srcRect, GraphicsUnit.Pixel);

var textRect = new Rectangle(
e.Bounds.Left, e.Bounds.Bottom - Font.Height - 4,
e.Bounds.Width, Font.Height + 2);
TextRenderer.DrawText(e.Graphics, item.ToolTipText, Font, textRect, ForeColor);

e.Graphics.Clip = new Region();
e.Graphics.DrawRectangle(SystemPens.ControlLight, e.Bounds);
}
}

Expand Down

0 comments on commit 8e53a03

Please sign in to comment.