Skip to content

Commit

Permalink
examples for .net 8 / 9 features
Browse files Browse the repository at this point in the history
  • Loading branch information
grantwinney committed Dec 19, 2024
1 parent 547bb40 commit efe85cd
Show file tree
Hide file tree
Showing 20 changed files with 1,186 additions and 22 deletions.
22 changes: 22 additions & 0 deletions .NET 08/GetStockIcon/GetStockIconTest.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35514.174 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GetStockIconTest", "GetStockIconTest\GetStockIconTest.csproj", "{9A3E489E-90B6-4419-9B18-9A3C6BEF4D76}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{9A3E489E-90B6-4419-9B18-9A3C6BEF4D76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9A3E489E-90B6-4419-9B18-9A3C6BEF4D76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9A3E489E-90B6-4419-9B18-9A3C6BEF4D76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9A3E489E-90B6-4419-9B18-9A3C6BEF4D76}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
151 changes: 151 additions & 0 deletions .NET 08/GetStockIcon/GetStockIconTest/Form1.Designer.cs

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

97 changes: 97 additions & 0 deletions .NET 08/GetStockIcon/GetStockIconTest/Form1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System.Linq;

namespace GetStockIconTest;

public partial class Form1 : Form
{
readonly Bitmap connImg;
readonly Bitmap disconnImg;

public Form1()
{
InitializeComponent();

connImg = SystemIcons.GetStockIcon(StockIconId.DriveNet, 128).ToBitmap();
disconnImg = SystemIcons.GetStockIcon(StockIconId.DriveNetDisabled, 128).ToBitmap();
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
connImg.Dispose();
disconnImg.Dispose();
}

private void Form1_Load(object sender, EventArgs e)
{
lboxSaveIcons.SelectedIndex = 0;

// Populate image list with default size, which should be 32 px
imageList1.Images.Add("Conn", SystemIcons.GetStockIcon(StockIconId.DriveNet));
imageList1.Images.Add("Disconn", SystemIcons.GetStockIcon(StockIconId.DriveNetDisabled));

button1.ImageKey = "Conn";
pictureBox1.Image = connImg;

// Populate another image list with 64 px size
foreach (StockIconId icon in Enum.GetValues(typeof(StockIconId)))
imageList2.Images.Add(icon.ToString(), SystemIcons.GetStockIcon(icon, 64));

// Then use the icons in the second list to populate the toolbar
// and generate a bunch of pictures with the icon at the larger size
foreach (var imageKey in imageList2.Images.Keys)
{
var btn = new ToolStripButton(imageList2.Images[imageKey]);
btn.Click += (s, e) => MessageBox.Show($"Stock Icon: {imageKey}");
btn.Image = imageList2.Images[imageKey];
btn.ToolTipText = imageKey;
toolStrip1.Items.Add(btn);

var pb = new PictureBox
{
Width = 64, Height = 64,
Image = imageList2.Images[imageKey]
};
toolTip1.SetToolTip(pb, imageKey);
flowLayoutPanel1.Controls.Add(pb);
}
}

private void button1_Click(object sender, EventArgs e)
{
// Toggle between two icons
if (button1.ImageKey == "Conn")
{
button1.ImageKey = "Disconn";
button1.Text = "Disconnected";
pictureBox1.Image = disconnImg;
}
else
{
button1.ImageKey = "Conn";
button1.Text = "Connected";
pictureBox1.Image = connImg;
}
}

private void btnSaveIcons_Click(object sender, EventArgs e)
{
// Save all icons in .ico format (which looks ugly)
// and in .bmp format (which looks great)
var result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
var savePath = folderBrowserDialog1.SelectedPath;
foreach (StockIconId icon in Enum.GetValues(typeof(StockIconId)))
{
var stockIcon = SystemIcons.GetStockIcon(icon, Convert.ToInt32(lboxSaveIcons.SelectedItem));

// Save .bmp file
stockIcon.ToBitmap().Save(Path.Combine(savePath, $"{icon}.bmp"));

// Save .ico file
using FileStream fs = new(Path.Combine(savePath, $"{icon}.ico"), FileMode.Create);
stockIcon.Save(fs);
}
}
}
}
Loading

0 comments on commit efe85cd

Please sign in to comment.