3
在我的COCOA應用程序中,我實現了一個自定義無邊框窗口。該窗口的內容區域完全由WebView覆蓋。當用戶在內容區域的任何位置點擊並拖動鼠標時,我希望無邊框窗口移動。我試圖覆蓋isMovableByWindowBackground但沒用。我該如何解決這個問題?移動無邊界NSWindow完全覆蓋Web視圖
在我的COCOA應用程序中,我實現了一個自定義無邊框窗口。該窗口的內容區域完全由WebView覆蓋。當用戶在內容區域的任何位置點擊並拖動鼠標時,我希望無邊框窗口移動。我試圖覆蓋isMovableByWindowBackground但沒用。我該如何解決這個問題?移動無邊界NSWindow完全覆蓋Web視圖
在WebView上調用-setMovableByWindowBackround:YES並使窗口變爲可能。
這就是我做到的。
#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
感謝您的幫助。我可以通過覆蓋NSWindow的sendEvent方法使窗口移動。 – fz300
@ fz300請問您可以發佈您的解決方案嗎? – yuf