简体   繁体   中英

How to enable resize handles on a simple MacOS window?

I have a very simple MacOS window, single compilation file, compiled with the clang compiler (eg clang -framework AppKit -o simple-mac-window osx_main.mm ). It can be run from the command line.

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

I have set the NSWindowStyleMaskResizable flag on the styleMask when initialising the window. This allows me to drag and resize the window when selecting the edges of the screen. However I do not get the handles to resize the window when I move my cursor to edge of the window. What are the minimum code elements I need to add in order to support this?

I have tried checking out the NSWindow Apple documentation but it seems that all is required is the flag. Perhaps I need to set some values on the window object or update the window delegate to include something? Or is this because of the minimal approach taken to get a window running?

There are two issues (at least). First, passing nil for the untilDate: parameter means your event loop spins, handling a nil event all the time. You should pass [NSDate distantFuture] .

Second, standalone executables — that is, those that are not in an app bundle — start life with an activation policy of NSApplicationActivationPolicyProhibited . You need to do [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular] before your event loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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