-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathProgram.cs
178 lines (147 loc) · 7.1 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.IO;
using System.Diagnostics;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
using MS.WindowsAPICodePack.Internal;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
namespace ConsoleToast
{
class Program
{
static void Main(string[] args)
{
ShortCutCreator.TryCreateShortcut("ConsoleToast.App", "ConsoleToast");
Console.WriteLine("Type 'exit' to quit. ENTER to show a notification");
while (Console.ReadLine() != "exit")
{
ShowImageToast(
"ConsoleToast.App",
DateTime.Now.ToLongTimeString() + " title with image",
"this is a message",
Path.GetFullPath("plasticlogo.png"));
/*ShowTextToast(
"ConsoleToast.App",
DateTime.Now.ToLongTimeString() + "title",
"this is a message");*/
}
}
static void ShowTextToast(string appId, string title, string message)
{
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(
ToastTemplateType.ToastText02);
// Fill in the text elements
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
stringElements[0].AppendChild(toastXml.CreateTextNode(title));
stringElements[1].AppendChild(toastXml.CreateTextNode(message));
// Create the toast and attach event listeners
ToastNotification toast = new ToastNotification(toastXml);
ToastEvents events = new ToastEvents();
toast.Activated += events.ToastActivated;
toast.Dismissed += events.ToastDismissed;
toast.Failed += events.ToastFailed;
// Show the toast. Be sure to specify the AppUserModelId
// on your application's shortcut!
ToastNotificationManager.CreateToastNotifier(appId).Show(toast);
}
static void ShowImageToast(string appId, string title, string message, string image)
{
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(
ToastTemplateType.ToastImageAndText02);
// Fill in the text elements
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
stringElements[0].AppendChild(toastXml.CreateTextNode(title));
stringElements[1].AppendChild(toastXml.CreateTextNode(message));
// Specify the absolute path to an image
String imagePath = "file:///" + image;
XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
imageElements[0].Attributes.GetNamedItem("src").NodeValue = imagePath;
// Create the toast and attach event listeners
ToastNotification toast = new ToastNotification(toastXml);
ToastEvents events = new ToastEvents();
toast.Activated += events.ToastActivated;
toast.Dismissed += events.ToastDismissed;
toast.Failed += events.ToastFailed;
// Show the toast. Be sure to specify the AppUserModelId
// on your application's shortcut!
ToastNotificationManager.CreateToastNotifier(appId).Show(toast);
}
class ToastEvents
{
internal void ToastActivated(ToastNotification sender, object e)
{
Console.WriteLine("User activated the toast");
}
internal void ToastDismissed(ToastNotification sender, ToastDismissedEventArgs e)
{
String outputText = "";
switch (e.Reason)
{
case ToastDismissalReason.ApplicationHidden:
outputText = "The app hid the toast using ToastNotifier.Hide";
break;
case ToastDismissalReason.UserCanceled:
outputText = "The user dismissed the toast";
break;
case ToastDismissalReason.TimedOut:
outputText = "The toast has timed out";
break;
}
Console.WriteLine(outputText);
}
internal void ToastFailed(ToastNotification sender, ToastFailedEventArgs e)
{
Console.WriteLine("The toast encountered an error.");
}
}
static class ShortCutCreator
{
// In order to display toasts, a desktop application must have
// a shortcut on the Start menu.
// Also, an AppUserModelID must be set on that shortcut.
// The shortcut should be created as part of the installer.
// The following code shows how to create
// a shortcut and assign an AppUserModelID using Windows APIs.
// You must download and include the Windows API Code Pack
// for Microsoft .NET Framework for this code to function
internal static bool TryCreateShortcut(string appId, string appName)
{
String shortcutPath = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData) +
"\\Microsoft\\Windows\\Start Menu\\Programs\\" + appName + ".lnk";
if (!File.Exists(shortcutPath))
{
InstallShortcut(appId, shortcutPath);
return true;
}
return false;
}
static void InstallShortcut(string appId, string shortcutPath)
{
// Find the path to the current executable
String exePath = Process.GetCurrentProcess().MainModule.FileName;
IShellLinkW newShortcut = (IShellLinkW)new CShellLink();
// Create a shortcut to the exe
VerifySucceeded(newShortcut.SetPath(exePath));
VerifySucceeded(newShortcut.SetArguments(""));
// Open the shortcut property store, set the AppUserModelId property
IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut;
using (PropVariant applicationId = new PropVariant(appId))
{
VerifySucceeded(newShortcutProperties.SetValue(
SystemProperties.System.AppUserModel.ID, applicationId));
VerifySucceeded(newShortcutProperties.Commit());
}
// Commit the shortcut to disk
IPersistFile newShortcutSave = (IPersistFile)newShortcut;
VerifySucceeded(newShortcutSave.Save(shortcutPath, true));
}
static void VerifySucceeded(UInt32 hresult)
{
if (hresult <= 1)
return;
throw new Exception("Failed with HRESULT: " + hresult.ToString("X"));
}
}
}
}