www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/mbcprint.m

    function mbcprint(comp, sz, dest)
%MBCPRINT Prints a component 
%
%  MBCPRINT(COMP, SZ, DEST) prints the specified MBC component, COMP.  SZ
%  specifies a [Width, Height] vector and DEST is a string controlling
%  where the output will be sent.  Valid settings for DEST are in the table
%  below.
%
%  MBCPRINT assumes that COMP is the only visible item in its parent
%  figure.  If other items are present they may well appear in the printed
%  copy as well.  
%
%  DEST          | Meaning
%  ----------------------------------------------------------
%  clipboard     | Copies the component to the clipboard using the -dmeta
%                | flag of the print command.  The output will be the size
%                | specified by SZ.
%                |
%  printer       | Copies the component directly to the printer.  The
%                | output will have the same aspect ratio as defined by the
%                | size SZ, but will be scaled to fir the paper size.
%                |
%  printdialog   | Opens the print dialog before sending to the printer.
%                |
%  printpreview  | Opens the print preview dialog before sending to the
%                | printer.
%
%  This function is a low-level printing implementation that can be used
%  for printing older MBC components.  If you want to print a modern
%  component, you should subclass from mbcwidgets.abstractcomponent and use
%  its print interface (uses mbcprint). 

%  Copyright 2000-2015 The MathWorks, Inc. and Ford Global Technologies, Inc.


% Page borders (N, E, S, W)
Border = [2 1.5 2 1.5];

if nargin<3
    dest = 'printer';
end
if nargin<2
    sz = get(comp, 'Position');
    sz = sz(3:4);
end

% comp could be an MBC component but parent will always be a graphics
% object
parent = get(comp, 'Parent');
hFig = ancestor(parent, 'figure');
CompIsFig = false;
if isempty(hFig)
    % The root would not have a figure ancestor.  In this case comp must
    % itself be the figure.
    if ~isgraphics(comp, 'figure')
        error(message('mbc:mbcprint:InvalidArgument'));
    end
    CompIsFig = true;
    hFig = comp;
end

% Save some figure property settings
backup_props = {'Color', 'Units', 'Position', ...
    'PaperPosition', 'PaperPositionMode', 'PaperUnits', 'WindowStyle'};
backup_vals = get(hFig, backup_props);

% Set some required figure properties
set(hFig, 'WindowStyle', 'normal', ...     % Need to force figures not to be docked
    'Units', 'pixels', ...
    'Color', 'w', ...
    'PaperPositionMode', 'auto');
% don't need to resize figures or graphical components that don't use pixel units 
GraphicsNonPixels = isgraphics(comp) && ~strcmp(comp.Units,'pixels');
ResizeComp = ~(CompIsFig || GraphicsNonPixels);
if strcmp(dest, 'clipboard')
    xregcenterfigure(hFig, sz);
    if ResizeComp
        set(comp, 'Position', [1 1 sz]);
    end
    print(hFig, '-dmeta');
else
    set(hFig,'PaperUnits', 'centimeters', ...
        'Position', [1 1 100 100]);
    
    % Get the current paper size and use it to work out dots/cm
    Ppos = get(hFig, 'PaperPosition');
    pixelsPerCm = 100./Ppos(3:4);
    
    % Work out size on paper for given aspect ratio
    Psz = get(hFig,'PaperSize');
    Ppossz = min((Psz - [Border(2)+Border(4) Border(1)+Border(3)])./sz).*sz;

    % Put output centered horizontally and at the top of the page.
    set(hFig,'PaperPosition', ...
        [Border(4) + (Psz(1)-Border(4)-Border(2))/2 - Ppossz(1)/2, ...
        Psz(2) - Border(1) - Ppossz(2), ...
        Ppossz]);
    xregcenterfigure(hFig, Ppossz.*pixelsPerCm);

    if ResizeComp
        set(comp, 'Position', [1 1 Ppossz.*pixelsPerCm]);
    end
    set(comp,'Visible','on');
    
    % Print the figure
    hFig = mbcgui.hgclassesutil.toNative(hFig);
    switch dest
        case 'printer'
            print(hFig);
        case 'printdialog'
            printdlg(hFig);
        case 'printpreview'
            printpreview(hFig);
        otherwise
            warning(message('mbc:mbcprint:InvalidArgument1', dest));
    end
end

% Restore figure properties
set(hFig, backup_props, backup_vals);