如何在简单的 MacOS window 上启用调整大小句柄?
How to enable resize handles on a simple MacOS window?
我有一个非常简单的 MacOS window,单个编译文件,使用 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.");
}
我在初始化 window 时在 styleMask
上设置了 NSWindowStyleMaskResizable
标志。这允许我在选择屏幕边缘时拖动 window 并调整其大小。但是,当我将光标移动到 window 的边缘时,我没有获得调整 window 大小的句柄。为了支持这个,我至少需要添加哪些代码元素?
我已经尝试查看 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 以包含一些内容?或者这是因为获得 window 运行ning 所采取的最小方法?
有两个问题(至少)。首先,为 untilDate:
参数传递 nil
意味着您的事件循环旋转,一直处理 nil
事件。你应该通过 [NSDate distantFuture]
.
其次,独立的可执行文件——即那些不在应用程序包中的文件——以 NSApplicationActivationPolicyProhibited
的激活策略开始。您需要在事件循环之前执行 [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]
。
我有一个非常简单的 MacOS window,单个编译文件,使用 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.");
}
我在初始化 window 时在 styleMask
上设置了 NSWindowStyleMaskResizable
标志。这允许我在选择屏幕边缘时拖动 window 并调整其大小。但是,当我将光标移动到 window 的边缘时,我没有获得调整 window 大小的句柄。为了支持这个,我至少需要添加哪些代码元素?
我已经尝试查看 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 以包含一些内容?或者这是因为获得 window 运行ning 所采取的最小方法?
有两个问题(至少)。首先,为 untilDate:
参数传递 nil
意味着您的事件循环旋转,一直处理 nil
事件。你应该通过 [NSDate distantFuture]
.
其次,独立的可执行文件——即那些不在应用程序包中的文件——以 NSApplicationActivationPolicyProhibited
的激活策略开始。您需要在事件循环之前执行 [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]
。