0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

iconファイルを作れない環境でNotifyiconを使いたいときの忘備録

Last updated at Posted at 2020-08-15

Visual Studioもない、ネットにも繋がらない、windowsの標準アプリしかない、そんな環境でちょっとしたデスクトップアプリをC#で作りたいとき。

デフォルトのwindows10だとiconファイルが作れない

ペイントだとiconファイルで保存できないので、Notifyiconを表示できない事象が発生。

Windows10でアイコンファイルを作成する方法
上記を参考にiconファイル作成

  1. ペイントで画像を作成し、32×32の24ビットマップ形式で保存
  2. ファイルの拡張子をbmp⇒iconに変更
  3. 以下のようにNotifyIconの作成
form.cs
this.notifyIcon = new NotifyIcon()
{
    Icon = new Icon("作ったアイコン画像.icon"),
    Visible = true,
    Text = "sample"
};

エラー発生

System.ArgumentException: 'Argument 'picture' must be a picture that can be used as a Icon.'

ビットマップファイルををコード内でiconファイルに変換

ひとまず、拡張子をbmp⇒iconに戻す。

form.cs
var bitmap = new Bitmap("作ったアイコン画像.bmp");
this.notifyIcon = new NotifyIcon()
{
    Icon = Icon.FromHandle(bitMap.GetHicon()),
    Visible = true,
    Text = "sample"
};

タスクトレイに作成したアイコンが表示される。

まとめ

iconファイルを作れない環境で、Notifyiconを表示することができた。
bmp⇒iconへ変換するツールを作ってもいいかもしれないけど
いちいち変換が面倒なのでbmpでもアイコン表示できるよう対応したほうが楽なのかも。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?