0

Leopard 的 AppKit 发行说明说:

在许多应用程序中,向用户显示页面设置面板是不合适的,甚至在文件菜单中包含页面设置...项目是不合适的,但是没有其他简单的方法可以让用户指定要使用的页面设置参数打印时。(新的 UI 建议:如果您的应用程序没有在每个文档的基础上持久存储页面设置参数,或者有某种机制将它们与您的应用程序可能处理的任何其他类型的大型对象相关联,它可能不应该有一个页面设置面板。)

我有相当简单的打印需求,想从我的应用程序中删除“页面设置...”命令。

我之前打印文本的方法是为当前纸张大小和方向创建一个合适大小的屏幕外文本视图,然后为该视图启动打印操作。

但是,如果允许用户在打印面板中更改纸张大小和方向,我需要对该更改做出反应并重新配置我的打印文本视图。

处理这种情况的正确方法是什么?

我当前破解的示例应用程序通过子类化 NSTextView 并重新配置-[MyTextView knowsPageRange:].

4

1 回答 1

1

One approach that's slightly hacky but works really well is to run two NSPrintOperations.

We use this in our app's printing code, to detect if the user selected a regular printer or a receipt printer in the print panel. The app prints an entirely different document for each kind of printer.

So in PyObjC, untested code:

def smartlyPrintView_(self, theViewToPrint):
    # set up and run an initial NSPrintOperation to get printInfo
    fakePrintOperation = NSPrintOperation.printOperationWithView_(theViewToPrint)
    NSPrintOperation.setCurrentOperation_(fakePrintOperation)

    if NSPrintPanel.printPanel().runModal() == True:
        # get the printInfo so we can do stuff with it
        printInfo = fakePrintOperation.printInfo()
        # get rid of our fakePrintOperation
        NSPrintOperation.currentOperation().cleanUpOperation()

        # do stuff

        # run a real NSPrintOperation without a printPanel
        realPrintOperation = NSPrintOperation.printOperationWithView_printInfo_(theViewToPrint, printInfo)
        realPrintOperation.setShowsPrintPanel_(False)
        realPrintOperation.runOperation()

    else:
        NSPrintOperation.currentOperation().cleanUpOperation()

Recipe to translate this to Obj-C:

Add some variable declarations, replace the underscores with colons, and interleave the arguments with the method's clauses. Mix in half a cup of flour and a handful of semi-colons. Simmer at low heat for thirthy minutes and serve hot. Good luck :)

于 2008-11-02T22:56:36.873 回答