11

在我的应用程序中,我更改了标准窗口按钮关闭/缩小/扩展的位置,如下所示:

 //Create the buttons
    NSButton *minitButton = [NSWindow standardWindowButton:NSWindowMiniaturizeButton forStyleMask:window.styleMask];
NSButton *closeButton = [NSWindow standardWindowButton:NSWindowCloseButton forStyleMask:window.styleMask];
NSButton *fullScreenButton = [NSWindow standardWindowButton:NSWindowZoomButton forStyleMask:window.styleMask];


//set their location
[closeButton setFrame:CGRectMake(7+70, window.frame.size.height - 22 - 52, closeButton.frame.size.width, closeButton.frame.size.height)];
[fullScreenButton setFrame:CGRectMake(47+70, window.frame.size.height - 22 -52, fullScreenButton.frame.size.width, fullScreenButton.frame.size.height)];
[minitButton setFrame:CGRectMake(27+70, window.frame.size.height - 22 - 52, minitButton.frame.size.width, minitButton.frame.size.height)];

//add them to the window
[window.contentView addSubview:closeButton];
[window.contentView addSubview:fullScreenButton];
[window.contentView addSubview:minitButton];

现在,当带有按钮的窗口出现时,有两个问题: 1. 它们是灰色的,颜色不正确 2. 当鼠标悬停在它们上面时,它们不显示 + - 或 x 符号

谁能告诉我我做错了什么。谢谢。

4

4 回答 4

11

这是悬停魔法的机制:在绘制自身之前,标准带圆圈的按钮(例如NSWindowMiniaturizeButton)调用其superview未记录的方法_mouseInGroup:。如果此方法返回YES带圆圈的按钮,则会在内部绘制带有图标的自身。就这样。

如果您将这些按钮放在您自己的视图中,您可以简单地实现此方法并根据需要控制此鼠标悬停外观。如果您只是移动或重新布局这些按钮并且它们仍然是subviews of NSThemeFrame(或类似的东西),则您必须_mouseInGroup:为此类调整方法,并且可能不值得这样做,因为我们有非常简单的先前方法。

在我的情况下,我有一个自定义NSView,其中包含我的标准按钮作为subviews 并且此代码使上述所有内容变得神奇:

- (void)updateTrackingAreas
{
    NSTrackingArea *const trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options:(NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect) owner:self userInfo:nil];
    [self addTrackingArea:trackingArea];
}

- (void)mouseEntered:(NSEvent *)event
{
    [super mouseEntered:event];
    self.mouseInside = YES;
    [self setNeedsDisplayForStandardWindowButtons];
}

- (void)mouseExited:(NSEvent *)event
{
    [super mouseExited:event];
    self.mouseInside = NO;
    [self setNeedsDisplayForStandardWindowButtons];
}

- (BOOL)_mouseInGroup:(NSButton *)button
{
    return self.mouseInside;
}

- (void)setNeedsDisplayForStandardWindowButtons
{
    [self.closeButtonView setNeedsDisplay];
    [self.miniaturizeButtonView setNeedsDisplay];
    [self.zoomButtonView setNeedsDisplay];
}
于 2015-05-23T20:40:52.053 回答
2

我完全知道这个问题很Valentin Shergin的回答正确的。与谷歌在 Chrome 中所做的不同,它阻止了任何私有 API的使用。只是想为那些不喜欢子类的人分享一种方法,只是将这些按钮放在现有视图中(例如)。NSViewself.window.contentView

由于我只是想通过重新定位NSWindowButtons setFrame:,我发现一旦调整窗口大小,跟踪区域似乎会自动“修复”自身,而无需使用任何私有 API(至少在 10.11 中)。

因此,您可以执行以下操作以将“假调整大小”应用于您重新定位按钮的窗口:

NSRect frame = [self.window frame];
frame.size = NSMakeSize(frame.size.width, frame.size.height+1.f);
[self.window setFrame:frame display:NO animate:NO];
frame.size = NSMakeSize(frame.size.width, frame.size.height-1.f);
[self.window setFrame:frame display:NO animate:YES];

(我是在我的主窗口中完成的NSWindowDelegate windowDidBecomeMain:。只要窗口已加载且可见,它就应该可以工作。)

于 2016-03-29T18:49:24.973 回答
0

您不会再次添加它们。您正在将它们移动到 contentView。这些按钮最初位于 window.contentView.superview 中。

[window.contentView.superview addSubview:closeButton];
[window.contentView.superview addSubview:fullScreenButton];
[window.contentView.superview addSubview:minitButton];

无需 trackingArea 即可让您获得正确的行为。

于 2013-07-02T18:20:30.557 回答
-1

呼叫[button highlight:yes]每个按钮。

于 2011-12-24T10:21:01.143 回答