1

我遵循了夜间 Webkit 构建中的 NetscapeCocoaPlugin 示例,并且能够构建使用 Cocoa 事件模型的 NPAPI 样式插件。

我现在的问题是如何在 NPP_SetWindow 中获取 NSView。

线程中的一张海报说,可以使用 [NSView focusView],但我无法让它工作

我当前的功能如下所示:

NPError NPP_SetWindow(NPP instance, NPWindow* window)
{
PluginObject *obj = instance->pdata;
obj->window = *window;

NSLog(@"Set Window called");

NSView* currentView = [NSView focusView];

[[NSColor redColor] set]; // Sets current drawing color.
NSRectFill(NSMakeRect(10, 10, 2, 20)); // Defines a rectangle and then fills it with the current drawing color.
[[NSColor colorWithCalibratedRed:0.7 green:0.9 blue:0.3 alpha:1.0] set]; // Sets a new color.
[[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(5, 0, 10, 10)] fill]; // Draws a circle in the new color.

[currentView setNeedsDisplay:YES];

return NPERR_NO_ERROR;
}
4

1 回答 1

1

你不能。曾经有一段时间你可以通过 hack 获得 NSView,但它从未得到支持,从来都不是一个好主意,并且不再可能,因为所有三个浏览器都切换到使用进程外插件,这意味着你无法访问到 NSView。

您可以获取 CGContextRef,然后创建自己的屏幕外 NSWindow 和 NSView 并将它们渲染到 CGContextRef 中,但是您还必须代理所有事件。FireBreath中有一个WebView 包装器,它仍然是实验性的,但它是相当痛苦的。最终我打算把它变成更通用的东西,这样 NSView 就可以(有点)在插件中使用,但是没有本地方法可以这样做。

这里有一篇关于mac绘图模型的优秀博客文章:http: //www.escapedthoughts.com/weblog/geek/P110308-mac-npapi-plugins.writeback

于 2011-08-11T15:53:35.750 回答