0

在我的程序中,我能够确定是否在某个 NSRect 内进行了鼠标点击。如何通过单击此 NSRect 打开一个新的 NSWindow?

4

2 回答 2

2

如果要显示现有窗口(使用 Interface Builder 创建),只需在窗口对象上调用makeKeyAndOrderFront 。
如果您想以编程方式创建一个新窗口,您可以在此处找到答案。

于 2009-05-31T17:13:51.227 回答
0

要处理事件,您需要在 NSView 或 NSViewController 子类中实现 NSResponder 的相关方法。例如,您可以实现mouseDown:-mouseUp:处理鼠标点击(以相当简单的方式),如下所示:

- (void) mouseDown: (NSEvent *) event
{
    if ( [event type] != NSLeftMouseDown )
    {
        // not the left button, let other things handle it
        [super mouseDown: event];
        return;
    }

    NSPoint location = [self convertPoint: [event locationInWindow] fromView: nil];
    if ( !NSPointInRect(location, self.theRect) )
    {
        [super mouseDown: event];
        return;
    }

    self.hasMouseDown = YES;
}

- (void) mouseUp: (NSEvent *) event
{
    if ( (!self.hasMouseDown) || ([event type] != NSLeftMouseUp) )
    {
        [super mouseUp: event];
        return;
    }

    NSPoint location = [self convertPoint: [event locationInWindow] fromView: nil];
    if ( !NSPointInRect(location, self.theRect) )
    {
        [super mouseDown: event];
        return;
    }

    self.hasMouseDown = NO;

    // mouse went down and up within the target rect, so you can do stuff now
}
于 2009-06-01T01:29:01.483 回答