简体   繁体   English

如何获取Mac OS X Dock的位置,宽度和高度? 可可/碳/ C ++ / Qt

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

I'm trying to get position and width of Mac OS X Dock in my C++/Qt app. 我正在尝试在C ++ / Qt应用程序中获取Mac OS X Dock的位置和宽度。 But I can only find ways to get Available space of Desktop, it means I can get Dock height, but not width. 但是我只能找到获取桌面可用空间的方法,这意味着我可以获取Dock的高度,但不能获取宽度。

Is there ways to get Dock position and width using native OS API? 是否有使用本地OS API获取Dock位置和宽度的方法?

This might help in a hack-free solution, NSScreen provides a method ( visibleframe ) which subtracts the menu and the Dock from the screen size. 这可能对无黑客的解决方案有所帮助,NSScreen提供了一种方法( visibleframe ),该方法从屏幕大小中减去菜单和Dock。 The frame method contains both. frame方法包含两者。

[NSStatusBar system​Status​Bar].thickness will return the height of the Menu bar. [NSStatusBar system​Status​Bar].thickness将返回菜单栏的高度。

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

Doesn't see like such a thing is possible, not even from the apple APIs, much less from Qt. 甚至从Apple API来看,似乎都不可能做到这一点,而从Qt来看更是如此。

The only solution that comes to mind, a rather crude one, is to take a screenshot and use some basic image recognition to find the position and dimensions of the dock. 想到的唯一解决方案是一个相当粗糙的解决方案,它是截取屏幕截图并使用一些基本的图像识别来找到扩展坞的位置和尺寸。

To expand upon MacAndor's answer, you can infer the dock position by comparing the -[NSScreen visibleFrame] (which excludes the space occupied by the dock and the menu bar) with the -[NSScreen frame] which encompasses the entire screen width and height. 要扩展MacAndor的答案,您可以通过将-[NSScreen visibleFrame] (不包括扩展坞和菜单栏所占用的空间)与包含整个屏幕宽度和高度的-[NSScreen frame]进行比较来推断扩展坞位置。

The example code below is dependent on the screen the window resides on. 下面的示例代码取决于窗口所在的屏幕。 This code can be adapted to work with multiple displays by enumerating through all screens instead of using the window's screen. 通过枚举所有屏幕而不是使用窗口的屏幕,此代码可适用于多个显示器。

// 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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM