From 9b376b87b6bea6c2e0bb04a131b1ccddc6cc3ef6 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Tue, 29 Nov 2022 18:36:25 +0800 Subject: [PATCH] Improve ListBox --- Properties/Resources.Designer.cs | 10 ++++++++++ Properties/Resources.resx | 5 ++++- rdp-portal.csproj | 2 +- rdp-portal.sln.DotSettings.user | 3 ++- src/MainForm.Designer.cs | 7 +++++-- src/MainForm.cs | 34 ++++++++++++++++++++++++++++++++ 6 files changed, 56 insertions(+), 5 deletions(-) diff --git a/Properties/Resources.Designer.cs b/Properties/Resources.Designer.cs index 8e1130d..44aa4d7 100644 --- a/Properties/Resources.Designer.cs +++ b/Properties/Resources.Designer.cs @@ -68,5 +68,15 @@ namespace rdp_portal.Properties { return ((System.Drawing.Bitmap)(obj)); } } + + /// + /// Looks up a localized resource of type System.Drawing.Icon similar to (Icon). + /// + internal static System.Drawing.Icon icon { + get { + object obj = ResourceManager.GetObject("icon", resourceCulture); + return ((System.Drawing.Icon)(obj)); + } + } } } diff --git a/Properties/Resources.resx b/Properties/Resources.resx index 9042e1c..ec3c574 100644 --- a/Properties/Resources.resx +++ b/Properties/Resources.resx @@ -62,4 +62,7 @@ ..\Resources\banner.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a - \ No newline at end of file + + ..\Resources\icon.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + diff --git a/rdp-portal.csproj b/rdp-portal.csproj index 39d0cc9..41d327a 100644 --- a/rdp-portal.csproj +++ b/rdp-portal.csproj @@ -201,7 +201,6 @@ Settings.settings True - @@ -209,6 +208,7 @@ + diff --git a/rdp-portal.sln.DotSettings.user b/rdp-portal.sln.DotSettings.user index 0991bf6..9bc35bf 100644 --- a/rdp-portal.sln.DotSettings.user +++ b/rdp-portal.sln.DotSettings.user @@ -1,4 +1,5 @@  False True - True \ No newline at end of file + True + False \ No newline at end of file diff --git a/src/MainForm.Designer.cs b/src/MainForm.Designer.cs index 8f408ff..5c3ab03 100644 --- a/src/MainForm.Designer.cs +++ b/src/MainForm.Designer.cs @@ -272,13 +272,16 @@ // this.listBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left))); this.listBox.DisplayMember = "Name"; + this.listBox.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; + this.listBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.listBox.FormattingEnabled = true; - this.listBox.ItemHeight = 16; + this.listBox.ItemHeight = 32; this.listBox.Location = new System.Drawing.Point(13, 107); this.listBox.Margin = new System.Windows.Forms.Padding(4); this.listBox.Name = "listBox"; this.listBox.Size = new System.Drawing.Size(211, 276); this.listBox.TabIndex = 0; + this.listBox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox_DrawItem); this.listBox.SelectedValueChanged += new System.EventHandler(this.listBox_SelectedValueChanged); this.listBox.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.listBox_MouseDoubleClick); // @@ -354,4 +357,4 @@ #endregion } -} \ No newline at end of file +} diff --git a/src/MainForm.cs b/src/MainForm.cs index 9ef2ff4..66a34d8 100644 --- a/src/MainForm.cs +++ b/src/MainForm.cs @@ -205,5 +205,39 @@ namespace RDP_Portal { private void listBox_MouseDoubleClick(object sender, MouseEventArgs e) { buttonConnect_Click(sender, e); } + + /** + * From https://stackoverflow.com/questions/8333282/how-can-i-include-icons-in-my-listbox + */ + private void listBox_DrawItem(object sender, DrawItemEventArgs e) { + if (e.Index == -1) + return; + + e.DrawBackground(); + Brush myBrush = Brushes.Black; + + + var iconWidth = listBox.ItemHeight; + var iconMargin = 4; + var textMargin = (iconWidth - 18) / 2; + var rect = new Rectangle(e.Bounds.X + iconMargin, e.Bounds.Y, iconWidth, iconWidth); + //assuming the icon is already added to project resources + + e.Graphics.DrawIcon(rdp_portal.Properties.Resources.icon, rect); + + var profile = (Profile)listBox.Items[e.Index]; + + e.Graphics.DrawString( + profile.Name, + e.Font, + myBrush, + new Rectangle(e.Bounds.X + iconMargin * 2 + iconWidth, e.Bounds.Y + textMargin, e.Bounds.Width, e.Bounds.Height), + StringFormat.GenericDefault + ); + + // If the ListBox has focus, draw a focus rectangle around the selected item. + e.DrawFocusRectangle(); + } + } }