0

为了使 MAAttachedWindow 可调整大小,我使用此处的代码:

- (void)mouseDown:(NSEvent *)event {
NSPoint pointInView = [event locationInWindow];

BOOL resize = YES;
if (NSPointInRect(pointInView, [self resizeRect]))
{
    resize = YES;
}

NSWindow *window = self;
NSPoint originalMouseLocation = [window convertBaseToScreen:[event locationInWindow]];
NSRect originalFrame = [window frame];

while (YES)
{
    //
    // Lock focus and take all the dragged and mouse up events until we
    // receive a mouse up.
    //
    NSEvent *newEvent = [window
                         nextEventMatchingMask:(NSLeftMouseDraggedMask | NSLeftMouseUpMask)];

    if ([newEvent type] == NSLeftMouseUp)
    {
        break;
    }

    //
    // Work out how much the mouse has moved
    //
    NSPoint newMouseLocation = [window convertBaseToScreen:[newEvent locationInWindow]];
    NSPoint delta = NSMakePoint(
                                newMouseLocation.x - originalMouseLocation.x,
                                newMouseLocation.y - originalMouseLocation.y);

    NSRect newFrame = originalFrame;

    if (!resize)
    {
        //
        // Alter the frame for a drag
        //
        newFrame.origin.x += delta.x;
        newFrame.origin.y += delta.y;
    }
    else
    {
        //
        // Alter the frame for a resize
        //
        //          newFrame.size.width += delta.x;
        newFrame.size.width += delta.x*2; // customize
        newFrame.size.height -= delta.y;
        newFrame.origin.y += delta.y;

        //
        // Constrain to the window's min and max size
        //
        NSRect newContentRect = [window contentRectForFrameRect:newFrame];
        NSSize maxSize = [window maxSize];
        NSSize minSize = [window minSize];
        if (newContentRect.size.width > maxSize.width)
        {
            newFrame.size.width -= newContentRect.size.width - maxSize.width;
        }
        else if (newContentRect.size.width < minSize.width)
        {
            newFrame.size.width += minSize.width - newContentRect.size.width;
        }
        if (newContentRect.size.height > maxSize.height)
        {
            newFrame.size.height -= newContentRect.size.height - maxSize.height;
            newFrame.origin.y += newContentRect.size.height - maxSize.height;
        }
        else if (newContentRect.size.height < minSize.height)
        {
            newFrame.size.height += minSize.height - newContentRect.size.height;
            newFrame.origin.y -= minSize.height - newContentRect.size.height;
        }
    }




    [window setFrame:newFrame display:NO animate:NO];
    }

}

当我拖动窗口时,我可以调整它的大小。但时不时会有一点反弹。您可以下载演示项目。运行项目并单击状态菜单上的图标以打开窗口。试着前后伸展,这样你就可以轻松地复制它。希望有人可以帮我解决这个问题。

4

1 回答 1

1

我检查了您的代码,似乎该错误是偶尔NSPoint newMouseLocation = [window convertBaseToScreen:[newEvent locationInWindow]];会返回原始鼠标位置而不是新位置。

我不确定为什么会失败,但无论如何这不是最佳方法。与其从原始鼠标位置计算新帧,不如不断更新该位置,并将窗口移动鼠标在当前跟踪迭代中移动的量

这是一个清理的、缩短的版本,似乎消除了这个错误。

- (void) mouseDown:(NSEvent *)event
{
    NSWindow *window = self;
    NSPoint originalMouseLocation = [window convertBaseToScreen:[event locationInWindow]];


while (YES)
{

    NSEvent *newEvent = [window
                         nextEventMatchingMask:(NSLeftMouseDraggedMask | NSLeftMouseUpMask)];

    if ([newEvent type] == NSLeftMouseUp)
    {
        break;
    }

    NSPoint newMouseLocation = [window convertBaseToScreen:[newEvent locationInWindow]];

    NSPoint delta = NSMakePoint(
                                roundf(newMouseLocation.x - originalMouseLocation.x),
                                roundf(newMouseLocation.y - originalMouseLocation.y));

    NSRect newFrame = [window frame];
    newFrame.size.width += delta.x*2; // customize
    newFrame.size.height -= delta.y;
    newFrame.origin.y += delta.y;

    [window setFrame:newFrame display:YES animate:NO];
    originalMouseLocation = newMouseLocation; // <-- added this

}

}

于 2011-05-01T06:07:27.923 回答