⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sampleshowpicview.java

📁 用java 实现的IE browser适合于学者
💻 JAVA
字号:
/*
 * Created on 2005-1-27
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.hnjchina.example;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.part.ViewPart;

/**
 * @author limeiyong
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SampleShowPicView extends ViewPart{
    public void createPartControl(final Composite parent) {
//        Display display = new Display ();
//        final Shell shell = new Shell (display,SWT.SHELL_TRIM | SWT.NO_BACKGROUND |SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL);
        final	Shell	shell=Display.getCurrent().getActiveShell();
        Image originalImage = null;
        FileDialog dialog = new FileDialog(shell, SWT.OPEN);
        dialog.setText ("选择一个图片文件或者取消");
        String string = dialog.open ();
        if (string != null) {
          originalImage = new Image(parent.getDisplay(),string);
        }
        if (originalImage == null) {
          int width = 150, height = 200;
          originalImage = new Image (shell.getDisplay(), width, height);
          GC gc = new GC (originalImage);
          gc.fillRectangle (0, 0, width, height);
          gc.drawLine (0, 0, width, height);
          gc.drawLine (0, height, width, 0);
          gc.drawText ("默认图片", 10, 10);
          gc.dispose ();
        }
        final Image image = originalImage;
        final Point origin = new Point (0, 0);
//        final ScrollBar hBar = shell.getHorizontalBar ();
        final ScrollBar hBar = parent.getHorizontalBar();
        hBar.addListener (SWT.Selection, new Listener () {
          public void handleEvent (Event e) {
            int hSelection = hBar.getSelection ();
            int destX = -hSelection - origin.x;
            origin.x = -hSelection;
            Rectangle rect = image.getBounds ();
//            shell.scroll (destX, 0, 0, 0, rect.width, rect.height, false);
          }
        });
//        final ScrollBar vBar = shell.getVerticalBar ();
        final ScrollBar vBar =  parent.getVerticalBar();
        vBar.addListener (SWT.Selection, new Listener () {
          public void handleEvent (Event e) {
            int vSelection = vBar.getSelection ();
            int destY = -vSelection - origin.y;
            origin.y = -vSelection;
            Rectangle rect = image.getBounds ();
//            shell.scroll (0, destY, 0, 0, rect.width, rect.height, false);
          }
        });
        parent.addListener(SWT.Resize,  new Listener () {
          public void handleEvent (Event e) {
            Rectangle rect = image.getBounds ();
//            Rectangle client = shell.getClientArea ();
            Rectangle client = parent.getClientArea();
            hBar.setMaximum (rect.width);
            vBar.setMaximum (rect.height);
            hBar.setThumb (Math.min (rect.width, client.width));
            vBar.setThumb (Math.min (rect.height, client.height));
            int hPage = rect.width - client.width;
            int vPage = rect.height - client.height;
            int hSelection = hBar.getSelection ();
            int vSelection = vBar.getSelection ();
            if (hSelection >= hPage) {
              if (hPage <= 0) hSelection = 0;
              origin.x = -hSelection;
            }
            if (vSelection >= vPage) {
              if (vPage <= 0) vSelection = 0;
              origin.y = -vSelection;
            }
            parent.redraw ();
          }
        });
        parent.addListener (SWT.Paint, new Listener () {
          public void handleEvent (Event e) {
            GC gc = e.gc;
            gc.drawImage (image, origin.x, origin.y);
            Rectangle rect = image.getBounds ();
            Rectangle client = parent.getClientArea ();
            int marginWidth = client.width - rect.width;
            if (marginWidth > 0) {
              gc.fillRectangle (rect.width, 0, marginWidth, client.height);
            }
            int marginHeight = client.height - rect.height;
            if (marginHeight > 0) {
              gc.fillRectangle (0, rect.height, client.width, marginHeight);
            }
          }
        });
    }
    public void setFocus() {
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -