Improve ListBox

This commit is contained in:
Louis Lam 2022-11-29 18:36:25 +08:00
parent b422dcadac
commit 9b376b87b6
6 changed files with 56 additions and 5 deletions

View File

@ -68,5 +68,15 @@ namespace rdp_portal.Properties {
return ((System.Drawing.Bitmap)(obj)); return ((System.Drawing.Bitmap)(obj));
} }
} }
/// <summary>
/// Looks up a localized resource of type System.Drawing.Icon similar to (Icon).
/// </summary>
internal static System.Drawing.Icon icon {
get {
object obj = ResourceManager.GetObject("icon", resourceCulture);
return ((System.Drawing.Icon)(obj));
}
}
} }
} }

View File

@ -62,4 +62,7 @@
<data name="banner" type="System.Resources.ResXFileRef, System.Windows.Forms"> <data name="banner" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\banner.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value> <value>..\Resources\banner.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data> </data>
</root> <data name="icon" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\icon.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>

View File

@ -201,7 +201,6 @@
<DependentUpon>Settings.settings</DependentUpon> <DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput> <DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile> </Compile>
<None Include="Resources\icon.ico" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.config" /> <None Include="App.config" />
@ -209,6 +208,7 @@
<ItemGroup> <ItemGroup>
<Content Include="app.manifest" /> <Content Include="app.manifest" />
<Content Include="Resources\banner.png" /> <Content Include="Resources\banner.png" />
<Content Include="Resources\icon.ico" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="packages\Fody.6.6.4\build\Fody.targets" Condition="Exists('packages\Fody.6.6.4\build\Fody.targets')" /> <Import Project="packages\Fody.6.6.4\build\Fody.targets" Condition="Exists('packages\Fody.6.6.4\build\Fody.targets')" />

View File

@ -1,4 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=rdp_002Dportal_002FMainForm/@EntryIndexedValue">False</s:Boolean> <s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=rdp_002Dportal_002FMainForm/@EntryIndexedValue">False</s:Boolean>
<s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=rdp_002Dportal_002FProperties_002FResources/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=rdp_002Dportal_002FProperties_002FResources/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/ResxEditorPersonal/Initialized/@EntryValue">True</s:Boolean></wpf:ResourceDictionary> <s:Boolean x:Key="/Default/ResxEditorPersonal/Initialized/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/ResxEditorPersonal/ShowOnlyErrors/@EntryValue">False</s:Boolean></wpf:ResourceDictionary>

View File

@ -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.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.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.FormattingEnabled = true;
this.listBox.ItemHeight = 16; this.listBox.ItemHeight = 32;
this.listBox.Location = new System.Drawing.Point(13, 107); this.listBox.Location = new System.Drawing.Point(13, 107);
this.listBox.Margin = new System.Windows.Forms.Padding(4); this.listBox.Margin = new System.Windows.Forms.Padding(4);
this.listBox.Name = "listBox"; this.listBox.Name = "listBox";
this.listBox.Size = new System.Drawing.Size(211, 276); this.listBox.Size = new System.Drawing.Size(211, 276);
this.listBox.TabIndex = 0; 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.SelectedValueChanged += new System.EventHandler(this.listBox_SelectedValueChanged);
this.listBox.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.listBox_MouseDoubleClick); this.listBox.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.listBox_MouseDoubleClick);
// //
@ -354,4 +357,4 @@
#endregion #endregion
} }
} }

View File

@ -205,5 +205,39 @@ namespace RDP_Portal {
private void listBox_MouseDoubleClick(object sender, MouseEventArgs e) { private void listBox_MouseDoubleClick(object sender, MouseEventArgs e) {
buttonConnect_Click(sender, 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();
}
} }
} }