Can I move a window using an object inside this window?

Can Cocoa move a window by dragging an object that is inside that window? For example: I have a webview inside a window, big as a window, so setMovableByWindowBackground obviously won't work. Is there a way to click and drag a web view and move the whole window?

+3
source share
1 answer

Of course, you only need to track 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];
}

What I got from here: http://www.cocoadev.com/index.pl?SetMovableByWindowBackground

+5
source

Source: https://habr.com/ru/post/1726340/


All Articles