mirror of
https://github.com/wailsapp/wails.git
synced 2026-03-14 14:45:49 +01:00
[v3 plugins] Support single-instance on Windows
This commit is contained in:
parent
4ee75e93c9
commit
3b0a57ca6d
2 changed files with 44 additions and 0 deletions
|
|
@ -137,6 +137,7 @@ var (
|
|||
procEnumDisplayDevices = moduser32.NewProc("EnumDisplayDevicesW")
|
||||
procEnumDisplaySettings = moduser32.NewProc("EnumDisplaySettingsW")
|
||||
procEnumDisplaySettingsEx = moduser32.NewProc("EnumDisplaySettingsExW")
|
||||
procEnumWindows = moduser32.NewProc("EnumWindows")
|
||||
procChangeDisplaySettingsEx = moduser32.NewProc("ChangeDisplaySettingsExW")
|
||||
procSendInput = moduser32.NewProc("SendInput")
|
||||
procSetWindowsHookEx = moduser32.NewProc("SetWindowsHookExW")
|
||||
|
|
@ -656,6 +657,14 @@ func ReleaseCapture() bool {
|
|||
return ret != 0
|
||||
}
|
||||
|
||||
func EnumWindows(enumFunc uintptr, lparam uintptr) bool {
|
||||
ret, _, _ := procEnumWindows.Call(
|
||||
enumFunc,
|
||||
lparam)
|
||||
|
||||
return ret != 0
|
||||
}
|
||||
|
||||
func GetWindowThreadProcessId(hwnd HWND) (HANDLE, int) {
|
||||
var processId int
|
||||
ret, _, _ := procGetWindowThreadProcessId.Call(
|
||||
|
|
|
|||
35
v3/plugins/single_instance/plugin_windows.go
Normal file
35
v3/plugins/single_instance/plugin_windows.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
//go:build windows
|
||||
|
||||
package single_instance
|
||||
|
||||
import (
|
||||
"github.com/wailsapp/wails/v3/pkg/w32"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
type enumWindowsCallback func(hwnd syscall.Handle, lParam uintptr) uintptr
|
||||
|
||||
func enumWindowsProc(hwnd syscall.Handle, lParam uintptr) uintptr {
|
||||
_, processID := w32.GetWindowThreadProcessId(uintptr(hwnd))
|
||||
targetProcessID := uint32(lParam)
|
||||
if uint32(processID) == targetProcessID {
|
||||
// Bring the window forward
|
||||
w32.SetForegroundWindow(w32.HWND(hwnd))
|
||||
println("Found window: ", hwnd)
|
||||
}
|
||||
|
||||
// Continue enumeration
|
||||
return 1
|
||||
}
|
||||
|
||||
func (p *Plugin) activeInstance(pid int) error {
|
||||
|
||||
// Get the window associated with the process ID.
|
||||
targetProcessID := uint32(pid) // Replace with the desired process ID
|
||||
|
||||
w32.EnumWindows(
|
||||
syscall.NewCallback(enumWindowsCallback(enumWindowsProc)),
|
||||
uintptr(targetProcessID),
|
||||
)
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue