微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在简单的MacOS窗口上启用调整大小手柄?

如何解决如何在简单的MacOS窗口上启用调整大小手柄?

我有一个非常简单的MacOS窗口,一个编译文件,使用clang编译器(例如clang -framework AppKit -o simple-mac-window osx_main.mm)进行编译。可以从命令行运行。

//OSX Main - Entry point for the OSX platform.
#include <stdio.h>
#include <AppKit/AppKit.h>

static float GlobalRenderWidth = 1024;
static float GlobalRenderHeight = 768;
static bool Running = true;

//Declare an interface that inherits from NSObject and implements NSWindowDelegate.
@interface SimpleMainWindowDelegate: NSObject<NSWindowDelegate>
@end
@implementation SimpleMainWindowDelegate

- (void)windowWillClose:(id)sender {
    Running = false;
}

@end

int main(int argc,const char * argv[]) {

    SimpleMainWindowDelegate *mainWindowDelegate = [[SimpleMainWindowDelegate alloc] init];

    NSRect screenRect = [[NSScreen mainScreen] frame];
    NSRect initialFrame = NSMakeRect((screenRect.size.width - GlobalRenderWidth) * 0.5,(screenRect.size.height - GlobalRenderHeight) * 0.5,GlobalRenderWidth,GlobalRenderHeight);

    NSWindow *window = [[NSWindow alloc] initWithContentRect: initialFrame
                                            styleMask: NSWindowStyleMaskTitled |
                                                        NSWindowStyleMaskClosable |
                                                        NSWindowStyleMaskMiniaturizable |
                                                        NSWindowStyleMaskResizable
                                            backing:NSbackingStoreBuffered
                                            defer:NO];
    [window setBackgroundColor: NSColor.redColor];
    [window setTitle:@"simple-mac-window"];
    [window makeKeyAndOrderFront: nil];
    [window setDelegate: mainWindowDelegate];

    while(Running) {
        NSEvent* event;
        do {
            event = [NSApp nextEventMatchingMask: NSEventMaskAny
            untilDate: nil
            inMode: NSDefaultRunLoopMode
            dequeue: YES];

            switch([event type]) {
                default:
                [NSApp sendEvent: event];
            }
        } while (event != nil);
    }

    printf("Finished running simple-mac-window.");
}

初始化窗口时,我在NSWindowStyleMaskResizable上设置了styleMask标志。这使我在选择屏幕边缘时可以拖动窗口并调整窗口大小。但是,当我将光标移到窗口边缘时,无法获得调整窗口大小的句柄。为了支持功能,我需要添加最少的代码元素?

我尝试签出NSWindow Apple documentation,但似乎只需要该标志。也许我需要在窗口对象上设置一些值或更新window delegate使其包含某些内容?还是这是因为采用最少的方法来运行窗口?

解决方法

(至少)有两个问题。首先,为nil参数传递untilDate:意味着事件循环旋转,始终处理nil事件。您应该通过[NSDate distantFuture]

第二个独立的可执行文件(即那些不在应用程序捆绑包中的可执行文件)以NSApplicationActivationPolicyProhibited的激活策略开始运行。您需要在事件循环之前执行[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。