github.com/secoba/wails/v2@v2.6.4/internal/frontend/desktop/windows/winc/utils.go (about) 1 //go:build windows 2 3 /* 4 * Copyright (C) 2019 The Winc Authors. All Rights Reserved. 5 * Copyright (C) 2010-2013 Allen Dang. All Rights Reserved. 6 */ 7 8 package winc 9 10 import ( 11 "fmt" 12 "syscall" 13 "unsafe" 14 15 "github.com/secoba/wails/v2/internal/frontend/desktop/windows/winc/w32" 16 ) 17 18 func internalTrackMouseEvent(hwnd w32.HWND) { 19 var tme w32.TRACKMOUSEEVENT 20 tme.CbSize = uint32(unsafe.Sizeof(tme)) 21 tme.DwFlags = w32.TME_LEAVE 22 tme.HwndTrack = hwnd 23 tme.DwHoverTime = w32.HOVER_DEFAULT 24 25 w32.TrackMouseEvent(&tme) 26 } 27 28 func SetStyle(hwnd w32.HWND, b bool, style int) { 29 originalStyle := int(w32.GetWindowLongPtr(hwnd, w32.GWL_STYLE)) 30 if originalStyle != 0 { 31 if b { 32 originalStyle |= style 33 } else { 34 originalStyle &^= style 35 } 36 w32.SetWindowLongPtr(hwnd, w32.GWL_STYLE, uintptr(originalStyle)) 37 } 38 } 39 40 func SetExStyle(hwnd w32.HWND, b bool, style int) { 41 originalStyle := int(w32.GetWindowLongPtr(hwnd, w32.GWL_EXSTYLE)) 42 if originalStyle != 0 { 43 if b { 44 originalStyle |= style 45 } else { 46 originalStyle &^= style 47 } 48 w32.SetWindowLongPtr(hwnd, w32.GWL_EXSTYLE, uintptr(originalStyle)) 49 } 50 } 51 52 func CreateWindow(className string, parent Controller, exStyle, style uint) w32.HWND { 53 instance := GetAppInstance() 54 var parentHwnd w32.HWND 55 if parent != nil { 56 parentHwnd = parent.Handle() 57 } 58 var hwnd w32.HWND 59 hwnd = w32.CreateWindowEx( 60 exStyle, 61 syscall.StringToUTF16Ptr(className), 62 nil, 63 style, 64 w32.CW_USEDEFAULT, 65 w32.CW_USEDEFAULT, 66 w32.CW_USEDEFAULT, 67 w32.CW_USEDEFAULT, 68 parentHwnd, 69 0, 70 instance, 71 nil) 72 73 if hwnd == 0 { 74 errStr := fmt.Sprintf("Error occurred in CreateWindow(%s, %v, %d, %d)", className, parent, exStyle, style) 75 panic(errStr) 76 } 77 78 return hwnd 79 } 80 81 func RegisterClass(className string, wndproc uintptr) { 82 instance := GetAppInstance() 83 icon := w32.LoadIconWithResourceID(instance, w32.IDI_APPLICATION) 84 85 var wc w32.WNDCLASSEX 86 wc.Size = uint32(unsafe.Sizeof(wc)) 87 wc.Style = w32.CS_HREDRAW | w32.CS_VREDRAW 88 wc.WndProc = wndproc 89 wc.Instance = instance 90 wc.Background = w32.COLOR_BTNFACE + 1 91 wc.Icon = icon 92 wc.Cursor = w32.LoadCursorWithResourceID(0, w32.IDC_ARROW) 93 wc.ClassName = syscall.StringToUTF16Ptr(className) 94 wc.MenuName = nil 95 wc.IconSm = icon 96 97 if ret := w32.RegisterClassEx(&wc); ret == 0 { 98 panic(syscall.GetLastError()) 99 } 100 } 101 102 func RegisterWindowMessage(name string) uint32 { 103 n := syscall.StringToUTF16Ptr(name) 104 105 ret := w32.RegisterWindowMessage(n) 106 if ret == 0 { 107 panic(syscall.GetLastError()) 108 } 109 return ret 110 } 111 112 func getMonitorInfo(hwnd w32.HWND) *w32.MONITORINFO { 113 currentMonitor := w32.MonitorFromWindow(hwnd, w32.MONITOR_DEFAULTTONEAREST) 114 var info w32.MONITORINFO 115 info.CbSize = uint32(unsafe.Sizeof(info)) 116 w32.GetMonitorInfo(currentMonitor, &info) 117 return &info 118 } 119 func getWindowInfo(hwnd w32.HWND) *w32.WINDOWINFO { 120 var info w32.WINDOWINFO 121 info.CbSize = uint32(unsafe.Sizeof(info)) 122 w32.GetWindowInfo(hwnd, &info) 123 return &info 124 } 125 126 func RegClassOnlyOnce(className string) { 127 isExists := false 128 for _, class := range gRegisteredClasses { 129 if class == className { 130 isExists = true 131 break 132 } 133 } 134 135 if !isExists { 136 RegisterClass(className, GeneralWndprocCallBack) 137 gRegisteredClasses = append(gRegisteredClasses, className) 138 } 139 } 140 141 func ScreenToClientRect(hwnd w32.HWND, rect *w32.RECT) *Rect { 142 l, t, r, b := rect.Left, rect.Top, rect.Right, rect.Bottom 143 l1, t1, _ := w32.ScreenToClient(hwnd, int(l), int(t)) 144 r1, b1, _ := w32.ScreenToClient(hwnd, int(r), int(b)) 145 return NewRect(l1, t1, r1, b1) 146 } 147 148 // ScaleWithDPI scales the pixels from the default DPI-Space (96) to the target DPI-Space. 149 func ScaleWithDPI(pixels int, dpi uint) int { 150 return (pixels * int(dpi)) / 96 151 } 152 153 // ScaleToDefaultDPI scales the pixels from scaled DPI-Space to default DPI-Space (96). 154 func ScaleToDefaultDPI(pixels int, dpi uint) int { 155 return (pixels * 96) / int(dpi) 156 }