在 Visual FoxPro 9.0 中防止从报表预览进行打印

本文介绍了如何在 Visual FoxPro 9.0 中使用对象辅助报表功能,禁用报告在预览时打印的选项。

原始产品版本: Visual FoxPro
原始 KB 数: 895279

概要

Microsoft Visual FoxPro 9.0 引入了 Object-Assisted 报告体系结构。 通过这种新设计,可以使用 Visual FoxPro 9.0 报表工具(如报表设计器和报表预览)直接通过代码进行接口。 因此,在 Visual FoxPro 9.0 中禁用从报表预览打印的功能比在早期版本的 Visual FoxPro 中更容易。 本文介绍了如何编写代码以直接与对象辅助报告交互并配置报告预览。

更多信息

默认情况下,在 Visual FoxPro 9.0 中预览 Object-Assisted 报表时,用户可以使用以下任一方法从报表预览窗口中打印报表:

  • 在“报表预览”工具栏上,单击“ 打印 ”按钮。
  • 在“报表预览”窗口中右键单击,然后单击“ 打印”。

您可能需要禁用此选项。 在 Visual FoxPro 的早期版本中,必须更改 Visual FoxPro 资源文件(FoxUser.dbf)。

在 Visual FoxPro 9.0 中,你仍然可以更改 FoxUser.dbf 文件以禁用从报表预览窗口中打印。 但是,随着 Object-Assisted 报告的引入,现在提供了一种替代方法。 默认情况下,Visual FoxPro 9.0 中的对象辅助报告功能未启用。 要启用它,您必须更改REPORTBEHAVIOR设置。 要做到这一点,请使用以下方法之一:

  • 方法 1:通过 Visual FoxPro IDE 更改报表引擎行为设置:

    1. “工具” 菜单上,单击 “选项”
    2. 选项对话框中,点击报告选项卡,然后在报告引擎行为列表中选择90(对象辅助)
    3. (可选)如果希望此设置在 Visual FoxPro 9.0 会话之间保留,请单击“选项”对话框中的“设为默认值”。
  • 方法二:在代码中运行以下命令以启用对象辅助报告:

    SET REPORTBEHAVIOR 90

代码示例

当您运行以下代码示例时,将预览名为 Colors.frx 的示例报告文件。 代码示例浏览了报告,并通过在预览容器中设置属性和使用预览扩展处理程序的组合,彻底禁用报告预览窗口中的打印功能。

此代码示例还解决了Visual FoxPro 9.0对象辅助报表预览中的一个问题,即报表预览工具栏在同一报表预览会话中的多次出现之间未能正确保留显示设置。

此代码示例适用于Visual FoxPro 9.0开发环境和使用Visual FoxPro 9.0创建的可执行文件,只要您使用面向对象的报表设计功能即可。

