如何获取Mac OS X Dock 的位置、宽度和高度? Cocoa/Carbon/C++/Qt

How to get Position, Width and Height of Mac OS X Dock? Cocoa/Carbon/C++/Qt

我试图在我的 C++/Qt 应用程序中获取 Mac OS X Dock 的位置和宽度。 但是我只能想方设法获取桌面的可用 space,这意味着我可以获得 Dock 高度,但不能获取宽度。

有没有办法使用原生 OS API 获取 Dock 位置和宽度?

不认为这样的事情是可能的,即使是从 apple API 中,更不用说从 Qt 中了。

想到的唯一解决方案,一个相当粗糙的解决方案,就是截屏并使用一些基本的图像识别来找到扩展坞的位置和尺寸。

这可能有助于无黑客攻击的解决方案,NSScreen 提供了一种方法 (visibleframe),可以从屏幕尺寸中减去菜单和 Dock。 frame 方法包含两者。

[NSStatusBar system​Status​Bar].thickness 将 return 菜单栏的高度。

https://developer.apple.com/reference/appkit/nsscreen/1388369-visibleframe?language=objc

要扩展 MacAndor 的答案,您可以通过比较包含整个屏幕宽度和高度的 -[NSScreen visibleFrame] (which excludes the space occupied by the dock and the menu bar) with the -[NSScreen frame] 来推断停靠位置。

下面的示例代码取决于 window 所在的屏幕。通过枚举所有屏幕而不是使用 window 的屏幕,此代码可以适用于多个显示器。

// Infer the dock position (left, bottom, right)
NSScreen *screen = [self.window screen];    
NSRect visibleFrame = [screen visibleFrame];
NSRect screenFrame = screen.frame;

if (visibleFrame.origin.x > screenFrame.origin.x) {
    NSLog(@"Dock is positioned on the LEFT");
} else if (visibleFrame.origin.y > screenFrame.origin.y) {
    NSLog(@"Dock is positioned on the BOTTOM");
} else if (visibleFrame.size.width < screenFrame.size.width) {
    NSLog(@"Dock is positioned on the RIGHT");
} else {
    NSLog(@"Dock is HIDDEN");
}