2009-12-22 66 views
3

Cocoa可以通过拖动窗口内的对象来移动窗口吗? 例如:我有一个窗口内的webview,窗口大,所以setMovableByWindowBackground显然不工作。有没有办法点击并拖动Web视图并移动整个窗口?我可以使用该窗口内的对象移动窗口吗?

+1

一切皆有可能。这并不是一个好主意。 – 2009-12-22 13:07:03

+0

您是否希望整个WebView可拖动或仅选择部分? – 2009-12-22 17:47:34

回答

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

+0

谢谢!看来WebView是唯一无法响应mouseDragged和mouseDown事件的对象。 – Donovan 2009-12-22 13:52:21