Is it possible in Cocoa to move a window by dragging an object which is inside that window? For example: I have a webview inside a window, big as the window, so setMovableByWindowBackground will obviously not work. Is there a way to click and drag the web view and move the whole window?
Asked
Active
Viewed 1,780 times
1 Answers
5
Sure, you've just got to track the mouse movements using mouseDragged
. Something similar to this should work:
- (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];
}
Which I got from here: http://www.cocoadev.com/index.pl?SetMovableByWindowBackground

kubi
- 48,104
- 19
- 94
- 118
-
Thank you! It seems that WebView is the only object which cannot respond to mouseDragged and mouseDown events. – Donovan Dec 22 '09 at 13:52