drsl90685154 2017-12-20 03:09
浏览 185
已采纳

如何获取Windows应用程序的位置和区域(例如“ Google Chrome”)? [关闭]

How can I get pid and Handle, then get the position?

I don't have a clue about that. Please ignore the following code.

package main

import (
    "fmt"

    "syscall"
)

func main () {
    if err := syscall.Setregid(int(233), int(233)); err != nil {
        fmt.Println("setregid:", err)
    }
}
  • 写回答

1条回答 默认 最新

  • doujianzhi3358 2017-12-20 11:34
    关注

    Despite the code you posted (which would only work under Linux), everybody seems to be assuming that you asking how it can be done under Windows.

    As @remy-lebeau mentioned you can use FindWindow() and GetWindowRect() to achieve this:

    package main
    
    import (
        "fmt"
        "syscall"
        "unsafe"
    )
    
    type HWND uintptr
    
    type RECT struct {
        Left, Top, Right, Bottom int32
    }
    
    var (
        user32, _     = syscall.LoadLibrary("user32.dll")
        findWindowW, _ = syscall.GetProcAddress(user32, "FindWindowW")
        getWindowRect, _ = syscall.GetProcAddress(user32, "GetWindowRect")
    )
    
    //HWND WINAPI FindWindow(
    //  _In_opt_ LPCTSTR lpClassName,
    //  _In_opt_ LPCTSTR lpWindowName
    //);
    func FindWindowByTitle(title string) HWND {
        ret, _, _ := syscall.Syscall(
            findWindowW,
            2,
            0,
            uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title))),
            0,
        )
        return HWND(ret)
    }
    
    //BOOL WINAPI GetWindowRect(
    //  _In_  HWND   hWnd,
    //  _Out_ LPRECT lpRect
    //);
    func GetWindowDimensions(hwnd HWND) *RECT {
        var rect RECT
    
        syscall.Syscall(
            getWindowRect,
            2,
            uintptr(hwnd),
            uintptr(unsafe.Pointer(&rect)),
            0,
        )
    
        return &rect
    }
    
    func main() {
        defer syscall.FreeLibrary(user32)
    
        hwnd := FindWindowByTitle("New Tab - Google Chrome")
        fmt.Printf("Return: %d
    ", hwnd)
    
        if hwnd > 0 {
            rect := GetWindowDimensions(hwnd)
            fmt.Printf("Return: %v
    ", rect)
        }
    }
    

    Be aware that for brevity no error handling is done.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容