From 8e53a035089f2811c4b2c4b65b42565a73dd98ad Mon Sep 17 00:00:00 2001 From: Tsuda Kageyu Date: Fri, 21 Aug 2015 05:10:02 +0900 Subject: [PATCH] Move the owner draw code to the custom control from the form. --- SampleApp/Form1.Designer.cs | 40 ++++++++++++++++++------------------- SampleApp/Form1.cs | 32 ----------------------------- SampleApp/IconListView.cs | 36 +++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 53 deletions(-) diff --git a/SampleApp/Form1.Designer.cs b/SampleApp/Form1.Designer.cs index 314cdf0..12fcad0 100644 --- a/SampleApp/Form1.Designer.cs +++ b/SampleApp/Form1.Designer.cs @@ -38,19 +38,19 @@ private void InitializeComponent() this.cbShowChecker = new System.Windows.Forms.CheckBox(); this.iconPickerDialog = new SampleApp.IconPickerDialog(); this.SuspendLayout(); - // + // // txtFileName - // - this.txtFileName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + // + this.txtFileName.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.txtFileName.Location = new System.Drawing.Point(110, 12); this.txtFileName.Name = "txtFileName"; this.txtFileName.ReadOnly = true; this.txtFileName.Size = new System.Drawing.Size(370, 19); this.txtFileName.TabIndex = 1; - // + // // btnSelectIcon - // + // this.btnSelectIcon.Location = new System.Drawing.Point(12, 12); this.btnSelectIcon.Name = "btnSelectIcon"; this.btnSelectIcon.Size = new System.Drawing.Size(92, 19); @@ -58,26 +58,24 @@ private void InitializeComponent() this.btnSelectIcon.Text = "Select Icon..."; this.btnSelectIcon.UseVisualStyleBackColor = true; this.btnSelectIcon.Click += new System.EventHandler(this.btnPickFile_Click); - // + // // lvwIcons - // - this.lvwIcons.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) - | System.Windows.Forms.AnchorStyles.Left) + // + this.lvwIcons.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.lvwIcons.BackgroundImageTiled = true; this.lvwIcons.Location = new System.Drawing.Point(12, 37); this.lvwIcons.MultiSelect = false; this.lvwIcons.Name = "lvwIcons"; - this.lvwIcons.OwnerDraw = true; this.lvwIcons.Size = new System.Drawing.Size(468, 291); this.lvwIcons.TabIndex = 2; this.lvwIcons.TileSize = new System.Drawing.Size(132, 130); this.lvwIcons.UseCompatibleStateImageBehavior = false; this.lvwIcons.View = System.Windows.Forms.View.Tile; - this.lvwIcons.DrawItem += new System.Windows.Forms.DrawListViewItemEventHandler(this.lvwIcons_DrawItem); - // + // // btnSaveAsIco - // + // this.btnSaveAsIco.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btnSaveAsIco.Location = new System.Drawing.Point(164, 334); this.btnSaveAsIco.Name = "btnSaveAsIco"; @@ -86,13 +84,13 @@ private void InitializeComponent() this.btnSaveAsIco.Text = "Save as Single .ico..."; this.btnSaveAsIco.UseVisualStyleBackColor = true; this.btnSaveAsIco.Click += new System.EventHandler(this.btnSaveAsIco_Click); - // + // // saveIcoDialog - // + // this.saveIcoDialog.Filter = "Icon files|*.ico"; - // + // // btnSaveAsPng - // + // this.btnSaveAsPng.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.btnSaveAsPng.Location = new System.Drawing.Point(325, 334); this.btnSaveAsPng.Name = "btnSaveAsPng"; @@ -101,9 +99,9 @@ private void InitializeComponent() this.btnSaveAsPng.Text = "Save as Multiple .png..."; this.btnSaveAsPng.UseVisualStyleBackColor = true; this.btnSaveAsPng.Click += new System.EventHandler(this.btnSaveAsPng_Click); - // + // // cbShowChecker - // + // this.cbShowChecker.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.cbShowChecker.AutoSize = true; this.cbShowChecker.Location = new System.Drawing.Point(12, 338); @@ -113,9 +111,9 @@ private void InitializeComponent() this.cbShowChecker.Text = "Show Checker"; this.cbShowChecker.UseVisualStyleBackColor = true; this.cbShowChecker.CheckedChanged += new System.EventHandler(this.cbShowChecker_CheckedChanged); - // + // // Form1 - // + // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(492, 369); diff --git a/SampleApp/Form1.cs b/SampleApp/Form1.cs index 77e481b..ae6736e 100644 --- a/SampleApp/Form1.cs +++ b/SampleApp/Form1.cs @@ -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); - } } } diff --git a/SampleApp/IconListView.cs b/SampleApp/IconListView.cs index f598a32..ff94abd 100644 --- a/SampleApp/IconListView.cs +++ b/SampleApp/IconListView.cs @@ -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); } }