使具有透明标题栏的NSWindow部分不可移动

4

我有一个类似于提醒事项中的分屏的 NSWindow。因此,我使用以下代码:

self.window.titlebarAppearsTransparent = true
self.window.styleMask |= NSFullSizeContentViewWindowMask

这个很好用。但是窗口里面有一个SplitView(就像提醒应用程序中的),右边有一个NSOutlineView,这个OutlineView延伸到窗口角落的上方。

现在的问题是: 在OutlineView的顶部单击和拖动会使窗口移动。有没有什么方法可以禁用它,但又保留应用程序左侧的移动功能?


窗口拖动是窗口服务器的一个功能。祝你好运。 - CodaFi
1个回答

4

好的,你需要做两件事:

首先你需要将窗口设置为不可移动。为此,可以子类化窗口并覆盖isMovable方法并返回no。或者调用setMovable:方法并将其设置为no。

之后,您需要通过添加一个大小和位置完全相同的视图来手动重新启用拖动。或者您可以设置一个NSTrackingArea。 无论哪种方式,您都需要覆盖mouseDown:方法并插入一些代码以移动窗口。

我的代码如下:

Objective-C

[self.window setMovable:false];

// OR (in NSWindow subclass)

- (BOOL)isMovable {
    return false;
}

//Mouse Down
- (void)mouseDown:(NSEvent *)theEvent {
    _initialLocation = [theEvent locationInWindow];

    NSPoint point;
    while (1) {
        theEvent = [[self window] nextEventMatchingMask: (NSLeftMouseDraggedMask | NSLeftMouseUpMask)];
        point =[theEvent locationInWindow];

        NSRect screenVisibleFrame = [[NSScreen mainScreen] visibleFrame];
        NSRect windowFrame = [self.window frame];
        NSPoint newOrigin = windowFrame.origin;

        // Get the mouse location in window coordinates.
        NSPoint currentLocation = point;
        // Update the origin with the difference between the new mouse location and the old mouse location.
        newOrigin.x += (currentLocation.x - _initialLocation.x);
        newOrigin.y += (currentLocation.y - _initialLocation.y);

        // Don't let window get dragged up under the menu bar
        if ((newOrigin.y + windowFrame.size.height) > (screenVisibleFrame.origin.y + screenVisibleFrame.size.height)) {
            newOrigin.y = screenVisibleFrame.origin.y + (screenVisibleFrame.size.height - windowFrame.size.height);
        }

        // Move the window to the new location
        [self.window setFrameOrigin:newOrigin];
        if ([theEvent type] == NSLeftMouseUp) {
            break;
        }
    }
}

initialLocation 是一个 NSPoint 属性

注意:我在这里查找了一些信息这里


你能解释一下 while 循环吗?我不明白如何让它更新实际窗口... - Julian F. Weinert
该死,这是一个缩进问题,而不是未加大括号的if语句。 - Julian F. Weinert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接