使用Golang实现自定义鼠标指针图片替换

使用Golang实现自定义鼠标指针图片替换 你好, 我正在寻找一个能够让我用图像替换鼠标指针的库。

我一直在尝试看看是否拥有在 Go 语言中创建取色器/颜色选择器功能所需的所有组件。 光标将被替换为鼠标位置下桌面的放大区域。

谢谢

1 回复

更多关于使用Golang实现自定义鼠标指针图片替换的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


在Go中实现自定义鼠标指针图片替换,可以通过以下方案实现:

package main

import (
    "fmt"
    "image"
    "runtime"
    "time"

    "github.com/go-vgo/robotgo"
    "github.com/kbinani/screenshot"
    "golang.org/x/exp/shiny/screen"
    "golang.org/x/mobile/event/mouse"
)

// 自定义光标管理器
type CustomCursor struct {
    cursorImage image.Image
    screen      screen.Screen
    window      screen.Window
    scale       int // 放大倍数
    regionSize  int // 捕获区域大小
}

func NewCustomCursor(scale, regionSize int) (*CustomCursor, error) {
    cc := &CustomCursor{
        scale:      scale,
        regionSize: regionSize,
    }
    
    // 创建自定义光标图像(这里使用放大桌面区域)
    err := cc.updateCursorImage()
    if err != nil {
        return nil, err
    }
    
    return cc, nil
}

func (cc *CustomCursor) updateCursorImage() error {
    // 获取当前鼠标位置
    x, y := robotgo.GetMousePos()
    
    // 计算捕获区域
    captureX := x - cc.regionSize/2
    captureY := y - cc.regionSize/2
    captureWidth := cc.regionSize
    captureHeight := cc.regionSize
    
    // 捕获桌面区域
    bounds := image.Rect(captureX, captureY, captureX+captureWidth, captureY+captureHeight)
    img, err := screenshot.CaptureRect(bounds)
    if err != nil {
        return err
    }
    
    // 放大图像
    cc.cursorImage = scaleImage(img, cc.scale)
    return nil
}

// 图像缩放函数
func scaleImage(src image.Image, scale int) image.Image {
    bounds := src.Bounds()
    dst := image.NewRGBA(image.Rect(0, 0, bounds.Dx()*scale, bounds.Dy()*scale))
    
    for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
        for x := bounds.Min.X; x < bounds.Max.X; x++ {
            color := src.At(x, y)
            for dy := 0; dy < scale; dy++ {
                for dx := 0; dx < scale; dx++ {
                    dst.Set(x*scale+dx, y*scale+dy, color)
                }
            }
        }
    }
    return dst
}

// 启动光标更新循环
func (cc *CustomCursor) Start() {
    go func() {
        for {
            cc.updateCursorImage()
            time.Sleep(50 * time.Millisecond) // 20fps更新频率
        }
    }()
}

// 主程序
func main() {
    // 根据操作系统选择实现方式
    switch runtime.GOOS {
    case "windows":
        runWindowsImplementation()
    case "darwin":
        runMacImplementation()
    case "linux":
        runLinuxImplementation()
    default:
        fmt.Println("Unsupported operating system")
    }
}

// Windows实现
func runWindowsImplementation() {
    cc, err := NewCustomCursor(3, 20) // 3倍放大,20x20区域
    if err != nil {
        panic(err)
    }
    
    cc.Start()
    
    // 保持程序运行
    select {}
}

// 颜色选择器功能示例
type ColorPicker struct {
    lastColor string
}

func (cp *ColorPicker) GetColorAt(x, y int) (string, error) {
    // 使用robotgo获取屏幕颜色
    color := robotgo.GetPixelColor(x, y)
    cp.lastColor = color
    return color, nil
}

// 创建系统托盘图标(可选)
func createSystemTray() {
    // 使用walk或fyne等GUI库创建系统托盘
    // 这里需要具体的GUI库实现
}

// 鼠标事件处理
func handleMouseEvents() {
    // 监听鼠标移动事件
    robotgo.EventHook(robotgo.MouseMove, []string{}, func(e robotgo.Event) {
        // 更新光标位置
        x, y := robotgo.GetMousePos()
        fmt.Printf("Mouse moved to: %d, %d\n", x, y)
    })
    
    robotgo.EventHook(robotgo.MouseDown, []string{}, func(e robotgo.Event) {
        fmt.Println("Mouse button pressed")
    })
    
    robotgo.EventStart()
}

对于跨平台实现,还需要考虑以下平台特定代码:

// Linux实现(使用X11)
func runLinuxImplementation() {
    // 需要安装xorg-dev依赖
    // 使用github.com/BurntSushi/xgb或github.com/jezek/xgb包
}

// macOS实现
func runMacImplementation() {
    // 使用Core Graphics框架
    // 可能需要CGo绑定
}

关键依赖包:

  1. github.com/go-vgo/robotgo - 跨平台鼠标/键盘控制
  2. github.com/kbinani/screenshot - 屏幕截图功能
  3. golang.org/x/exp/shiny - 原生GUI支持(实验性)

编译注意事项:

# Windows需要链接user32.dll和gdi32.dll
# Linux需要X11开发库
# macOS需要Core Graphics框架

# 编译命令示例
go build -ldflags="-H windowsgui" main.go  # Windows隐藏控制台

这个实现会持续捕获鼠标位置的桌面区域,放大后作为自定义光标显示。由于Go的标准库不直接提供系统光标替换API,实际部署可能需要结合CGo调用系统API或使用更底层的图形库。

回到顶部