我可以在打印时显示 window 吗?
Can I show a window while printing?
我有一个 NSView
的子类只用于打印,它在打印时会做一些相当复杂的事情,这可能会导致出现一个进度对话框。问题是进度对话框绘制到打印输出而不是屏幕上。
我尝试用 [NSGraphicsContext saveGraphicsState]
和 [NSGraphicsContext restoreGraphicsState];
将可能绘制到 window 的地方括起来,但没有用。
我也试过了
[self unlockFocus];
[self stuffThatMightDrawAWindow];
[self lockFocus];
但这没有用。事实上我发现在 unlockFocus
之后,[NSView focusView]
仍然指向我的打印视图,好像 unlockFocus
实际上没有做任何事情。
我想我应该尝试重新组织我的代码,以便在我的打印视图的 drawRect:
方法中只绘制打印输出,但我很好奇这是否是唯一的解决方案。
我尝试重构以在 rectForPage:
方法中进行所有内容准备,第一个我知道页码的地方。现在打印输出看起来不错,但是如果我在 rectForPage:
期间 运行 一个简单的模式 NSAlert
,除了按钮之外,警报是空白的:
我发现了一个 old (2001) post by Erik M. Buck 说法:
While an NSView is printing, its connection to the Window Server is replaced
by a connection to the print job output. Sometimes the NSView needs to
communicate briefly with the Window Server while printing; for example, it
may need to read some data stored only on the Window Server, or open an
attention panel to alert the user of a problem. In these cases, it can
temporarily swap in the NSApplication object's display context to restore
access to the application's Window Server state and to the screen. When
finished, the view object restores the print operation's context to continue
generating its image.
所以,解决方案是
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext: [NSApp context]];
[self stuffThatShowsDialogsInsteadOfPrinting];
[NSGraphicsContext restoreGraphicsState];
这是第二行,[NSApp context]
,如果没有提示我永远不会想到。
我有一个 NSView
的子类只用于打印,它在打印时会做一些相当复杂的事情,这可能会导致出现一个进度对话框。问题是进度对话框绘制到打印输出而不是屏幕上。
我尝试用 [NSGraphicsContext saveGraphicsState]
和 [NSGraphicsContext restoreGraphicsState];
将可能绘制到 window 的地方括起来,但没有用。
我也试过了
[self unlockFocus];
[self stuffThatMightDrawAWindow];
[self lockFocus];
但这没有用。事实上我发现在 unlockFocus
之后,[NSView focusView]
仍然指向我的打印视图,好像 unlockFocus
实际上没有做任何事情。
我想我应该尝试重新组织我的代码,以便在我的打印视图的 drawRect:
方法中只绘制打印输出,但我很好奇这是否是唯一的解决方案。
我尝试重构以在 rectForPage:
方法中进行所有内容准备,第一个我知道页码的地方。现在打印输出看起来不错,但是如果我在 rectForPage:
期间 运行 一个简单的模式 NSAlert
,除了按钮之外,警报是空白的:
我发现了一个 old (2001) post by Erik M. Buck 说法:
While an NSView is printing, its connection to the Window Server is replaced by a connection to the print job output. Sometimes the NSView needs to communicate briefly with the Window Server while printing; for example, it may need to read some data stored only on the Window Server, or open an attention panel to alert the user of a problem. In these cases, it can temporarily swap in the NSApplication object's display context to restore access to the application's Window Server state and to the screen. When finished, the view object restores the print operation's context to continue generating its image.
所以,解决方案是
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext: [NSApp context]];
[self stuffThatShowsDialogsInsteadOfPrinting];
[NSGraphicsContext restoreGraphicsState];
这是第二行,[NSApp context]
,如果没有提示我永远不会想到。