- 2009-09-22 (火) 1:52
- Programming
ちょっと頼まれてソフトをしこしこ作っていたのだけど、コアな機能よりUIに手間がかかってしまいます。WPF って慣れないので、ひとつひとつの作業にいちいちヘルプをにらめっこする必要があるんですよねぇ…
今日はバージョン情報ダイアログを作るのに四苦八苦。必要な機能は、
- アセンブリからアプリ名やバージンを勝手に取ってくること
- アプリケーションアイコンを抽出して表示すること
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Stream stream = new MemoryStream();
Bitmap bitmap = System.Drawing.Icon.ExtractAssociatedIcon(
Environment.GetCommandLineArgs()[0]).ToBitmap();
bitmap.MakeTransparent(bitmap.GetPixel(0, 0));
bitmap.Save(stream, ImageFormat.Bmp);
BitmapDecoder decoder = new BmpBitmapDecoder(
stream,
BitmapCreateOptions.None,
BitmapCacheOption.None);
imageAppIcon.Source = decoder.Frames[0];
# using Forms = System.Windows.Forms;
string appVersion = Forms.Application.ProductVersion;
string appProductName = Forms.Application.ProductName;
string appCompanyName = Forms.Application.CompanyName;
textBlockAppName.Text = appProductName;
textBlockAppVersion.Text = "Version " + appVersion;
textBlockAppCopyRight.Text = string.Format(
"Copyright 2009 {1}.\nAll rights reserved\n\n" +
"Powered by .NET Framework 3.5 SP1",
appVersion, appCompanyName);
}
アイコンを抽出する作業が面倒だった…orz
ExtractAssociatedIcon(Environment.GetCommandLineArgs()[0])
でさっくり抜いてくるまではいいんだけど、そうやって ImageSource にぶち込めるようになるのだろうとヘルプをぐるぐる。答えは、BitmapDecoder を利用する、らしいです。
バージョン情報やアプリケーション名の取得は、WinForm のやり方で取得。名前空間がかぶりまくるので注意しましょう。
ふぅ…アイコンの背景が黒くなるのはご愛嬌…どうすりゃいいんだろ(=w=?
ウィンドウの色で塗ってしまおうかしらん?
- Newer: 2009年09月21日の記事一覧
- Older: Ben Spies replica YAMAHA R1

