【问题标题】:How can my Cocoa application be notified of NSScreen resolution changes? [duplicate]如何通知我的 Cocoa 应用程序 NSScreen 分辨率更改? [复制]
【发布时间】:2017-03-15 09:42:32
【问题描述】:

我正在制作一个带有浮动窗口的 Cocoa 应用程序。浮动窗口应该位于主屏幕的中心,大小为主屏幕的 1/4。以下 Swift 是我的应用程序的精髓:

import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    var panel: NSPanel!
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        let screenRect:CGRect = NSScreen.main()!.frame
        panel = NSPanel(
            contentRect: NSRect(
                x: screenRect.width/4,
                y: screenRect.height/4,
                width:  screenRect.width/2,
                height: screenRect.height/2
            ),
            styleMask: NSWindowStyleMask.nonactivatingPanel,
            backing: NSBackingStoreType.buffered,
            defer: false
        )
        panel.alphaValue = 0.5
        panel.backgroundColor = NSColor.red
        panel.level = Int(CGWindowLevelForKey(CGWindowLevelKey.maximumWindow))
        panel.orderFront(nil)
    }
}

这会产生一个像这样的面板:

当主屏幕分辨率改变时会出现问题。显示这一点的一种方法是转到系统偏好设置>显示并将分辨率设置为“缩放”和“更多空间”。完成后,面板如下所示:

如您所见,更改分辨率后面板的位置不正确。我希望面板保持其位置:居中和屏幕大小的 1/4。为此,我要检测屏幕分辨率(即NSScreenframe 属性)何时发生变化,以便我可以更改面板的大小和位置。

NSScreenframe 属性发生变化时,是否会触发某些事件?或者有什么不同的方法来处理这个问题?

【问题讨论】:

    标签: objective-c swift cocoa screen-resolution


    【解决方案1】:

    在@Adolfo 的帮助下,这行得通:

    import Cocoa
    @NSApplicationMain
    class AppDelegate: NSObject, NSApplicationDelegate {
        var panel: NSPanel!
    
        func getPanelRect() -> NSRect {
            let screenRect:CGRect = NSScreen.main()!.frame
            return NSRect(
                x: screenRect.width/4,
                y: screenRect.height/4,
                width:  screenRect.width/2,
                height: screenRect.height/2
            )
        }
    
        func applicationDidFinishLaunching(_ aNotification: Notification) {
            panel = NSPanel(
                contentRect: self.getPanelRect(),
                styleMask: NSWindowStyleMask.nonactivatingPanel,
                backing: NSBackingStoreType.buffered,
                defer: false
            )
            panel.alphaValue = 0.5
            panel.backgroundColor = NSColor.red
            panel.level = Int(CGWindowLevelForKey(CGWindowLevelKey.maximumWindow))
            panel.orderFront(nil)
    
            NotificationCenter.default.addObserver(
                forName: NSNotification.Name.NSApplicationDidChangeScreenParameters,
                object: NSApplication.shared(),
                queue: OperationQueue.main
            ) { notification -> Void in
                print("screen parameters changed")
                self.panel.setFrame(self.getPanelRect(), display: true)
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多