public void Scribble(string text) { // デスクトップの背景となる画像のサイズを決定する。 var imageWidth = (int)SystemParameters.PrimaryScreenWidth; var imageHeight = (int)SystemParameters.PrimaryScreenHeight; // ファイル名を決定する。 // デスクトップの背景が「単色」に設定されている場合、SystemParametersInfo でファイルパスを指定しても、 // それが以前設定していた画像パスと同じなら、画像の読み込みは行われず、既存の TranscodedWallpaper が使われ、 // 結果として意図しない背景になることがある。 // これに対応するため、日時付きのファイル名にして、同じ画像パスにならないようにする。 // v1.1 では、常に Background.bmp というファイル名だったので、上記現象が発生していた。 var fileName = "Background_" + DateTime.Now.ToString("yyyyMMddTHHmmss,fff", CultureInfo.InvariantCulture) + ".bmp"; var filePath = Path.Combine(currentDirectory, fileName); // 描画 using (var image = new DesktopBackgroundImage(imageWidth, imageHeight)) { scribbler.Scribble(text, image.Graphics, imageWidth, imageHeight); Backup(image.Original); image.Save(filePath); } // デスクトップの背景に設定 SetBackgroundImage(filePath); // 履歴 history.Push(text); }
public void Undo() { // 現在の背景のパスを取得する。 var currentPath = DesktopBackgroundImage.GetCurrentPath(); if (string.IsNullOrWhiteSpace(currentPath)) { 最新の画像を設定(); return; } string currentFullPath; try { currentFullPath = Path.GetFullPath(currentPath); } catch { // GetFullPath で例外が発生するということは、レジストリに無効なパスが設定されていたということ。 // この場合は、このプログラムが分かる最も新しい画像を設定する。 最新の画像を設定(); return; } var directoryName = Path.GetDirectoryName(currentFullPath); if (directoryName == currentDirectory) { // 現在の背景のパスがカレントディレクトリの画像を指しているならば、一つ古い画像を設定する。 最新のバックアップを設定(); return; } if (directoryName == backupDirectory) { // 現在の背景のパスが Backup 内の画像を指しているならば、その画像より一つ古い画像を設定する。 // そのような画像が無ければ何もしない。 if (!Directory.Exists(backupDirectory)) { return; } var currentFileName = Path.GetFileName(currentPath); var files = Directory.EnumerateFiles(backupDirectory) .Select(f => new { Name = Path.GetFileName(f), Path = f }) .Where(f => f.Name.CompareTo(currentFileName) < 0) .OrderByDescending(f => f.Name); if (files.Any()) { SetBackgroundImage(Path.GetFullPath(files.First().Path)); } return; } // ここに到達するということは、有効ではあるが全くあずかり知らぬパスが設定されているということ。 最新の画像を設定(); }
public void Redo() { // 現在の背景のパスを取得する。 var currentPath = DesktopBackgroundImage.GetCurrentPath(); // 現在の背景のパスが無効なパスならばやり直せない。 string currentFullPath; try { currentFullPath = Path.GetFullPath(currentPath); } catch { // GetFullPath で例外が発生するということは、レジストリに無効なパスが設定されていたということ。 // この場合は、やり直せない。 return; } // 現在の背景のパスが Backup 内の画像を指していなければ、やり直せない。 var directoryName = Path.GetDirectoryName(currentPath); if (directoryName != backupDirectory) { return; } // 現在の背景のパスが Backup 内の画像を指しているならば、その画像より一つ新しい画像を設定する。 if (!Directory.Exists(backupDirectory)) { カレントディレクトリの画像を設定(); return; } var currentFileName = Path.GetFileName(currentPath); var files = Directory.EnumerateFiles(backupDirectory) .Select(f => new { Name = Path.GetFileName(f), Path = f }) .Where(f => f.Name.CompareTo(currentFileName) > 0) .OrderBy(f => f.Name); if (files.Any()) { SetBackgroundImage(Path.GetFullPath(files.First().Path)); } else { カレントディレクトリの画像を設定(); } }