要使用本代码示例,请按照以下步骤操作:

  1. 将以下代码保存到 Visual FoxPro 9.0 中的新程序文件,然后运行代码。

    *----------- START CODE
    *
    *-----------------------------------
    * AUTHOR: Trevor Hancock
    * CREATED: 02/24/05 02:47:08 PM
    * ABSTRACT:
    * Shows how to disable printing from
    * PREVIEW when you use object-assisted
    * reporting in VFP9.
    *
    * Also includes a workaround for problem
    * where the PREVIEW toolbar does not
    * recognize the TextOnToolbar or
    * PrintFromPreview settings between
    * toolbar displays during same preview.
    *-----------------------------------
    
    *-- Defines whether to alllow for
    *-- printing during preview. Used to set
    *-- the "AllowPrintfromPreview" property
    *-- of the object-assisted preview container
    *-- later in this code.
    #DEFINE PrintFromPreview .T.
    
    LOCAL loPreviewContainer AS FORM, ;
    loReportListener AS REPORTLISTENER, ;
    loExHandler AS ExtensionHandler OF SYS(16)
    
    *-- Get a preview container from the
    *-- .APP registered to handle object-assisted
    *-- report previewing.
    loPreviewContainer = NULL
    DO (_REPORTPREVIEW) WITH loPreviewContainer
    *-- Create a PREVIEW listener
    loReportListener = NEWOBJECT('ReportListener')
    loReportListener.LISTENERTYPE = 1 &&Preview
    
    *-- Link the Listener and preview container
    loReportListener.PREVIEWCONTAINER = loPreviewContainer
    
    *-- Changing this property prevents printing from the Preview toolbar
    *-- and from right-clicking on the preview container, and then clicking "print".
    *-- This property is not in the VFP9 documentation at this point.
    loPreviewContainer.AllowPrintfromPreview = PrintFromPreview
    *-- Controls appearance of text on some of the
    *-- Preview toolbar buttons. .F. is the default.
    *-- Included here just to show that it is an available option.
    loPreviewContainer.TextOnToolbar = .F.
    
    *-- Create an extension handler and hook it to the
    *-- preview container. This will let you manipulate
    *-- properties of the container and its Preview toolbar
    loExHandler = NEWOBJECT('ExtensionHandler')
    loPreviewContainer.SetExtensionHandler( loExHandler )
    
    REPORT FORM ;
    HOME() + 'Samples\Solution\Reports\colors.frx' ;
    OBJECT loReportListener
    
    RELEASE loPreviewContainer, loReportListener, loExHandler
    
    *-------------------------
    *-------------------------
    DEFINE CLASS ExtensionHandler AS CUSTOM
    *-- Ref to the Preview Container's Preview Form
    PreviewForm = NULL
    
    *-- Here you implement (hook into) the PreviewForm_Assign
    *-- event of the preview container's parent proxy
    PROCEDURE PreviewForm_Assign( loRef )
    *-- Perform default behavior: assign obj ref.
    THIS.PreviewForm = loRef
    
    *-- Grab the obj ref to the preview form and bind to its
    *-- ShowToolbar() method. This lets the
    *-- STB_Handler() method of this extension handler
    *-- to run code whenever the Preview toolbar is shown
    *-- by the PreviewForm.ShowToolbar() method.
    IF !ISNULL( loRef )
    BINDEVENT(THIS.PreviewForm, ;
    'ShowToolbar', THIS, 'STB_Handler')
    ENDIF
    ENDPROC
    
    PROCEDURE STB_Handler(lEnabled)
    *-- Here you work around the setting
    *-- persistence problem in the Preview toolbar. 
    *-- The Preview toolbar class (frxpreviewtoolbar)
    *-- already has code that you can use to enforce
    *-- setting's persistence; it is just not called. Here,
    *-- you call it.
    WITH THIS.PreviewForm.TOOLBAR
    .REFRESH()
    *-- When you call frxpreviewtoolbar::REFRESH(), the
    *-- toolbar caption is set to its Preview form,
    *-- which differs from typical behavior. You must revert that
    *-- to be consistent. If you did not do this,
    *-- you would see " - Page 2" appended to the toolbar
    *-- caption if you skipped pages.
    .CAPTION = THIS.PreviewForm.formCaption
    ENDWITH
    ENDPROC
    
    *-- A preview container requires these methods
    *-- to be implemented in an associated preview extension handler.
    *-- They are not used in this example, but still must be here.
    PROCEDURE AddBarsToMenu( cPopup, iNextBar )
    PROCEDURE SHOW( iStyle )
    ENDPROC
    PROCEDURE HandledKeyPress( nKeyCode, nShiftAltCtrl )
    RETURN .F.
    ENDPROC
    PROCEDURE PAINT
    ENDPROC
    PROCEDURE RELEASE
    RETURN .T.
    ENDPROC ENDDEFINE
    *
    *
    *----------- END CODE
    

    Colors.frx 报表的预览会被打开。

    注意

    可以通过右键单击预览或单击“报表预览”工具栏上的“ 打印 ”,从“报表预览”窗口打印。

  2. 关闭预览, 然后在程序编辑器中修改代码。

  3. 将代码顶部的 #DEFINE 语句修改为 .F. 值。 要执行此操作,请找到以下代码。

    #DEFINE PrintFromPreview .T.
    

    将其替换为以下代码:

    #DEFINE PrintFromPreview .F.
    
  4. 保存代码,然后再次运行。

注意

现在,您无法通过在“报表预览”窗口中右键单击预览或单击报表预览工具栏上的打印按钮来进行打印。