创建Windows任务栏通知

huangapple go评论63阅读模式
英文:

C create Windows Taskbar Notification

问题

如何创建一个类似这样的弹出窗口:[![Popup](https://i.stack.imgur.com/W2hvO.png)](https://i.stack.imgur.com/W2hvO.png)

我尝试了 `Shell_NotifyIcon()`,但它只在任务栏中创建了一个图标,而气球提示在Windows 10中不再起作用。也许我漏掉了什么,但我找不到其他可以实现这个功能的函数。

以下是我的设置:

```cpp
NOTIFYICONDATAA dataa;
dataa.cbSize=sizeof(dataa);
dataa.hWnd=hWnd;
dataa.uID=0;
dataa.uFlags=NIF_ICON | NIF_TIP | NIF_MESSAGE;
dataa.hIcon=LoadIcon(NULL, IDI_QUESTION);
strcpy(dataa.szTip, "Test1");
Shell_NotifyIcon(NIM_ADD, &dataa);
Shell_NotifyIcon(NIM_MODIFY, &dataa); 

<details>
<summary>英文:</summary>

How do i create a Popup like this: [![Popup](https://i.stack.imgur.com/W2hvO.png)](https://i.stack.imgur.com/W2hvO.png)

I try `Shell_NotifyIcon()` but this only creates an image in the Taskbar and the Balloon function don&#180;t work in Windows 10 anymore. Maybe i missed something but i don&#180;t find any other function to do that.

Following is my setup:

NOTIFYICONDATAA dataa;
dataa.cbSize=sizeof(dataa);
dataa.hWnd=hWnd;
dataa.uID=0;
dataa.uFlags=NIF_ICON | NIF_TIP | NIF_MESSAGE;
dataa.hIcon=LoadIcon(NULL, IDI_QUESTION);
strcpy(dataa.szTip, "Test1");
Shell_NotifyIcon(NIM_ADD, &dataa);
Shell_NotifyIcon(NIM_MODIFY, &dataa);


</details>


# 答案1
**得分**: 1

以下是翻译好的部分:

```c
使用以下代码,我可以创建一个类似于这样的通知:

[![通知][1]][1] 

    NOTIFYICONDATAA dataa;
		dataa.uTimeout			=		3000;
		dataa.uVersion			=		NIM_SETVERSION;
		dataa.cbSize			=		sizeof(dataa);
		dataa.hWnd				=		hWnd;
		dataa.uID				=		0;
		dataa.uFlags			=		NIF_ICON | NIF_TIP | NIF_MESSAGE | NIF_INFO;
		dataa.hIcon				=		LoadIcon(NULL, IDI_WARNING);
		dataa.dwInfoFlags		=		NIIF_WARNING;
		strcpy(dataa.szTip, "Test1");
		strcpy(dataa.szInfo, "Test2");
		strcpy(dataa.szInfoTitle, "Test3");
		Shell_NotifyIcon(NIM_ADD, &dataa);
		Shell_NotifyIcon(NIM_SETVERSION, &dataa);

感谢 Remy 和 Jonathan。这似乎比 [toast 通知][2] 选项更容易。
在我的测试中,我只能更改 Test2 和 Test3 字符串。标题字符串我无法更改,我认为这是 Windows 的行为。运行 .exe 文件后,应用程序名称将用作标题字符串。

要生成通知并使用代码中的结构体,**必须设置版本**为

&gt; #define _WIN32_IE 0x0650 // 或其他大于 0x600 的值

然后再包含 shellapi.h。

  [1]: https://i.stack.imgur.com/YVLHv.png
  [2]: https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/toast-notifications-overview
英文:

With following code i can create a Notification like this:

创建Windows任务栏通知

NOTIFYICONDATAA dataa;
	dataa.uTimeout			=		3000;
	dataa.uVersion			=		NIM_SETVERSION;
	dataa.cbSize			=		sizeof(dataa);
	dataa.hWnd				=		hWnd;
	dataa.uID				=		0;
	dataa.uFlags			=		NIF_ICON | NIF_TIP | NIF_MESSAGE | NIF_INFO;
	dataa.hIcon				=		LoadIcon(NULL, IDI_WARNING);
	dataa.dwInfoFlags		=		NIIF_WARNING;
	strcpy(dataa.szTip, &quot;Test1&quot;);
	strcpy(dataa.szInfo, &quot;Test2&quot;);
	strcpy(dataa.szInfoTitle, &quot;Test3&quot;);
	Shell_NotifyIcon(NIM_ADD, &amp;dataa);
	Shell_NotifyIcon(NIM_SETVERSION, &amp;dataa);

Thanks to Remy and Jonathan. This seems easier than the toast notification option.
In my tests i can only change the Test2 and Test3 string. The title string i can not change, i think this do Windows. After running the .exe, the App name will used as title string.

To generate the Notification and use the struct like in code, the Version must set to

> #define _WIN32_IE 0x0650 //or other value above 0x600

before including shellapi.h.

huangapple
  • 本文由 发表于 2023年6月19日 05:12:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定