2011-09-02 321 views
3

在我的COCOA应用程序中,我实现了一个自定义无边框窗口。该窗口的内容区域完全由WebView覆盖。当用户在内容区域的任何位置点击并拖动鼠标时,我希望无边框窗口移动。我试图覆盖isMovableByWindowBackground但没用。我该如何解决这个问题?移动无边界NSWindow完全覆盖Web视图

回答

2

在WebView上调用-setMovableByWindowBackround:YES并使窗口变为可能。

+0

感谢您的帮助。我可以通过覆盖NSWindow的sendEvent方法使窗口移动。 – fz300

+0

@ fz300请问您可以发布您的解决方案吗? – yuf

2

这就是我做到的。

#import "BorderlessWindow.h" 


@implementation BorderlessWindow 

@synthesize initialLocation; 

- (id)initWithContentRect:(NSRect)contentRect 
      styleMask:(NSUInteger)windowStyle 
       backing:(NSBackingStoreType)bufferingType 
       defer:(BOOL)deferCreation 
{ 
if((self = [super initWithContentRect:contentRect 
            styleMask:NSBorderlessWindowMask 
           backing:NSBackingStoreBuffered 
           defer:NO])) 
{ 
    return self; 
} 

return nil; 
} 

- (BOOL) canBecomeKeyWindow 
{ 
return YES; 
} 

- (BOOL) acceptsFirstResponder 
{ 
return YES; 
} 

- (NSTimeInterval)animationResizeTime:(NSRect)newWindowFrame 
{ 
return 0.1; 
} 

- (void)sendEvent:(NSEvent *)theEvent 
{ 
if([theEvent type] == NSKeyDown) 
{ 
    if([theEvent keyCode] == 36) 
     return; 
} 

if([theEvent type] == NSLeftMouseDown) 
    [self mouseDown:theEvent]; 
else if([theEvent type] == NSLeftMouseDragged) 
    [self mouseDragged:theEvent]; 

[super sendEvent:theEvent]; 
} 


- (void)mouseDown:(NSEvent *)theEvent 
{  
self.initialLocation = [theEvent locationInWindow]; 
} 

- (void)mouseDragged:(NSEvent *)theEvent 
{ 
NSRect screenVisibleFrame = [[NSScreen mainScreen] visibleFrame]; 
NSRect windowFrame = [self frame]; 
NSPoint newOrigin = windowFrame.origin; 

NSPoint currentLocation = [theEvent locationInWindow]; 
if(currentLocation.y > windowFrame.size.height - 40) 
{ 
    newOrigin.x += (currentLocation.x - initialLocation.x); 
    newOrigin.y += (currentLocation.y - initialLocation.y); 

    if ((newOrigin.y + windowFrame.size.height) > (screenVisibleFrame.origin.y + screenVisibleFrame.size.height)) 
    { 
     newOrigin.y = screenVisibleFrame.origin.y + (screenVisibleFrame.size.height - windowFrame.size.height); 
    } 

    [self setFrameOrigin:newOrigin]; 
} 
} 


@end 

和.h文件:

@interface BorderlessWindow : NSWindow { 
NSPoint initialLocation; 
} 

- (id)initWithContentRect:(NSRect)contentRect 
      styleMask:(NSUInteger)windowStyle 
       backing:(NSBackingStoreType)bufferingType 
       defer:(BOOL)deferCreation; 

@property (assign) NSPoint initialLocation; 

@end