Refactor, styling improvements.
This commit is contained in:
parent
22038b61ff
commit
1bb9ad397f
5 changed files with 149 additions and 251 deletions
|
@ -266,6 +266,7 @@ func loadFlags() []zenity.Option {
|
||||||
setDefault(&okLabel, "Yes")
|
setDefault(&okLabel, "Yes")
|
||||||
setDefault(&cancelLabel, "No")
|
setDefault(&cancelLabel, "No")
|
||||||
case passwordDlg:
|
case passwordDlg:
|
||||||
|
setDefault(&title, "Type your password")
|
||||||
setDefault(&icon, "dialog-password")
|
setDefault(&icon, "dialog-password")
|
||||||
setDefault(&okLabel, "OK")
|
setDefault(&okLabel, "OK")
|
||||||
setDefault(&cancelLabel, "Cancel")
|
setDefault(&cancelLabel, "Cancel")
|
||||||
|
|
|
@ -61,6 +61,6 @@ func entry(text string, opts options) (string, bool, error) {
|
||||||
|
|
||||||
func password(opts options) (string, string, bool, error) {
|
func password(opts options) (string, string, bool, error) {
|
||||||
opts.hideText = true
|
opts.hideText = true
|
||||||
pass, ok, err := entry("Type your password", opts)
|
pass, ok, err := entry("Password:", opts)
|
||||||
return "", pass, ok, err
|
return "", pass, ok, err
|
||||||
}
|
}
|
||||||
|
|
363
entry_windows.go
363
entry_windows.go
|
@ -14,7 +14,7 @@ func entry(text string, opts options) (string, bool, error) {
|
||||||
if opts.title != nil {
|
if opts.title != nil {
|
||||||
title = *opts.title
|
title = *opts.title
|
||||||
}
|
}
|
||||||
return editBox(title, text, opts.entryText, "ClassEntry", false)
|
return editBox(title, text, opts.entryText, "ClassEntry", opts.hideText)
|
||||||
}
|
}
|
||||||
|
|
||||||
func password(opts options) (string, string, bool, error) {
|
func password(opts options) (string, string, bool, error) {
|
||||||
|
@ -22,89 +22,35 @@ func password(opts options) (string, string, bool, error) {
|
||||||
if opts.title != nil {
|
if opts.title != nil {
|
||||||
title = *opts.title
|
title = *opts.title
|
||||||
}
|
}
|
||||||
pass, ok, err := editBox(title, "Type your password", "", "ClassPassword", true)
|
pass, ok, err := editBox(title, "Password:", "", "ClassPassword", true)
|
||||||
return "", pass, ok, err
|
return "", pass, ok, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
gdi32 = syscall.NewLazyDLL("gdi32.dll")
|
|
||||||
|
|
||||||
createWindowEx = user32.NewProc("CreateWindowExW")
|
createWindowEx = user32.NewProc("CreateWindowExW")
|
||||||
defWindowProcW = user32.NewProc("DefWindowProcW")
|
defWindowProc = user32.NewProc("DefWindowProcW")
|
||||||
destroyWindowW = user32.NewProc("DestroyWindow")
|
destroyWindow = user32.NewProc("DestroyWindow")
|
||||||
dispatchMessage = user32.NewProc("DispatchMessageW")
|
dispatchMessage = user32.NewProc("DispatchMessageW")
|
||||||
postQuitMessageW = user32.NewProc("PostQuitMessage")
|
postQuitMessage = user32.NewProc("PostQuitMessage")
|
||||||
registerClassExW = user32.NewProc("RegisterClassExW")
|
registerClassEx = user32.NewProc("RegisterClassExW")
|
||||||
unregisterClassW = user32.NewProc("UnregisterClassW")
|
unregisterClassW = user32.NewProc("UnregisterClassW")
|
||||||
translateMessage = user32.NewProc("TranslateMessage")
|
translateMessage = user32.NewProc("TranslateMessage")
|
||||||
getWindowTextLengthW = user32.NewProc("GetWindowTextLengthW")
|
getWindowTextLengthW = user32.NewProc("GetWindowTextLengthW")
|
||||||
getWindowTextW = user32.NewProc("GetWindowTextW")
|
getWindowTextW = user32.NewProc("GetWindowTextW")
|
||||||
getWindowLongW = user32.NewProc("GetWindowLongW")
|
getWindowRect = user32.NewProc("GetWindowRect")
|
||||||
setWindowLongW = user32.NewProc("SetWindowLongW")
|
setWindowPos = user32.NewProc("SetWindowPos")
|
||||||
getWindowRectW = user32.NewProc("GetWindowRect")
|
showWindow = user32.NewProc("ShowWindow")
|
||||||
setWindowPosW = user32.NewProc("SetWindowPos")
|
|
||||||
showWindowW = user32.NewProc("ShowWindow")
|
|
||||||
updateWindowW = user32.NewProc("UpdateWindow")
|
|
||||||
isDialogMessage = user32.NewProc("IsDialogMessageW")
|
isDialogMessage = user32.NewProc("IsDialogMessageW")
|
||||||
getSystemMetricsW = user32.NewProc("GetSystemMetrics")
|
getSystemMetricsW = user32.NewProc("GetSystemMetrics")
|
||||||
systemParametersInfoW = user32.NewProc("SystemParametersInfoW")
|
systemParametersInfo = user32.NewProc("SystemParametersInfoW")
|
||||||
getWindowDC = user32.NewProc("GetWindowDC")
|
getWindowDC = user32.NewProc("GetWindowDC")
|
||||||
releaseDC = user32.NewProc("ReleaseDC")
|
releaseDC = user32.NewProc("ReleaseDC")
|
||||||
getDpiForWindow = user32.NewProc("GetDpiForWindow")
|
getDpiForWindow = user32.NewProc("GetDpiForWindow")
|
||||||
|
setFocus = user32.NewProc("SetFocus")
|
||||||
|
|
||||||
|
deleteObject = gdi32.NewProc("DeleteObject")
|
||||||
getDeviceCaps = gdi32.NewProc("GetDeviceCaps")
|
getDeviceCaps = gdi32.NewProc("GetDeviceCaps")
|
||||||
createFontIndirectW = gdi32.NewProc("CreateFontIndirectW")
|
createFontIndirect = gdi32.NewProc("CreateFontIndirectW")
|
||||||
|
|
||||||
getModuleHandleW = kernel32.NewProc("GetModuleHandleW")
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
swShow = 5
|
|
||||||
swShowNormal = 1
|
|
||||||
swUseDefault = 0x80000000
|
|
||||||
|
|
||||||
swpNoZOrder = 0x0004
|
|
||||||
swpNoSize = 0x0001
|
|
||||||
|
|
||||||
smCxScreen = 0
|
|
||||||
smCyScreen = 1
|
|
||||||
|
|
||||||
wsThickFrame = 0x00040000
|
|
||||||
wsSysMenu = 0x00080000
|
|
||||||
wsBorder = 0x00800000
|
|
||||||
wsCaption = 0x00C00000
|
|
||||||
wsChild = 0x40000000
|
|
||||||
wsVisible = 0x10000000
|
|
||||||
wsMaximizeBox = 0x00010000
|
|
||||||
wsMinimizeBox = 0x00020000
|
|
||||||
wsTabStop = 0x00010000
|
|
||||||
wsGroup = 0x00020000
|
|
||||||
wsOverlappedWindow = 0x00CF0000
|
|
||||||
wsExClientEdge = 0x00000200
|
|
||||||
|
|
||||||
wmCreate = 0x0001
|
|
||||||
wmDestroy = 0x0002
|
|
||||||
wmClose = 0x0010
|
|
||||||
wmCommand = 0x0111
|
|
||||||
wmSetFont = 0x0030
|
|
||||||
wmKeydown = 0x0100
|
|
||||||
wmInitDialog = 0x0110
|
|
||||||
|
|
||||||
esPassword = 0x0020
|
|
||||||
esAutoVScroll = 0x0040
|
|
||||||
esAutoHScroll = 0x0080
|
|
||||||
|
|
||||||
dtmFirst = 0x1000
|
|
||||||
dtmGetSystemTime = dtmFirst + 1
|
|
||||||
dtmSetSystemTime = dtmFirst + 2
|
|
||||||
|
|
||||||
vkEscape = 0x1B
|
|
||||||
enUpdate = 0x0400
|
|
||||||
bsPushButton = 0
|
|
||||||
colorWindow = 5
|
|
||||||
spiGetNonClientMetrics = 0x0029
|
|
||||||
gwlStyle = -16
|
|
||||||
maxPath = 260
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-wndclassexw
|
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-wndclassexw
|
||||||
|
@ -183,175 +129,103 @@ type _RECT struct {
|
||||||
bottom int32
|
bottom int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func getModuleHandle() (syscall.Handle, error) {
|
|
||||||
ret, _, err := getModuleHandleW.Call(uintptr(0))
|
|
||||||
if ret == 0 {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return syscall.Handle(ret), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type dpi uintptr
|
type dpi uintptr
|
||||||
|
|
||||||
func (d dpi) Scale(dim uintptr) uintptr {
|
func (d dpi) Scale(dim uintptr) uintptr {
|
||||||
|
if d == 0 {
|
||||||
|
return dim
|
||||||
|
}
|
||||||
return dim * uintptr(d) / 96 // USER_DEFAULT_SCREEN_DPI
|
return dim * uintptr(d) / 96 // USER_DEFAULT_SCREEN_DPI
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDPI(wnd uintptr) (dpi, error) {
|
func getDPI(wnd uintptr) dpi {
|
||||||
var res uintptr = 96
|
var res uintptr
|
||||||
|
|
||||||
if wnd != 0 && getDpiForWindow.Find() == nil {
|
if wnd != 0 && getDpiForWindow.Find() == nil {
|
||||||
res, _, _ = getDpiForWindow.Call(wnd)
|
res, _, _ = getDpiForWindow.Call(wnd)
|
||||||
} else {
|
} else if dc, _, _ := getWindowDC.Call(wnd); dc != 0 {
|
||||||
dc, _, err := getWindowDC.Call(wnd)
|
|
||||||
if dc == 0 {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
defer releaseDC.Call(0, dc)
|
defer releaseDC.Call(0, dc)
|
||||||
|
|
||||||
res, _, _ = getDeviceCaps.Call(dc, 90) // LOGPIXELSY
|
res, _, _ = getDeviceCaps.Call(dc, 90) // LOGPIXELSY
|
||||||
}
|
}
|
||||||
|
|
||||||
return dpi(res), nil
|
if res == 0 {
|
||||||
|
return 96 // USER_DEFAULT_SCREEN_DPI
|
||||||
|
}
|
||||||
|
return dpi(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func createWindow(exStyle uint64, className, windowName string, style, x, y, width, height uintptr,
|
func createWindow(exStyle uint64, className, windowName string, style, x, y, width, height,
|
||||||
parent, menu, instance syscall.Handle) (syscall.Handle, error) {
|
parent, menu, instance uintptr) (uintptr, error) {
|
||||||
ret, _, err := createWindowEx.Call(uintptr(exStyle), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(className))),
|
ret, _, err := createWindowEx.Call(uintptr(exStyle), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(className))),
|
||||||
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(windowName))), uintptr(style), x, y,
|
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(windowName))), style, x, y,
|
||||||
width, height, uintptr(parent), uintptr(menu), uintptr(instance), uintptr(0))
|
width, height, parent, menu, instance, 0)
|
||||||
|
|
||||||
if ret == 0 {
|
if ret == 0 {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return syscall.Handle(ret), nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func destroyWindow(hwnd syscall.Handle) error {
|
func unregisterClass(className string, instance uintptr) bool {
|
||||||
ret, _, err := destroyWindowW.Call(uintptr(hwnd))
|
ret, _, _ := unregisterClassW.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(className))), instance)
|
||||||
if ret == 0 {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func defWindowProc(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) uintptr {
|
|
||||||
ret, _, _ := defWindowProcW.Call(uintptr(hwnd), uintptr(msg), uintptr(wparam), uintptr(lparam))
|
|
||||||
return uintptr(ret)
|
|
||||||
}
|
|
||||||
|
|
||||||
func registerClassEx(wcx *_WNDCLASSEX) (uint16, error) {
|
|
||||||
ret, _, err := registerClassExW.Call(uintptr(unsafe.Pointer(wcx)))
|
|
||||||
|
|
||||||
if ret == 0 {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return uint16(ret), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func unregisterClass(className string, instance syscall.Handle) bool {
|
|
||||||
ret, _, _ := unregisterClassW.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(className))), uintptr(instance))
|
|
||||||
|
|
||||||
return ret != 0
|
return ret != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func postQuitMessage(exitCode int32) {
|
func getWindowTextLength(hwnd uintptr) int {
|
||||||
postQuitMessageW.Call(uintptr(exitCode))
|
ret, _, _ := getWindowTextLengthW.Call(hwnd)
|
||||||
}
|
|
||||||
|
|
||||||
func getWindowTextLength(hwnd syscall.Handle) int {
|
|
||||||
ret, _, _ := getWindowTextLengthW.Call(uintptr(hwnd))
|
|
||||||
return int(ret)
|
return int(ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getWindowText(hwnd syscall.Handle) string {
|
func getWindowText(hwnd uintptr) string {
|
||||||
textLen := getWindowTextLength(hwnd) + 1
|
textLen := getWindowTextLength(hwnd) + 1
|
||||||
|
|
||||||
buf := make([]uint16, textLen)
|
buf := make([]uint16, textLen)
|
||||||
getWindowTextW.Call(uintptr(hwnd), uintptr(unsafe.Pointer(&buf[0])), uintptr(textLen))
|
getWindowTextW.Call(hwnd, uintptr(unsafe.Pointer(&buf[0])), uintptr(textLen))
|
||||||
|
|
||||||
return syscall.UTF16ToString(buf)
|
return syscall.UTF16ToString(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
func systemParametersInfo(uiAction, uiParam uint32, pvParam unsafe.Pointer, fWinIni uint32) bool {
|
|
||||||
ret, _, _ := systemParametersInfoW.Call(uintptr(uiAction), uintptr(uiParam), uintptr(pvParam), uintptr(fWinIni), 0, 0)
|
|
||||||
return int32(ret) != 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func createFontIndirect(lplf *_LOGFONT) uintptr {
|
|
||||||
ret, _, _ := createFontIndirectW.Call(uintptr(unsafe.Pointer(lplf)), 0, 0)
|
|
||||||
return uintptr(ret)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getWindowLong(hwnd syscall.Handle, index int32) int32 {
|
|
||||||
ret, _, _ := getWindowLongW.Call(uintptr(hwnd), uintptr(index), 0)
|
|
||||||
return int32(ret)
|
|
||||||
}
|
|
||||||
|
|
||||||
func setWindowLong(hwnd syscall.Handle, index, value int32) int32 {
|
|
||||||
ret, _, _ := setWindowLongW.Call(uintptr(hwnd), uintptr(index), uintptr(value))
|
|
||||||
return int32(ret)
|
|
||||||
}
|
|
||||||
|
|
||||||
func getWindowRect(hwnd syscall.Handle, rect *_RECT) bool {
|
|
||||||
ret, _, _ := getWindowRectW.Call(uintptr(hwnd), uintptr(unsafe.Pointer(rect)), 0)
|
|
||||||
return ret != 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func setWindowPos(hwnd, hwndInsertAfter syscall.Handle, x, y, width, height int32, flags uint32) bool {
|
|
||||||
ret, _, _ := setWindowPosW.Call(uintptr(hwnd), uintptr(hwndInsertAfter),
|
|
||||||
uintptr(x), uintptr(y), uintptr(width), uintptr(height), uintptr(flags), 0, 0)
|
|
||||||
return ret != 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func showWindow(hwnd syscall.Handle, nCmdShow int32) bool {
|
|
||||||
ret, _, _ := showWindowW.Call(uintptr(hwnd), uintptr(nCmdShow), 0)
|
|
||||||
return ret != 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func updateWindow(hwnd syscall.Handle) bool {
|
|
||||||
ret, _, _ := updateWindowW.Call(uintptr(hwnd), 0, 0)
|
|
||||||
return ret != 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func getSystemMetrics(nindex int32) int32 {
|
func getSystemMetrics(nindex int32) int32 {
|
||||||
ret, _, _ := getSystemMetricsW.Call(uintptr(nindex), 0, 0)
|
ret, _, _ := getSystemMetricsW.Call(uintptr(nindex), 0, 0)
|
||||||
return int32(ret)
|
return int32(ret)
|
||||||
}
|
}
|
||||||
|
|
||||||
func centerWindow(hwnd syscall.Handle) {
|
func centerWindow(wnd uintptr) {
|
||||||
var rc _RECT
|
var rect _RECT
|
||||||
getWindowRect(hwnd, &rc)
|
getWindowRect.Call(wnd, uintptr(unsafe.Pointer(&rect)))
|
||||||
xPos := (getSystemMetrics(smCxScreen) - (rc.right - rc.left)) / 2
|
x := (getSystemMetrics(0 /* SM_CXSCREEN */) - (rect.right - rect.left)) / 2
|
||||||
yPos := (getSystemMetrics(smCyScreen) - (rc.bottom - rc.top)) / 2
|
y := (getSystemMetrics(1 /* SM_CYSCREEN */) - (rect.bottom - rect.top)) / 2
|
||||||
setWindowPos(hwnd, 0, xPos, yPos, 0, 0, swpNoZOrder|swpNoSize)
|
setWindowPos.Call(wnd, 0, uintptr(x), uintptr(y), 0, 0, 0x5) // SWP_NOZORDER|SWP_NOSIZE
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMessageFont() uintptr {
|
func getMessageFont() uintptr {
|
||||||
var metrics _NONCLIENTMETRICS
|
var metrics _NONCLIENTMETRICS
|
||||||
metrics.Size = uint32(unsafe.Sizeof(metrics))
|
metrics.Size = uint32(unsafe.Sizeof(metrics))
|
||||||
systemParametersInfo(spiGetNonClientMetrics, uint32(unsafe.Sizeof(metrics)), unsafe.Pointer(&metrics), 0)
|
systemParametersInfo.Call(0x29, /* SPI_GETNONCLIENTMETRICS */
|
||||||
return createFontIndirect(&metrics.MessageFont)
|
unsafe.Sizeof(metrics), uintptr(unsafe.Pointer(&metrics)), 0)
|
||||||
|
ret, _, _ := createFontIndirect.Call(uintptr(unsafe.Pointer(&metrics.MessageFont)))
|
||||||
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerClass(className string, instance syscall.Handle, fn interface{}) error {
|
func registerClass(className string, instance uintptr, proc interface{}) error {
|
||||||
var wcx _WNDCLASSEX
|
var wcx _WNDCLASSEX
|
||||||
wcx.Size = uint32(unsafe.Sizeof(wcx))
|
wcx.Size = uint32(unsafe.Sizeof(wcx))
|
||||||
wcx.WndProc = syscall.NewCallback(fn)
|
wcx.WndProc = syscall.NewCallback(proc)
|
||||||
wcx.Instance = uintptr(instance)
|
wcx.Instance = instance
|
||||||
wcx.Background = colorWindow + 1
|
wcx.Background = 5 // COLOR_WINDOW
|
||||||
wcx.ClassName = syscall.StringToUTF16Ptr(className)
|
wcx.ClassName = syscall.StringToUTF16Ptr(className)
|
||||||
|
|
||||||
_, err := registerClassEx(&wcx)
|
ret, _, err := registerClassEx.Call(uintptr(unsafe.Pointer(&wcx)))
|
||||||
|
if ret == 0 {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// https://docs.microsoft.com/en-us/windows/win32/winmsg/using-messages-and-message-queues
|
// https://docs.microsoft.com/en-us/windows/win32/winmsg/using-messages-and-message-queues
|
||||||
func messageLoop(hwnd uintptr) error {
|
func messageLoop(wnd uintptr) error {
|
||||||
getMessage := getMessage.Addr()
|
getMessage := getMessage.Addr()
|
||||||
isDialogMessage := isDialogMessage.Addr()
|
isDialogMessage := isDialogMessage.Addr()
|
||||||
translateMessage := translateMessage.Addr()
|
translateMessage := translateMessage.Addr()
|
||||||
|
@ -367,7 +241,7 @@ func messageLoop(hwnd uintptr) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ret, _, _ = syscall.Syscall(isDialogMessage, 2, hwnd, uintptr(unsafe.Pointer(&msg)), 0)
|
ret, _, _ = syscall.Syscall(isDialogMessage, 2, wnd, uintptr(unsafe.Pointer(&msg)), 0)
|
||||||
if ret == 0 {
|
if ret == 0 {
|
||||||
syscall.Syscall(translateMessage, 1, uintptr(unsafe.Pointer(&msg)), 0, 0)
|
syscall.Syscall(translateMessage, 1, uintptr(unsafe.Pointer(&msg)), 0, 0)
|
||||||
syscall.Syscall(dispatchMessage, 1, uintptr(unsafe.Pointer(&msg)), 0, 0)
|
syscall.Syscall(dispatchMessage, 1, uintptr(unsafe.Pointer(&msg)), 0, 0)
|
||||||
|
@ -376,40 +250,52 @@ func messageLoop(hwnd uintptr) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// editBox displays textedit/inputbox dialog.
|
// editBox displays textedit/inputbox dialog.
|
||||||
func editBox(title, text, defaultText, className string, password bool) (string, bool, error) {
|
func editBox(title, text, defaultText, className string, password bool) (out string, ok bool, err error) {
|
||||||
var out string
|
var wnd, textCtl, editCtl uintptr
|
||||||
notCancledOrClosed := true
|
var okBtn, cancelBtn, extraBtn uintptr
|
||||||
|
defWindowProc := defWindowProc.Addr()
|
||||||
|
|
||||||
var hwndEdit syscall.Handle
|
layout := func(dpi dpi) {
|
||||||
|
setWindowPos.Call(wnd, 0, 0, 0, dpi.Scale(281), dpi.Scale(140), 0x6) // SWP_NOZORDER|SWP_NOMOVE
|
||||||
|
setWindowPos.Call(textCtl, 0, dpi.Scale(12), dpi.Scale(10), dpi.Scale(241), dpi.Scale(16), 0x4) // SWP_NOZORDER
|
||||||
|
setWindowPos.Call(editCtl, 0, dpi.Scale(12), dpi.Scale(30), dpi.Scale(241), dpi.Scale(24), 0x4) // SWP_NOZORDER
|
||||||
|
setWindowPos.Call(okBtn, 0, dpi.Scale(12), dpi.Scale(65), dpi.Scale(75), dpi.Scale(24), 0x4) // SWP_NOZORDER
|
||||||
|
setWindowPos.Call(cancelBtn, 0, dpi.Scale(95), dpi.Scale(65), dpi.Scale(75), dpi.Scale(24), 0x4) // SWP_NOZORDER
|
||||||
|
setWindowPos.Call(extraBtn, 0, dpi.Scale(178), dpi.Scale(65), dpi.Scale(75), dpi.Scale(24), 0x4) // SWP_NOZORDER
|
||||||
|
|
||||||
instance, err := getModuleHandle()
|
font := getMessageFont()
|
||||||
if err != nil {
|
sendMessage.Call(textCtl, 0x0030 /* WM_SETFONT */, font, 0)
|
||||||
return out, false, err
|
sendMessage.Call(editCtl, 0x0030 /* WM_SETFONT */, font, 0)
|
||||||
|
sendMessage.Call(okBtn, 0x0030 /* WM_SETFONT */, font, 0)
|
||||||
|
sendMessage.Call(cancelBtn, 0x0030 /* WM_SETFONT */, font, 0)
|
||||||
|
sendMessage.Call(extraBtn, 0x0030 /* WM_SETFONT */, font, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn := func(hwnd syscall.Handle, msg uint32, wparam, lparam uintptr) uintptr {
|
proc := func(wnd uintptr, msg uint32, wparam, lparam uintptr) uintptr {
|
||||||
switch msg {
|
switch msg {
|
||||||
case wmClose:
|
case 0x0002: // WM_DESTROY
|
||||||
notCancledOrClosed = false
|
postQuitMessage.Call(0)
|
||||||
destroyWindow(hwnd)
|
|
||||||
case wmDestroy:
|
case 0x0010: // WM_CLOSE
|
||||||
postQuitMessage(0)
|
destroyWindow.Call(wnd)
|
||||||
case wmKeydown:
|
|
||||||
if wparam == vkEscape {
|
case 0x0111: // WM_COMMAND
|
||||||
notCancledOrClosed = false
|
switch wparam {
|
||||||
destroyWindow(hwnd)
|
|
||||||
}
|
|
||||||
case wmCommand:
|
|
||||||
if wparam == 100 {
|
|
||||||
out = getWindowText(hwndEdit)
|
|
||||||
destroyWindow(hwnd)
|
|
||||||
} else if wparam == 110 {
|
|
||||||
notCancledOrClosed = false
|
|
||||||
destroyWindow(hwnd)
|
|
||||||
}
|
|
||||||
case 0x2e0: // WM_DPICHANGED
|
|
||||||
default:
|
default:
|
||||||
ret := defWindowProc(hwnd, msg, wparam, lparam)
|
return 1
|
||||||
|
case 1, 6: // IDOK, IDYES
|
||||||
|
out = getWindowText(editCtl)
|
||||||
|
ok = true
|
||||||
|
case 2: // IDCANCEL
|
||||||
|
case 7: // IDNO
|
||||||
|
}
|
||||||
|
destroyWindow.Call(wnd)
|
||||||
|
|
||||||
|
case 0x02e0: // WM_DPICHANGED
|
||||||
|
layout(dpi(uint32(wparam) >> 16))
|
||||||
|
|
||||||
|
default:
|
||||||
|
ret, _, _ := syscall.Syscall6(defWindowProc, 4, wnd, uintptr(msg), wparam, lparam, 0, 0)
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -418,43 +304,50 @@ func editBox(title, text, defaultText, className string, password bool) (string,
|
||||||
|
|
||||||
defer setup()()
|
defer setup()()
|
||||||
|
|
||||||
err = registerClass(className, instance, fn)
|
instance, _, err := getModuleHandle.Call(0)
|
||||||
|
if instance == 0 {
|
||||||
|
return "", false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = registerClass(className, instance, proc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return out, false, err
|
return "", false, err
|
||||||
}
|
}
|
||||||
defer unregisterClass(className, instance)
|
defer unregisterClass(className, instance)
|
||||||
|
|
||||||
dpi, err := getDPI(0)
|
dpi := getDPI(0)
|
||||||
hwnd, _ := createWindow(0, className, title, wsOverlappedWindow, swUseDefault, swUseDefault, dpi.Scale(235), dpi.Scale(140), 0, 0, instance)
|
|
||||||
hwndText, _ := createWindow(0, "STATIC", text, wsChild|wsVisible, dpi.Scale(10), dpi.Scale(10), dpi.Scale(200), dpi.Scale(16), hwnd, 0, instance)
|
|
||||||
|
|
||||||
flags := wsBorder | wsChild | wsVisible | wsGroup | wsTabStop | esAutoHScroll
|
wnd, _ = createWindow(0x10101, // WS_EX_CONTROLPARENT|WS_EX_WINDOWEDGE|WS_EX_DLGMODALFRAME
|
||||||
|
className, title, 0x84c80000, // WS_POPUPWINDOW|WS_CLIPSIBLINGS|WS_DLGFRAME
|
||||||
|
0x80000000 /* CW_USEDEFAULT */, 0x80000000, /* CW_USEDEFAULT */
|
||||||
|
dpi.Scale(281), dpi.Scale(140), 0, 0, instance)
|
||||||
|
|
||||||
|
textCtl, _ = createWindow(0, "STATIC", text, 0x5002e080, // WS_CHILD|WS_VISIBLE|WS_GROUP|SS_WORDELLIPSIS|SS_EDITCONTROL|SS_NOPREFIX
|
||||||
|
dpi.Scale(12), dpi.Scale(10), dpi.Scale(241), dpi.Scale(16), wnd, 0, instance)
|
||||||
|
|
||||||
|
var flags uintptr = 0x50030080 // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP|ES_AUTOHSCROLL
|
||||||
if password {
|
if password {
|
||||||
flags |= esPassword
|
flags |= 0x20 // ES_PASSWORD
|
||||||
}
|
}
|
||||||
hwndEdit, _ = createWindow(wsExClientEdge, "EDIT", defaultText, uintptr(flags), dpi.Scale(10), dpi.Scale(30), dpi.Scale(200), dpi.Scale(24), hwnd, 0, instance)
|
editCtl, _ = createWindow(0x200, // WS_EX_CLIENTEDGE
|
||||||
|
"EDIT", defaultText, flags,
|
||||||
|
dpi.Scale(12), dpi.Scale(30), dpi.Scale(241), dpi.Scale(24), wnd, 0, instance)
|
||||||
|
|
||||||
hwndOK, _ := createWindow(wsExClientEdge, "BUTTON", "OK", wsChild|wsVisible|bsPushButton|wsGroup|wsTabStop, dpi.Scale(10), dpi.Scale(65), dpi.Scale(90), dpi.Scale(24), hwnd, 100, instance)
|
okBtn, _ = createWindow(0, "BUTTON", "OK", 0x50030001, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP|BS_DEFPUSHBUTTON
|
||||||
hwndCancel, _ := createWindow(wsExClientEdge, "BUTTON", "Cancel", wsChild|wsVisible|bsPushButton|wsGroup|wsTabStop, dpi.Scale(120), dpi.Scale(65), dpi.Scale(90), dpi.Scale(24), hwnd, 110, instance)
|
dpi.Scale(12), dpi.Scale(65), dpi.Scale(75), dpi.Scale(24), wnd, 1 /* IDOK */, instance)
|
||||||
|
cancelBtn, _ = createWindow(0, "BUTTON", "Cancel", 0x50010000, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP
|
||||||
|
dpi.Scale(95), dpi.Scale(65), dpi.Scale(75), dpi.Scale(24), wnd, 2 /* IDCANCEL */, instance)
|
||||||
|
extraBtn, _ = createWindow(0, "BUTTON", "Extra", 0x50010000, // WS_CHILD|WS_VISIBLE|WS_GROUP|WS_TABSTOP
|
||||||
|
dpi.Scale(178), dpi.Scale(65), dpi.Scale(75), dpi.Scale(24), wnd, 7 /* IDNO */, instance)
|
||||||
|
|
||||||
setWindowLong(hwnd, gwlStyle, getWindowLong(hwnd, gwlStyle)^wsMinimizeBox)
|
layout(getDPI(wnd))
|
||||||
setWindowLong(hwnd, gwlStyle, getWindowLong(hwnd, gwlStyle)^wsMaximizeBox)
|
centerWindow(wnd)
|
||||||
|
setFocus.Call(editCtl)
|
||||||
|
showWindow.Call(wnd, 1 /* SW_SHOWNORMAL */, 0)
|
||||||
|
|
||||||
font := getMessageFont()
|
err = messageLoop(wnd)
|
||||||
sendMessage.Call(uintptr(hwndText), wmSetFont, font, 0)
|
|
||||||
sendMessage.Call(uintptr(hwndEdit), wmSetFont, font, 0)
|
|
||||||
sendMessage.Call(uintptr(hwndOK), wmSetFont, font, 0)
|
|
||||||
sendMessage.Call(uintptr(hwndCancel), wmSetFont, font, 0)
|
|
||||||
|
|
||||||
centerWindow(hwnd)
|
|
||||||
|
|
||||||
showWindow(hwnd, swShowNormal)
|
|
||||||
updateWindow(hwnd)
|
|
||||||
|
|
||||||
err = messageLoop(uintptr(hwnd))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return out, false, err
|
return "", false, err
|
||||||
}
|
}
|
||||||
|
return out, ok, nil
|
||||||
return out, notCancledOrClosed, nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,7 +56,6 @@ func message(kind messageKind, text string, opts options) (bool, error) {
|
||||||
title = syscall.StringToUTF16Ptr(*opts.title)
|
title = syscall.StringToUTF16Ptr(*opts.title)
|
||||||
}
|
}
|
||||||
|
|
||||||
activate()
|
|
||||||
s, _, err := messageBox.Call(0,
|
s, _, err := messageBox.Call(0,
|
||||||
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))),
|
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))),
|
||||||
uintptr(unsafe.Pointer(title)), flags)
|
uintptr(unsafe.Pointer(title)), flags)
|
||||||
|
|
|
@ -13,6 +13,7 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
comdlg32 = syscall.NewLazyDLL("comdlg32.dll")
|
comdlg32 = syscall.NewLazyDLL("comdlg32.dll")
|
||||||
|
gdi32 = syscall.NewLazyDLL("gdi32.dll")
|
||||||
kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
||||||
ole32 = syscall.NewLazyDLL("ole32.dll")
|
ole32 = syscall.NewLazyDLL("ole32.dll")
|
||||||
shell32 = syscall.NewLazyDLL("shell32.dll")
|
shell32 = syscall.NewLazyDLL("shell32.dll")
|
||||||
|
@ -21,6 +22,7 @@ var (
|
||||||
|
|
||||||
commDlgExtendedError = comdlg32.NewProc("CommDlgExtendedError")
|
commDlgExtendedError = comdlg32.NewProc("CommDlgExtendedError")
|
||||||
|
|
||||||
|
getModuleHandle = kernel32.NewProc("GetModuleHandleW")
|
||||||
getCurrentThreadId = kernel32.NewProc("GetCurrentThreadId")
|
getCurrentThreadId = kernel32.NewProc("GetCurrentThreadId")
|
||||||
getConsoleWindow = kernel32.NewProc("GetConsoleWindow")
|
getConsoleWindow = kernel32.NewProc("GetConsoleWindow")
|
||||||
|
|
||||||
|
@ -109,9 +111,10 @@ func hookDialog(ctx context.Context, initDialog func(wnd uintptr)) (unhook conte
|
||||||
}
|
}
|
||||||
|
|
||||||
var hook, wnd uintptr
|
var hook, wnd uintptr
|
||||||
|
callNextHookEx := callNextHookEx.Addr()
|
||||||
tid, _, _ := getCurrentThreadId.Call()
|
tid, _, _ := getCurrentThreadId.Call()
|
||||||
hook, _, err = setWindowsHookEx.Call(12, // WH_CALLWNDPROCRET
|
hook, _, err = setWindowsHookEx.Call(12, // WH_CALLWNDPROCRET
|
||||||
syscall.NewCallback(func(code int, wparam uintptr, lparam *_CWPRETSTRUCT) uintptr {
|
syscall.NewCallback(func(code int32, wparam uintptr, lparam *_CWPRETSTRUCT) uintptr {
|
||||||
if lparam.Message == 0x0110 { // WM_INITDIALOG
|
if lparam.Message == 0x0110 { // WM_INITDIALOG
|
||||||
name := [8]uint16{}
|
name := [8]uint16{}
|
||||||
getClassName.Call(lparam.Wnd, uintptr(unsafe.Pointer(&name)), uintptr(len(name)))
|
getClassName.Call(lparam.Wnd, uintptr(unsafe.Pointer(&name)), uintptr(len(name)))
|
||||||
|
@ -131,7 +134,9 @@ func hookDialog(ctx context.Context, initDialog func(wnd uintptr)) (unhook conte
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
next, _, _ := callNextHookEx.Call(hook, uintptr(code), wparam, uintptr(unsafe.Pointer(lparam)))
|
next, _, _ := syscall.Syscall6(callNextHookEx, 4,
|
||||||
|
hook, uintptr(code), wparam, uintptr(unsafe.Pointer(lparam)),
|
||||||
|
0, 0)
|
||||||
return next
|
return next
|
||||||
}), 0, tid)
|
}), 0, tid)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue