用golang写一个win消息通知
时间: 2024-04-29 17:22:33 浏览: 13
好的,我可以为您提供帮助。以下是使用golang编写Windows消息通知的示例代码:
```go
package main
import (
"fmt"
"syscall"
"unsafe"
)
const (
// 用户消息ID(WM_USER和WM_APP之间的值可以用来定义应用程序特定的消息。)
WM_USER = 0x0400
// 发送通知消息
NIN_BALLOONSHOW = WM_USER + 2
// 隐藏通知消息
NIN_BALLOONHIDE = WM_USER + 3
// 卸载通知消息
NIN_BALLOONTIMEOUT = WM_USER + 4
)
// 定义通知的结构体
type NOTIFYICONDATA struct {
cbSize uint32
hWnd syscall.Handle
uID uint32
uFlags uint32
uCallbackMessage uint32
hIcon syscall.Handle
szTip [128]uint16
dwState uint32
dwStateMask uint32
szInfo [256]uint16
uTimeout uint32
szInfoTitle [64]uint16
dwInfoFlags uint32
guidItem syscall.GUID
hBalloonIcon syscall.Handle
}
// 显示通知函数
func showNotification(title, text string) {
nid := NOTIFYICONDATA{
cbSize: uint32(unsafe.Sizeof(NOTIFYICONDATA{})),
hWnd: syscall.Handle(0),
uID: 0,
uFlags: 0x00000010 | 0x00000020,
uCallbackMessage: 0,
hIcon: syscall.Handle(0),
szTip: [128]uint16{},
dwState: 0,
dwStateMask: 0,
szInfo: [256]uint16{},
uTimeout: 0,
szInfoTitle: [64]uint16{},
dwInfoFlags: 0,
guidItem: syscall.GUID{},
hBalloonIcon: syscall.Handle(0),
}
syscall.UTF16FromString(title)[:copy(nid.szInfoTitle[:], syscall.UTF16FromString(title))]
syscall.UTF16FromString(text)[:copy(nid.szInfo[:], syscall.UTF16FromString(text))]
_ = Shell_NotifyIcon(NIN_BALLOONSHOW, uintptr(unsafe.Pointer(&nid)))
}
// 隐藏通知函数
func hideNotification() {
nid := NOTIFYICONDATA{
cbSize: uint32(unsafe.Sizeof(NOTIFYICONDATA{})),
hWnd: syscall.Handle(0),
uID: 0,
}
_ = Shell_NotifyIcon(NIN_BALLOONHIDE, uintptr(unsafe.Pointer(&nid)))
}
// 超时隐藏通知函数
func timeoutNotification() {
nid := NOTIFYICONDATA{
cbSize: uint32(unsafe.Sizeof(NOTIFYICONDATA{})),
hWnd: syscall.Handle(0),
uID: 0,
}
_ = Shell_NotifyIcon(NIN_BALLOONTIMEOUT, uintptr(unsafe.Pointer(&nid)))
}
// 收到Windows发送通知后的回调函数,NIN_BALLOONUSER代表通知已经被用户关闭
func notifyCallback(hwnd uintptr, msg uint32, nptr uintptr, lptr uintptr) uintptr {
if msg == NIN_BALLOONUSER {
go func() {
hideNotification()
timeoutNotification()
}()
}
return 0
}
// Shell_NotifyIcon函数
var shell32 = syscall.NewLazyDLL("shell32.dll")
var Shell_NotifyIcon = shell32.NewProc("Shell_NotifyIconW")
func main() {
// 显示通知
showNotification("Hello, World!", "这是一个Golang编写的Windows消息通知示例。")
// 使用回调函数接收通知消息
syscall.SetProcessDPIAware()
msgReceiver := syscall.Handle(HWND_MESSAGE)
if err := RegisterClass(&WndClass{
LpszClassName: syscall.StringToUTF16Ptr("Message-only window class"),
HInstance: GetModuleHandle(nil),
LpfnWndProc: syscall.NewCallback(notifyCallback),
}); err != nil {
panic(err)
}
hwnd, err := CreateWindow(syscall.StringToUTF16Ptr("Message-only window class"), nil, 0, 0, 0, 0, 0, msgReceiver, 0, GetModuleHandle(nil), nil)
if err != nil {
panic(err)
}
defer DestroyWindow(hwnd)
var msg MSG
fmt.Println("Start running...")
for {
ret, err := GetMessage(&msg, 0, 0, 0)
if err != nil {
break
} else if ret == 0 {
break
} else {
TranslateMessage(&msg)
DispatchMessage(&msg)
}
}
// 隐藏通知
hideNotification()
}
```
运行代码后,即可看到Windows的消息通知弹出窗口。如果需要隐藏通知窗口,可以调用hideNotification()函数。如果需要在一定时间后自动隐藏通知窗口,可以调用timeoutNotification()函数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)