我可以使用该窗口内的对象移动窗口吗?

时间:2009-12-22 12:47:01

标签: cocoa window movable

Cocoa是否可以通过拖动窗口内的对象来移动窗口? 例如:我在窗口内有一个webview,大窗口,所以setMovableByWindowBackground显然不起作用。有没有办法点击并拖动网页视图并移动整个窗口?

1 个答案:

答案 0 :(得分:5)

当然,您只需使用mouseDragged跟踪鼠标移动。类似的东西应该有效:

- (void)mouseDragged:(NSEvent *)theEvent
{
   NSPoint currentLocation;
   NSPoint newOrigin;

   NSRect  screenFrame = [[NSScreen mainScreen] frame];
   NSRect  windowFrame = [self frame];

    currentLocation = [NSEvent mouseLocation];
    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) > (screenFrame.origin.y+screenFrame.size.height) ){
        newOrigin.y=screenFrame.origin.y + (screenFrame.size.height-windowFrame.size.height);
    }

    //go ahead and move the window to the new location
    [self setFrameOrigin:newOrigin];
}

我从这里得到的是:http://www.cocoadev.com/index.pl?SetMovableByWindowBackground