【问题标题】:Can I move window using an object inside that window?我可以使用该窗口内的对象移动窗口吗?
【发布时间】:2010-12-29 02:32:31
【问题描述】:

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

【问题讨论】:

  • 一切皆有可能。这并不是一个好主意。
  • 您希望整个 WebView 可拖动,还是只选择部分?

标签: cocoa window movable


【解决方案1】:

当然,您只需使用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

【讨论】:

  • 谢谢!似乎 WebView 是唯一无法响应 mouseDragged 和 mouseDown 事件的对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-22
  • 1970-01-01
  • 2017-11-15
  • 2017-10-06
  • 1970-01-01
相关资源
最近更新 更多