你的问题有点令人困惑。
如果你的意思是你想要一个透明的窗口,上面有一个透明的 web 视图,上面有一些控件/按钮。这使得控件/按钮看起来是浮动的,并且能够在应用程序失去焦点的情况下单击看不见的窗口和 Web 视图,并且您在下面单击的任何内容都会成为活动应用程序。
如果您的意思是应用程序保持活动状态,但点击事件作用于透明窗口下方的任何内容。请参阅@WilShipley 的回答。
我刚刚测试并运行的这个例子。
1、在 AppDelegate 的 awakeFromNib 中将 webview 的 Alpha 设置为 0。
AppDelegate.m
// AppDelegate.m
// Transparent
//
// Created by Mark Hunte on 19/01/2014.
// Copyright (c) 2014 Mark Hunte. All rights reserved.
//
#import "AppDelegate.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
-(void)awakeFromNib{
[_theWebView setAlphaValue:0];
}
@end
2,子类化 NSWindow 使用:
[self setOpaque:NO];
[self setHasShadow:NO];
您可以选择 styleMask 以及您想要的任何其他选项。
这里我给了它一个标题栏。
*(
如果您不想要标题栏但想要移动它。您将不得不覆盖:
- (BOOL)canBecomeKeyWindow
并使用:
- (void)mouseDown:(NSEvent *)theEvent {
- (void)mouseDragged:(NSEvent )theEvent {
跟踪窗口的移动和位置。
)
MyWindow.h
//
// MyWindow.h
//
// Created by markhunte on 12/12/2010.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface MyWindow: NSWindow {
}
- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag;
@end
MyWindow.m
//
// MyWindow.m
//
// Created by markhunte on 12/12/2010.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "MyWindow.h"
@implementation MyWindow
- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag {
self = [super initWithContentRect:contentRect styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO];
if (self)
{
[self setOpaque:NO];
[self setHasShadow:NO];
}
return self;
}
@end
3,子类化 NSWindow 的 View 使用:
[[NSColor clearColor] set];
NSRectFill([self frame]);
在其- (void)drawRect:(NSRect)rect
MyView.h
//
// MyView.h
//
// Created by markhunte on 13/12/2010.
// Copyright 2010 markosx.com. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface MyView : NSView{
}
- (id)initWithFrame:(NSRect)frameRect;
- (void)drawRect:(NSRect)rect;
@end
MyView.m
//
// MyView.m
//
// Created by markhunte on 13/12/2010.
// Copyright 2010 markosx.com. All rights reserved.
//
#import "MyView.h"
@implementation MyView
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
return self;
}
- (void)drawRect:(NSRect)rect
{
[[NSColor clearColor] set];
NSRectFill([self frame]);
}
@end
在 IB 布局中,将 webview 连接为 IBOutlet,此处命名为 theWebView
我在布局中放置了一些标签,只是为了说明。
当运行窗口显示为:
我可以点击透明窗口的任何部分和 webview 到它后面的任何东西。
我已将源代码添加到我的博客The Cocoa Quest