//go:build windows // ============================================================================ // tageditor_windows.go — 标签编辑输入窗口(难点2) // // 右键菜单点「标签(&T)」后弹出的小窗口:输入「标签」和「备注」两栏, // 确定时写入标签数据库(SaveTag),取消/关闭则不写。 // // 实现:仿 mouseAlertLoop 的窗口线程模式(runtime.LockOSThread + RegisterClassExW // + CreateWindowExW + GetMessage 消息循环 + 自有 WndProc)。不用 Wails 前端模态 // 是因为右键场景前台是 explorer,切到 Wails 窗口体验割裂且需新增前端 UI+事件通道。 // ============================================================================ package main import ( "fmt" "os" "runtime" "sync" "syscall" "unsafe" ) // 标签编辑窗口控件 ID const ( tagEditID = 1001 // 标签 EDIT noteEditID = 1002 // 备注 EDIT(多行) btnOKID = 1003 // 确定 btnCancelID = 1004 // 取消 tagEditorWidth = 400 // 逻辑宽度(px @96DPI),创建时按 DPI 缩放 tagEditorHeight = 260 // 逻辑高度(px @96DPI),创建时按 DPI 缩放 ) // 标准控件样式 const ( wsChild = 0x40000000 wsVisible = 0x10000000 wsTabStop = 0x00010000 wsGroup = 0x00020000 wsBorder = 0x00800000 wsCaption = 0x00C00000 wsSysMenu = 0x00080000 wsThickFrame = 0x00040000 wsOverlapped = 0x00000000 wsExDlgModal = 0x00000001 wsExTopMost = 0x00000008 wsExAppWindow = 0x00040000 esMultiLine = 0x0004 esAutoHScroll = 0x0080 esAutoVScroll = 0x0040 bsDefPushButton = 0x0001 ) // 本文件用到的 user32 过程(procCreateWindowEx/procGetMessage/procRegisterClassEx/ // procDefWindowProc/procDestroyWindow/procShowWindow/procSetForegroundWindow/ // procGetSystemMetrics/procGetModuleHandleW 等已在其它文件定义,此处不复用)。 var ( procGetWindowTextW = user32.NewProc("GetWindowTextW") procSetWindowTextW = user32.NewProc("SetWindowTextW") procGetDlgItem = user32.NewProc("GetDlgItem") procGetWindowTextLengthW = user32.NewProc("GetWindowTextLengthW") procSetFocus = user32.NewProc("SetFocus") procPostQuitMessage = user32.NewProc("PostQuitMessage") procGetClientRect = user32.NewProc("GetClientRect") ) // tagEditor 标签编辑窗口状态(单实例,包级)。 var tagEditor struct { mu sync.Mutex hwnd uintptr path string cb uintptr // WndProc 回调地址(保持引用防 GC) classOK bool started bool } // openTagEditor 弹出标签编辑窗口(由右键菜单回调异步调用)。 // 会启动一个 LockOSThread 的窗口线程。path 为规范化前的完整路径(显示用原始, // 写入库时由 SaveTag 再规范化)。 func openTagEditor(path string) { if path == "" { return } tagEditor.mu.Lock() if tagEditor.started { // 已有一个编辑窗口:把新路径切过去(复用窗口,避免多实例)。 hwnd := tagEditor.hwnd tagEditor.path = path tagEditor.mu.Unlock() if hwnd != 0 { procSetForegroundWindow.Call(hwnd) } return } tagEditor.started = true tagEditor.path = path tagEditor.mu.Unlock() go tagEditorLoop() } // tagEditorLoop 标签编辑窗口线程。 func tagEditorLoop() { runtime.LockOSThread() defer runtime.UnlockOSThread() hInst, _, _ := procGetModuleHandleW.Call(0) className, _ := syscall.UTF16PtrFromString("TengTuTagEditor") // 注册窗口类(WndProc 是 Go 回调,须保持地址)。 tagEditor.mu.Lock() tagEditor.cb = syscall.NewCallback(tagEditorWndProc) wc := wndclassex{ CbSize: uint32(unsafe.Sizeof(wndclassex{})), LpfnWndProc: tagEditor.cb, HInstance: hInst, LpszClassName: className, HCursor: 0, } atom, _, _ := procRegisterClassEx.Call(uintptr(unsafe.Pointer(&wc))) if atom == 0 { tagEditor.classOK = false tagEditor.mu.Unlock() fmt.Fprintf(os.Stderr, "[tageditor] RegisterClassEx failed\n") return } tagEditor.classOK = true tagEditor.mu.Unlock() // 按 DPI 缩放窗口尺寸:manifest permonitorv2 下窗口按物理像素创建, // 高 DPI 屏幕(125%/150%/200%)上 380x220 物理像素显得过小(用户反馈 // 「窗口小,看不到按钮/输入框」)。这里把逻辑尺寸乘上缩放因子。 scale := dpiScale() winW := scaleInt(tagEditorWidth, scale) winH := scaleInt(tagEditorHeight, scale) // 屏幕中心定位 sw, _, _ := procGetSystemMetrics.Call(0) // SM_CXSCREEN sh, _, _ := procGetSystemMetrics.Call(1) // SM_CYSCREEN x := int(sw)/2 - winW/2 y := int(sh)/2 - winH/2 // 创建主窗口(带标题栏、系统菜单;模态于 explorer 之上) hwnd, _, _ := procCreateWindowEx.Call( uintptr(wsExTopMost|wsExAppWindow), // 置顶 + 应用窗口 uintptr(unsafe.Pointer(className)), strPtr("添加标签 / 备注"), uintptr(wsOverlapped|wsCaption|wsSysMenu|wsThickFrame), uintptr(x), uintptr(y), uintptr(winW), uintptr(winH), 0, 0, hInst, 0, ) if hwnd == 0 { fmt.Fprintf(os.Stderr, "[tageditor] CreateWindowEx failed\n") tagEditor.mu.Lock() tagEditor.started = false tagEditor.mu.Unlock() return } tagEditor.mu.Lock() tagEditor.hwnd = hwnd tagEditor.mu.Unlock() // 创建控件:标签标签+输入框、备注标签+多行输入框、确定/取消按钮 createTagEditorControls(hwnd, hInst) procShowWindow.Call(hwnd, 1) // SW_SHOWNORMAL procSetForegroundWindow.Call(hwnd) // procGetDlgItem.Call 返回 (hwnd, r2, err);Go 不允许对多返回值直接下标, // 先取第一值(控件句柄)再 SetFocus。 tagEditCtrl, _, _ := procGetDlgItem.Call(hwnd, tagEditID) procSetFocus.Call(tagEditCtrl) // 消息循环(直到 WM_QUIT) var m msg for { ret, _, _ := procGetMessage.Call(uintptr(unsafe.Pointer(&m)), 0, 0, 0) if ret == 0 { break } procTranslateMessage.Call(uintptr(unsafe.Pointer(&m))) procDispatchMessage.Call(uintptr(unsafe.Pointer(&m))) } // 清理 if hwnd != 0 { procDestroyWindow.Call(hwnd) } tagEditor.mu.Lock() tagEditor.hwnd = 0 tagEditor.started = false tagEditor.mu.Unlock() } // createTagEditorControls 创建标签编辑窗口内的控件。 func createTagEditorControls(hwnd, hInst uintptr) { // mk 创建静态标签文本控件(class="STATIC",text 为显示文字)。 // procCreateWindowEx.Call 返回 (hwnd, r2, err),此处只关心句柄,故取 [0]。 // 坐标/尺寸用 int(DPI 缩放后计算),内部转 uintptr。 mk := func(text string, x, y, w, h int) { ts, _ := syscall.UTF16PtrFromString(text) procCreateWindowEx.Call(0, uintptr(unsafe.Pointer(utf16("STATIC"))), uintptr(unsafe.Pointer(ts)), uintptr(wsChild|wsVisible|wsGroup), uintptr(x), uintptr(y), uintptr(w), uintptr(h), hwnd, 0, hInst, 0) } // 按 DPI 缩放控件尺寸与坐标(与窗口同比例),否则高 DPI 屏上输入框/按钮 // 仍按物理像素创建而显得过小、且放不进被放大的窗口。 scale := dpiScale() pad := scaleInt(12, scale) labelH := scaleInt(18, scale) inputH := scaleInt(26, scale) btnW := scaleInt(80, scale) btnH := scaleInt(30, scale) gap := scaleInt(12, scale) // 行间距 labelX := pad labelW := scaleInt(64, scale) inputX := pad + labelW noteH := scaleInt(80, scale) // 备注输入框高度(多行) // 客户区实际尺寸:外层窗口含标题栏+边框,CreateWindowEx 传入的是外层宽高, // 若按外层高度布局按钮会超出客户区被裁切(用户反馈「按钮/输入框显示不全」)。 // 这里用 GetClientRect 取真实客户区,按钮贴客户区底边、备注框贴按钮上方。 var cr rect procGetClientRect.Call(hwnd, uintptr(unsafe.Pointer(&cr))) clientW := int(cr.Right - cr.Left) clientH := int(cr.Bottom - cr.Top) if clientW <= 0 || clientH <= 0 { // 兜底:取外层尺寸(几乎不会发生) clientW = scaleInt(tagEditorWidth, scale) clientH = scaleInt(tagEditorHeight, scale) } btnY := clientH - pad - btnH // 按钮底边贴客户区底边(留 pad) editW := clientW - inputX - pad // 输入框右缘贴客户区右边(留 pad) noteY := pad + inputH + gap // 备注框在标签输入框下方 gap // 备注框高度:按钮上方到备注框顶部之间全部利用(若剩余空间不足则用 noteH) if btnY-noteY-gap < noteH { noteH = btnY - noteY - gap if noteH < scaleInt(40, scale) { noteH = scaleInt(40, scale) // 保底 40px,避免极端情况挤压 } } // 标签 mk("标签:", labelX, pad, labelW, labelH) procCreateWindowEx.Call(0, uintptr(unsafe.Pointer(utf16("EDIT"))), 0, uintptr(wsChild|wsVisible|wsTabStop|wsBorder), uintptr(inputX), uintptr(pad-scaleInt(1, scale)), uintptr(editW), uintptr(inputH), hwnd, tagEditID, hInst, 0) // 备注 mk("备注:", labelX, noteY, labelW, labelH) procCreateWindowEx.Call(0, uintptr(unsafe.Pointer(utf16("EDIT"))), 0, uintptr(wsChild|wsVisible|wsTabStop|wsBorder|esMultiLine|esAutoVScroll|esAutoHScroll), uintptr(inputX), uintptr(noteY-scaleInt(1, scale)), uintptr(editW), uintptr(noteH), hwnd, noteEditID, hInst, 0) // 按钮:取消(左)、确定(右),底边贴客户区底边 procCreateWindowEx.Call(0, uintptr(unsafe.Pointer(utf16("BUTTON"))), uintptr(unsafe.Pointer(utf16("取消"))), uintptr(wsChild|wsVisible|wsTabStop), uintptr(clientW-btnW*2-pad*2), uintptr(btnY), uintptr(btnW), uintptr(btnH), hwnd, btnCancelID, hInst, 0) procCreateWindowEx.Call(0, uintptr(unsafe.Pointer(utf16("BUTTON"))), uintptr(unsafe.Pointer(utf16("确定"))), uintptr(wsChild|wsVisible|wsTabStop|bsDefPushButton), uintptr(clientW-btnW-pad), uintptr(btnY), uintptr(btnW), uintptr(btnH), hwnd, btnOKID, hInst, 0) } // utf16 快捷转 UTF16 指针(临时,须立即使用)。 func utf16(s string) *uint16 { p, _ := syscall.UTF16PtrFromString(s) return p } // tagEditorWndProc 标签编辑窗口消息处理。 func tagEditorWndProc(hwnd uintptr, msgID uint32, wParam uintptr, lParam uintptr) uintptr { switch msgID { case 0x0111: // WM_COMMAND:控件通知 id := uint32(wParam & 0xffff) switch id { case btnOKID: // 读控件文本 → 写库 → 关闭 tagEditor.mu.Lock() path := tagEditor.path tagEditor.mu.Unlock() tagText := getControlText(hwnd, tagEditID) noteText := getControlText(hwnd, noteEditID) if err := getTagSvc().SaveTag(path, tagText, noteText); err != nil { logTagErr("SaveTag", err) // 写库失败:提示并保持窗口打开(先取确定按钮句柄) okBtn, _, _ := procGetDlgItem.Call(hwnd, btnOKID) procSetWindowTextW.Call(okBtn, uintptr(unsafe.Pointer(utf16("确定(失败)")))) } else { procPostQuitMessage.Call(0) } case btnCancelID: procPostQuitMessage.Call(0) } case 0x0010: // WM_CLOSE:用户点标题栏关闭 → 视为取消 procPostQuitMessage.Call(0) case 0x0002: // WM_DESTROY procPostQuitMessage.Call(0) } // msgID 是 uint32,转 uintptr 传入 DefWindowProc; // procDefWindowProc.Call 返回 (ret, r2, err),只需取第一值。 ret, _, _ := procDefWindowProc.Call(hwnd, uintptr(msgID), wParam, lParam) return ret } // getControlText 读取 EDIT 控件文本。 func getControlText(parent, id uintptr) string { edit, _, _ := procGetDlgItem.Call(parent, id) if edit == 0 { return "" } // 先查长度,再读 n, _, _ := procGetWindowTextLengthW.Call(edit) if n == 0 { return "" } buf := make([]uint16, n+1) procGetWindowTextW.Call(edit, uintptr(unsafe.Pointer(&buf[0])), n+1) return syscall.UTF16ToString(buf) }