可可坐标和碳坐标之间的转换?

发布于 2024-10-01 22:44:54 字数 121 浏览 3 评论 0原文

我正在寻找一种强大的(我的意思是在所有多显示器配置中工作)算法来将可可点/矩形转换为碳当量以及相反的操作。

我用谷歌搜索过,但找不到可以正常工作的东西。

预先感谢您的帮助。

问候,

I am looking for a robust (I mean working in all multi-munitor configuration) algo to convert a cocoa point/rect to carbon equivalent and the reverse operation.

I've googled but was unable to find something which work properly.

Thanks in advance for your help.

Regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泼猴你往哪里跑 2024-10-08 22:44:54

经过几次迭代后,我在 NSScreen 上找到了以下类别:

+ (NSScreen*) screenWithPoint: (NSPoint) p
{
    for (NSScreen *candidate in [NSScreen screens])
        if (NSPointInRect(p, [candidate frame]))
            return candidate;
    return nil;
}

+ (NSScreen*) screenWithMenuBar
{
    return [self screenWithPoint:NSZeroPoint];
}

+ (float) menuScreenHeight
{
    return NSMaxY([[self screenWithMenuBar] frame]);
}

+ (CGPoint) carbonPointFrom: (NSPoint) cocoaPoint
{
    return CGPointMake(cocoaPoint.x, [self menuScreenHeight] - cocoaPoint.y);
}

+ (NSPoint) cocoaPointFrom: (CGPoint) carbonPoint
{
    return NSMakePoint(carbonPoint.x, [self menuScreenHeight] - carbonPoint.y);
}

即使在多个显示器上也能正常工作。

After several iterations I got to the following category on NSScreen:

+ (NSScreen*) screenWithPoint: (NSPoint) p
{
    for (NSScreen *candidate in [NSScreen screens])
        if (NSPointInRect(p, [candidate frame]))
            return candidate;
    return nil;
}

+ (NSScreen*) screenWithMenuBar
{
    return [self screenWithPoint:NSZeroPoint];
}

+ (float) menuScreenHeight
{
    return NSMaxY([[self screenWithMenuBar] frame]);
}

+ (CGPoint) carbonPointFrom: (NSPoint) cocoaPoint
{
    return CGPointMake(cocoaPoint.x, [self menuScreenHeight] - cocoaPoint.y);
}

+ (NSPoint) cocoaPointFrom: (CGPoint) carbonPoint
{
    return NSMakePoint(carbonPoint.x, [self menuScreenHeight] - carbonPoint.y);
}

This works fine even with multiple displays.

梦亿 2024-10-08 22:44:54

来自 UIElementInspector 的 Apple 代码中发布的代码结果是有错误的,因为它没有考虑非主屏幕的起源。固定(和重构)的代码如下所示:

+ (CGPoint)carbonScreenPointFromCocoaScreenPoint:(NSPoint)cocoaPoint {
  NSScreen* foundScreen = nil;
  NSRect screenFrame;
  for (NSScreen* screen in [NSScreen screens]) {
    screenFrame = [screen frame];
    if (NSPointInRect(cocoaPoint, screenFrame)) {
      foundScreen = screen;
      break;
    }
  }
  if (! foundScreen) return CGPointMake(0.0, 0.0);
  return CGPointMake(cocoaPoint.x, 
    screenFrame.origin.y + screenFrame.size.height - cocoaPoint.y);
}

The posted code from Apple's code from UIElementInspector turn out to be buggy, in that it doesn't account for the origin of a non-main screen. The fixed (and refactored) code looks like this:

+ (CGPoint)carbonScreenPointFromCocoaScreenPoint:(NSPoint)cocoaPoint {
  NSScreen* foundScreen = nil;
  NSRect screenFrame;
  for (NSScreen* screen in [NSScreen screens]) {
    screenFrame = [screen frame];
    if (NSPointInRect(cocoaPoint, screenFrame)) {
      foundScreen = screen;
      break;
    }
  }
  if (! foundScreen) return CGPointMake(0.0, 0.0);
  return CGPointMake(cocoaPoint.x, 
    screenFrame.origin.y + screenFrame.size.height - cocoaPoint.y);
}
就像说晚安 2024-10-08 22:44:54

或者从 UIElementInspector 示例项目中获取 Apple 的代码:

+ (CGPoint)carbonScreenPointFromCocoaScreenPoint:(NSPoint)cocoaPoint
{
    NSScreen *foundScreen = nil;
    CGPoint thePoint;

    for (NSScreen *screen in [NSScreen screens])
    {
        if (NSPointInRect(cocoaPoint, [screen frame]))
        {
            foundScreen = screen;
        }
    }

    if (foundScreen)
    {
        CGFloat screenHeight = [foundScreen frame].size.height;
        thePoint = CGPointMake(cocoaPoint.x, screenHeight - cocoaPoint.y - 1);
    }
    else
    {
        thePoint = CGPointMake(0.0, 0.0);
    }

    return thePoint;
}

Or take Apple's code from UIElementInspector sample project:

+ (CGPoint)carbonScreenPointFromCocoaScreenPoint:(NSPoint)cocoaPoint
{
    NSScreen *foundScreen = nil;
    CGPoint thePoint;

    for (NSScreen *screen in [NSScreen screens])
    {
        if (NSPointInRect(cocoaPoint, [screen frame]))
        {
            foundScreen = screen;
        }
    }

    if (foundScreen)
    {
        CGFloat screenHeight = [foundScreen frame].size.height;
        thePoint = CGPointMake(cocoaPoint.x, screenHeight - cocoaPoint.y - 1);
    }
    else
    {
        thePoint = CGPointMake(0.0, 0.0);
    }

    return thePoint;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文