<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    kooyee ‘s blog

    開源軟件, 眾人努力的結晶, 全人類的共同財富
    posts - 103, comments - 55, trackbacks - 0, articles - 66
       :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    [SWT] Print & Print Preview

    Posted on 2007-07-24 23:43 kooyee 閱讀(1053) 評論(0)  編輯  收藏 所屬分類: GUI骨衣

    從SWT中建立"print dialog", 當call print()時彈出print dialog
    public void print(){
            PrintDialog dialog 
    = new PrintDialog(da.sShell, SWT.NONE);
            PrinterData data 
    = dialog.open();
            
    if (data == nullreturn;
            
    if (data.printToFile) {
                data.fileName 
    = "print.out"// you probably want to ask the user for a filename
            }

            printer 
    = new Printer(data);
            print(printer);
            printer.dispose();
        }

    緊接著建立print(Printer printer)
    private void print(Printer printer) {
            
    if (printer.startJob("Text")) {   // the string is the job name - shows up in the printer's job list
                Rectangle trim = printer.computeTrim(0000);
                
    //Calculate the scale factor between the screen resolution and printer
                
    // resolution in order to correctly size the image for the printer
                Point screenDPI = da.sShell.getDisplay().getDPI();
                Point printerDPI 
    = printer.getDPI();
                
    int scaleFactor = printerDPI.x / screenDPI.x;
                
    //int width = image.getImageData().width;
                
    //int height = image.getImageData().height;
                /* Create printer GC, and create and set the printer font */
                gc 
    = new GC(printer); 
                gc.drawText("some text here", scaleFactor 
    * 20, scaleFactor * 20);
                
                printer.endJob();
                gc.dispose();
            }

        }



    完成"print" 之后添加"print preview"。只需要讓"print" method 帶入 2 個arguments:Device 和 GC
    當打印時帶入Printer device 和 包含打印信息的GC。當要預覽時帶入 Display device 和GC(
    drawing on canvas),就是相當于把要打印的東西顯示在canvans里。

    create all colors, fonts, and images using the correct device,之后運行gc.dispose()

    對于打印的字體的設置
    gc.setFont(new Font())


    對于打印的字體的設置可以參考:
    org.eclipse.swt.custom.StyledText
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.custom.StyleRange;
    import org.eclipse.swt.custom.StyledText;
    import org.eclipse.swt.graphics.Font;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;

    public class SampleStyledText {
      Display display 
    = new Display();
      Shell shell 
    = new Shell(display);
      
      StyledText styledText;

      
    public SampleStyledText() {
        init();
        
        shell.setLayout(
    new GridLayout());
        
        styledText 
    = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
        
        styledText.setLayoutData(
    new GridData(GridData.FILL_BOTH));
        
        Font font 
    = new Font(shell.getDisplay(), "Courier New"12, SWT.NORMAL);
        styledText.setFont(font);
        
        styledText.setText(
    "123456789\r\nABCDEFGHI");
        
        StyleRange styleRange1 
    = new StyleRange();
        styleRange1.start 
    = 2;
        styleRange1.length 
    = 16;
        styleRange1.foreground 
    = shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
        styleRange1.background 
    = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
        styleRange1.fontStyle 
    = SWT.BOLD;
        
        StyleRange styleRange2 
    = new StyleRange();
        styleRange2.start 
    = 14;
        styleRange2.length 
    = 3;
        styleRange2.fontStyle 
    = SWT.NORMAL;
        styleRange2.foreground 
    = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
        styleRange2.background 
    = shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
        
    //    styledText.setStyleRange(styleRange1);
    //    styledText.setStyleRange(styleRange2);
        
        
    //styledText.setStyleRanges(new StyleRange[]{styleRange1, styleRange2});
        
    //styledText.setStyleRanges(new StyleRange[]{styleRange2, styleRange1});
        
        
    //styledText.setLineBackground(1, 1, shell.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        
    //    styledText.setSelection(4);
    //    System.out.println(printStyleRanges(styledText.getStyleRanges()) );
    //    styledText.insert("000");
        
        
        System.out.println(printStyleRanges(styledText.getStyleRanges()) );
        
    //    styledText.setStyleRanges(new StyleRange[]{styleRange1});
    //    System.out.println(printStyleRanges(styledText.getStyleRanges()) );
        
        
    //shell.pack();
        shell.setSize(300120);
        shell.open();
        
    //textUser.forceFocus();

        
    // Set up the event loop.
        while (!shell.isDisposed()) {
          
    if (!display.readAndDispatch()) {
            
    // If no more entries in event queue
            display.sleep();
          }

        }


        display.dispose();
      }

      
      
    private String printStyleRanges(StyleRange[] styleRanges) {
        
        
    if(styleRanges == null)
          
    return "null";
        
    else if(styleRanges.length == 0)
          
    return "[]";
        
        StringBuffer sb 
    = new StringBuffer();
        
    for(int i=0; i<styleRanges.length; i++{
          sb.append(styleRanges[i] 
    + "\n");
        }

        
        
    return sb.toString();
      }

      
    public static void main(String[] args) {
        
    new SampleStyledText();
      }

    }





    參考:

    You will have to think about margins, page breaks, and scaling, and you
    may want to think about whether or not the chosen fonts exist on the printer.
    For an optimization, you might want to use a set of background images, one
    per page, for the display drawing so that your user can flip between the 'pages'
    without you having to render them again.

    Printing is a lot of work. The widgets don't do it for you - you have to do
    all the drawing yourself. Once you have done the work, however, it is a
    relatively simple thing to add a "print preview" feature.

    The closest thing in SWT to an example of this is the StyledText widget -
    have a look at the code in org.eclipse.swt.custom.StyledText. It doesn't do
    a print preview specifically, but it does know how to draw itself to the
    screen, and to print itself to a printer. Print preview would just be a bit
    of a twist on drawing to the screen.

    主站蜘蛛池模板: 久久亚洲精品无码观看不卡| 成人免费无码精品国产电影| 亚洲动漫精品无码av天堂| 免费无码AV一区二区| 四虎影视永久免费观看| 深夜a级毛片免费无码| 免费在线观看黄网| 国产精品一区二区三区免费| 亚洲一区二区三区香蕉| 巨胸喷奶水www永久免费| 亚洲热线99精品视频| 一级毛片免费观看不卡视频| 亚洲韩国在线一卡二卡| 人成午夜免费视频在线观看| 久久国产亚洲精品| 国产一区二区三区免费在线观看 | 亚洲va中文字幕无码| 又粗又长又爽又长黄免费视频| 亚洲日韩在线中文字幕第一页| 美女网站在线观看视频免费的| 国产亚洲免费的视频看| 51精品视频免费国产专区| 亚洲午夜在线播放| 亚洲精品成人片在线观看| 成人影片一区免费观看| 亚洲国产美女精品久久久久| 女人18毛片水最多免费观看| 一级毛片试看60分钟免费播放| 亚洲欧洲成人精品香蕉网| h视频在线观看免费完整版| 亚洲youwu永久无码精品| 日本亚洲视频在线 | 亚洲国产精品成人综合色在线婷婷 | 亚洲一区二区三区免费观看| 亚洲精品中文字幕无乱码麻豆| 日本特黄特色aa大片免费| 四虎国产精品免费永久在线| 亚洲sss综合天堂久久久| 亚洲人成无码网站久久99热国产| 99视频在线看观免费| 亚洲AV无码专区在线观看成人|