【问题标题】:Make NSWindow with transparent titlebar partly unmovable使带有透明标题栏的 NSWindow 部分不可移动
【发布时间】:2015-07-21 05:39:17
【问题描述】:

我有一个NSWindow,它的屏幕分屏,就像在提醒中一样。因此我使用此代码:

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

这非常有效。但在窗口内,我有一个 SplitView(如在提醒应用程序中)和右侧的 NSOutlineView。 OutlineView 上升到窗口角落的顶部。

现在的问题是:在 OutlineView 顶部单击并拖动会使窗口可移动。无论如何,我可以禁用此功能,但仍将移动功能保留在应用程序的左侧?

【问题讨论】:

  • 窗口拖动是窗口服务器的一个功能。祝你好运。

标签: objective-c swift cocoa


【解决方案1】:

好的,你需要做两件事:

首先,您需要将窗口设置为不可移动。为此,请将您的 Window 子类化并覆盖 isMovable 并返回 no。或者您拨打setMovable: 并将其设置为否。

之后,您必须手动重新启用拖动功能,方法是添加一个视图,该视图具有您想要拖动的区域的确切大小和位置。或者,您可以设置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;
        }
    }
}

initialLocationNSPoint 属性

注意:我查了一些东西 herehere

【讨论】:

  • 你能解释一下while循环吗?我不明白你如何让它更新实际窗口......
  • 该死,这是一个缩进问题,而不是一个没有支撑的 if 语句。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2015-02-17
  • 1970-01-01
  • 2012-10-01
  • 1970-01-01
  • 2019-09-10
相关资源
最近更新 更多