【问题标题】:How do I set the position and size of an NSOpenPanel dialog?如何设置 NSOpenPanel 对话框的位置和大小?
【发布时间】:2011-12-20 15:29:12
【问题描述】:

我没有太多的 Mac 编程经验,我来自 Windows 背景。

所以,我使用这段代码来显示一个 NSOpenPanel,但我还想指定对话框出现时在屏幕上的位置和大小。我该怎么做?

NSOpenPanel *openPanel  = [NSOpenPanel openPanel];
[openPanel setAllowsMultipleSelection:NO];
[openPanel setCanChooseDirectories:YES];
[openPanel setCanChooseFiles:NO];
NSInteger result    = [openPanel runModalForTypes:nil];

编辑


看来是通过调用

[openPanel setFrame:NSMakeRect(0, 0, 500, 500) display:YES];

我可以设置 NSOpenPanel 的大小(变成 500x500 像素),但不能设置它的左上角,它以屏幕为中心,而不是出现在屏幕的左上角。

【问题讨论】:

    标签: objective-c cocoa nsopenpanel


    【解决方案1】:

    您必须继承 NSOpenPanel 并覆盖 center 方法,该方法在面板被放置在屏幕上之前被调用。将它放在右上角的方法如下:

    - (void)center {
    
        NSRect myFrame = [self frame];
        NSRect screenFrame = [[self screen] visibleFrame];
    
        myFrame.size.height = round(screenFrame.size.height / 2);
        myFrame.origin.x = screenFrame.origin.x + screenFrame.size.width - myFrame.size.width;
        myFrame.origin.y = screenFrame.origin.y + screenFrame.size.height - myFrame.size.height;
    
        [self setFrame:myFrame display:YES];
    }
    

    【讨论】:

    • 谢谢,不幸的是,我需要一些时间来阅读 Obejctive-C 中的继承,然后才能听从您的建议,语法似乎有点棘手,它与 C++ 有很大不同。
    • 不幸的是,子类不能很好地与沙盒应用程序配合使用。 “.... 不是沙盒支持的子类”
    【解决方案2】:

    NSOpenPanel 继承自 NSWindow。我从未尝试过,但您似乎应该能够使用普通的 NSWindow 方法来设置它的大小和位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-22
      • 1970-01-01
      • 2011-08-25
      • 2020-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      相关资源
      最近更新 更多