在 Cocoa 中是否可以通过拖动窗口内的对象来移动窗口? 例如:我在窗口内有一个 webview,与窗口一样大,所以 setMovableByWindowBackground 显然不起作用。有没有办法单击并拖动 WebView 并移动整个窗口?
最佳答案
当然,您只需使用 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
关于cocoa - 我可以使用窗口内的对象来移动窗口吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1946185/