-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalert_windows.go
56 lines (49 loc) · 1.7 KB
/
alert_windows.go
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
package alert
import (
"github.com/fcjr/alert/internal/user32"
"golang.org/x/sys/windows"
)
// Info displays a basic alert with an "OK" button
func Info(title, message string) error {
var flags uint = user32.MB_ICONINFORMATION | user32.MB_OK
_, err := user32.MessageBoxW(user32.NULL, message, title, flags)
if err != nil {
return err
}
return nil
}
// Error displays a basic error alert with an "OK" button
func Error(title, message string) error {
var flags uint = user32.MB_ICONERROR | user32.MB_OK
_, err := user32.MessageBoxW(user32.NULL, message, title, flags)
if err != nil {
return err
}
return nil
}
// Question displays an alert with two buttons
//
// returns true iff the the defaultButton was pressed
func Question(title, message, defaultButton, alternateButton string) (bool, error) {
// register hook to capture and set custom dialog button text
var hook user32.HHOOK
hook = user32.SetWindowsHookEx(user32.WH_CBT,
(user32.HOOKPROC)(func(nCode int, wparam user32.WPARAM, lparam user32.LPARAM) user32.LRESULT {
if nCode < 0 {
return user32.CallNextHookEx(hook, nCode, wparam, lparam)
}
if nCode == user32.HCBT_ACTIVATE {
// set custom button text
user32.SetDlgItemText(wparam, user32.ID_BUT_YES, defaultButton)
user32.SetDlgItemText(wparam, user32.ID_BUT_NO, alternateButton)
}
return user32.CallNextHookEx(hook, nCode, wparam, lparam)
}), 0, (user32.DWORD)(windows.GetCurrentThreadId()))
defer user32.UnhookWindowsHookEx(hook)
var flags uint = user32.MB_ICONINFORMATION | user32.MB_YESNO | user32.MB_DEFBUTTON1
press, err := user32.MessageBoxW(user32.NULL, message, title, flags)
if err != nil {
return false, err
}
return press == user32.ID_BUT_YES, nil
}