0
0

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 1 year has passed since last update.

cscの作法 その454

Last updated at Posted at 2024-02-13

概要

cscの作法、調べてみた。
フォーム、表示しないでタスクトレイに住み憑く、方法。

参考にしたページ

写真

image.png

サンプルコード

using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

public static class Animator {
	public static void Animate(int interval, int frequency, Func<int, int, bool> callback) {
		var timer = new System.Windows.Forms.Timer();
		timer.Interval = interval;
		int frame = 0;
		timer.Tick += (sender, e) => {
			if (callback(frame, frequency) == false || frame >= frequency)
			{
				timer.Stop();
			}
			frame++;
		};
		timer.Start();
	}
	public static void Animate(int duration, Func<int, int, bool> callback) {
		const int interval = 25;
		if (duration < interval)
			duration = interval;
		Animate(25, duration / interval, callback);
	}
}
static class MyIconUtil {
	static class NativeMethods {
		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		public extern static bool DestroyIcon(IntPtr handle);
	}
	public static Icon Create16x16Icon(string[] iconDot) {
		Bitmap bmp = new Bitmap(16, 16);
		using (Graphics g = Graphics.FromImage(bmp))
		{
			g.Clear(Color.White);
		}
		for (int y = 0; y < 16; y++)
		{
			for (int x = 0; x < 16; x++)
			{
				if (iconDot[y][x] == '#')
				{
					bmp.SetPixel(x, y, Color.Black);
				}
			}
		}
		IntPtr Hicon = bmp.GetHicon();
		return Icon.FromHandle(Hicon);
	}
	public static void DestroyIcon(Icon icon) {
		NativeMethods.DestroyIcon(icon.Handle);
	}
}
class TaskTrayTest {
	static string[] iconDot = new string[] {
		"................",
		".###...##...##..",
		"..#...#..#.#..#.",
		"..#...#....#..#.",
		"..#...#....#..#.",
		"..#...#....#..#.",
		"..#...#....#..#.",
		"..#...#....#..#.",
		"..#...#....#..#.",
		"..#...#....#..#.",
		"..#...#....#..#.",
		"..#...#....#..#.",
		"..#...#....#..#.",
		"..#...#..#.#..#.",
		".###...##...##..",
		"................",
	};
	TaskTrayTest() {
		NotifyIcon trayIcon = new NotifyIcon();
		Icon tmpIcon = MyIconUtil.Create16x16Icon(iconDot);
		trayIcon.Icon = tmpIcon;
		trayIcon.Visible = true;
		trayIcon.Text = "常駐テスト";
		var menu = new ContextMenuStrip();
		menu.Items.AddRange(new ToolStripMenuItem[] {
			new ToolStripMenuItem("&Open", null, (s, e) => {
				MyDoSomething();
			}, "Open"),
			new ToolStripMenuItem("E&xit", null, (s, e) => {
				MyExit();
			}, "Exit")
		});
		trayIcon.DoubleClick += (s, e) => {
			MyDoSomething();
		};
		trayIcon.ContextMenuStrip = menu;
	}
	void MyDoSomething() {
		Console.WriteLine("open is called.");
		using (var form = new Form())
		{
			form.FormBorderStyle = FormBorderStyle.None;
			form.StartPosition = FormStartPosition.Manual;
			var graphicsPath = new GraphicsPath();
			graphicsPath.AddString("★", new FontFamily("メイリオ"), 0, 50, new Point(0, 0), StringFormat.GenericDefault);
			form.Region = new Region(graphicsPath);
			form.Left = 100;
			form.Top = 100;
			form.BackColor = Color.White;
			form.Load += (sender, e) => {
				Animator.Animate(800, (frame, frequency) => {
					if (!form.Visible || form.IsDisposed)
						return false;
					form.Left = 100 + 800 * frame / frequency;
					form.Top = 100 + 800 * frame / frequency;
					if (frame == frequency)
						form.Close();
					return true;
				});
			};
			form.ShowDialog();
		}
	}
	void MyExit() {
		var e = new CancelEventArgs();
		Application.Exit(e);
		if (e.Cancel)
		{
			Console.WriteLine("Application.Exit is canceled.");
		}
	}
	[STAThread]
	static void Main(string[] args) {
		new TaskTrayTest();
		Application.Run();
	}
}




以上。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?