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

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

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

    West Farm
    吾本布衣,生于川北,躬耕于代碼的田地上。
    posts - 16,  comments - 15,  trackbacks - 0
    Important:該類有諸多不盡人意的地方,讀者如果要用,請自行完善,但是至少值得一試。
    原代碼是一個老外寫的在桌面右下角彈出消息提示的一個類,我進行了改寫。
    任何改進,希望你能不吝分享。



    import org.eclipse.swt.SWT;
    import org.eclipse.swt.graphics.Color;
    import org.eclipse.swt.graphics.Font;
    import org.eclipse.swt.graphics.FontData;
    import org.eclipse.swt.graphics.GC;
    import org.eclipse.swt.graphics.Image;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.graphics.Rectangle;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Event;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Listener;
    import org.eclipse.swt.widgets.Scrollable;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.wb.swt.SWTResourceManager;

    /**
     * 具有淡入淡出效果且不需要用戶點擊關閉的消息提示框。
     * 
    @author ggfan@amarsoft
     *
     
    */
    public class Notifier {
        
    private static final int DISPLAY_TIME = 2000;
        
        
    private static final int FADE_TIMER = 50;
        
        
    private static final int FADE_IN_STEP = 30;
        
        
    private static final int FADE_OUT_STEP = 8;

        
    private static final int FINAL_ALPHA = 225;

        
    public static int DEFAULT_WIDTH = 150;
        
        
    public static  int DEFAULT_HEIGHT = 60;

        
    private static Color _titleFgColor = SWTResourceManager.getColor(407397);

        
    private static Color _fgColor = _titleFgColor;

        
    private static Color _bgFgGradient = SWTResourceManager.getColor(226239249);

        
    private static Color _bgBgGradient = SWTResourceManager.getColor(177211243);

        
    private static Color _borderColor = SWTResourceManager.getColor(407397);

        
    private static Image _oldImage;
        
    // TODO Scrollable可能不合適
        public static void notify(Scrollable scrollable, final String msg) {

            
    final Shell parentShell = scrollable.getShell();
            
    final Shell newShell = new Shell(parentShell, SWT.NO_FOCUS | SWT.NO_TRIM);
            newShell.setLayout(
    new FillLayout());
            newShell.setForeground(_fgColor);
            newShell.setBackground(_bgBgGradient);
            newShell.setBackgroundMode(SWT.INHERIT_FORCE);
            scrollable.addDisposeListener(new DisposeListener(){
        public void widgetDisposed(DisposeEvent e) {
    newShell.dispose();
        }
    });
            
    final Composite inner = new Composite(newShell, SWT.NONE);
            FillLayout layout 
    = new FillLayout();
            layout.marginWidth 
    = 20;
            layout.marginHeight 
    = 20;
            inner.setLayout(layout);

            newShell.addListener(SWT.Resize, 
    new Listener() {
                
    public void handleEvent(Event event) {
                    
    try {
                        Rectangle rect 
    = newShell.getClientArea();
                        Image newImage 
    = new Image(Display.getDefault(), Math.max(1, rect.width), rect.height);
                        GC gc 
    = new GC(newImage);
                        
    // 背景
                        gc.setForeground(_bgFgGradient);
                        gc.setBackground(_bgBgGradient);
                        gc.fillGradientRectangle(rect.x, rect.y, rect.width, rect.height, 
    true);
                        
    // 邊框
                        gc.setLineWidth(2);
                        gc.setForeground(_borderColor);
                        gc.drawRectangle(rect.x 
    + 1, rect.y + 1, rect.width - 2, rect.height - 2);

                        gc.dispose();
                        newShell.setBackgroundImage(newImage);
                        
    if (_oldImage != null) {
                            _oldImage.dispose();
                        }
                        _oldImage 
    = newImage;
                    } 
    catch (Exception err) {
                        err.printStackTrace();
                    }
                }
            });

            Label text 
    = new Label(inner, SWT.WRAP | SWT.CENTER);
            Font tf 
    = text.getFont();
            FontData tfd 
    = tf.getFontData()[0];
            tfd.setStyle(SWT.BOLD);
            tfd.height 
    = 8;
            text.setFont(SWTResourceManager.getFont(tfd.getName(), 
    8, SWT.NORMAL));
            text.setForeground(_fgColor);
            text.setText(msg);

            newShell.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

            
    if (Display.getDefault().getActiveShell() == null || Display.getDefault().getActiveShell().getMonitor() == null) { 
                
    return
            }
            newShell.setLocation(computePoint(scrollable));
            newShell.setAlpha(
    0);
            newShell.setVisible(
    true);

            fadeIn(newShell);
        }
        
        
    // TODO 當有滾動條出現的時候是否能夠居中?
        private static Point computePoint(Scrollable scrollable) {
            Point p 
    = scrollable.toDisplay(scrollable.getClientArea().x, scrollable.getClientArea().y);
            
    int w = scrollable.getClientArea().width;
            
    int h = scrollable.getClientArea().height;
            p.x 
    += w / 2 - DEFAULT_WIDTH / 2 ;
            p.y 
    += h / 2 - DEFAULT_HEIGHT / 2
            
    return p;
        }

        
    private static void fadeIn(final Shell _shell) {
            Runnable run 
    = new Runnable() {
                @Override
                
    public void run() {
                    
    try {
                        
    if (_shell == null || _shell.isDisposed()) {
                            
    return;
                        }

                        
    int cur = _shell.getAlpha();
                        cur 
    += FADE_IN_STEP;

                        
    if (cur > FINAL_ALPHA) {
                            _shell.setAlpha(FINAL_ALPHA);
                            startTimer(_shell);
                            
    return;
                        }

                        _shell.setAlpha(cur);
                        Display.getDefault().timerExec(FADE_TIMER, 
    this);
                    } 
    catch (Exception err) {
                        err.printStackTrace();
                    }
                }
            };
            Display.getDefault().timerExec(FADE_TIMER, run);
        }

        
    private static void startTimer(final Shell _shell) {
            Runnable run 
    = new Runnable() {

                @Override
                
    public void run() {
                    
    try {
                        
    if (_shell == null || _shell.isDisposed()) {
                            
    return;
                        }

                        fadeOut(_shell);
                    } 
    catch (Exception err) {
                        err.printStackTrace();
                    }
                }

            };
            Display.getDefault().timerExec(DISPLAY_TIME, run);

        }

        
    private static void fadeOut(final Shell _shell) {
            
    final Runnable run = new Runnable() {

                @Override
                
    public void run() {
                    
    try {
                        
    if (_shell == null || _shell.isDisposed()) {
                            
    return;
                        }

                        
    int cur = _shell.getAlpha();
                        cur 
    -= FADE_OUT_STEP;

                        
    if (cur <= 0) {
                            _shell.setAlpha(
    0);
                             
    if (_oldImage != null) {
                                 _oldImage.dispose();
                             }
                            _shell.dispose();
                            
    return;
                        }

                        _shell.setAlpha(cur);

                        Display.getDefault().timerExec(FADE_TIMER, 
    this);

                    } 
    catch (Exception err) {
                        err.printStackTrace();
                    }
                }

            };
            Display.getDefault().timerExec(FADE_TIMER, run);

        }
    }
    posted on 2011-10-12 10:48 West Farmer 閱讀(3268) 評論(6)  編輯  收藏 所屬分類: Eclipse-RCP

    FeedBack:
    # re: SWT:淡入淡出消息提示框,無需用戶干預。
    2011-10-12 18:04 | [西部農民]
    bug fix:
    scrollable.addDisposeListener(new DisposeListener(){
    public void widgetDisposed(DisposeEvent e) {
    newShell.dispose();
    }
    });  回復  更多評論
      
    # re: SWT:淡入淡出消息提示框,無需用戶干預。[未登錄]
    2013-02-27 16:12 | Sam
    當多個消息提示框同時顯示,會出現整個程序無響應的問題,我是在Eclipse RCP里用的,不知道的什么原因  回復  更多評論
      
    # re: SWT:淡入淡出消息提示框,無需用戶干預。
    2013-03-01 14:58 | @shrinkAshirt
    bug fixed:
    scrollable.addDisposeListener(new DisposeListener() {

    @Override
    public void widgetDisposed(DisposeEvent arg0) {
    // TODO Auto-generated method stub
    newShell.dispose();
    }
    });  回復  更多評論
      
    # re: SWT:淡入淡出消息提示框,無需用戶干預。
    2013-03-01 15:29 | @shrinkAshirt
    傳入scrollable干什么
      回復  更多評論
      
    # re: SWT:淡入淡出消息提示框,無需用戶干預。
    2013-03-14 00:13 | [西部農民]
    多個彈出框同時出現應該是不支持的,就像QQ的消息通知。是要一個一個顯示的,這應該和UI應用的線程模式有關。

    @Sam
      回復  更多評論
      
    # re: SWT:淡入淡出消息提示框,無需用戶干預。
    2015-10-29 14:59 | 李堯
    博主,怎么調用啊,寫全代碼吧,沒法用  回復  更多評論
      

    <2015年10月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿

    隨筆分類

    隨筆檔案

    相冊

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 无码AV动漫精品一区二区免费| 亚洲av无码片在线观看| 含羞草国产亚洲精品岁国产精品| 综合在线免费视频| 亚洲中文无码av永久| 在线观看视频免费完整版| 日韩亚洲国产高清免费视频| 成人免费视频小说| 亚洲第一成年网站视频| 国产片免费在线观看| 午夜免费国产体验区免费的| 国产精品亚洲产品一区二区三区| 国产精品免费久久| 国产亚洲高清不卡在线观看| 久久午夜羞羞影院免费观看| 亚洲国产亚洲片在线观看播放| 野花高清在线观看免费3中文| 亚洲精品无码久久久久秋霞| 免费99热在线观看| a毛片免费播放全部完整| 亚洲国产精品线在线观看| 99久久99这里只有免费费精品| 亚洲最大无码中文字幕| 国产精品另类激情久久久免费 | 亚洲黄网站wwwwww| 青春禁区视频在线观看直播免费| 综合偷自拍亚洲乱中文字幕| 亚洲精品国产综合久久一线| 中文字幕在线免费| 亚洲精品国产第一综合99久久| 亚洲片国产一区一级在线观看| 久草视频在线免费看| 亚洲国产乱码最新视频| 国产AⅤ无码专区亚洲AV| 中文字幕免费在线观看| 国产精品亚洲一区二区三区久久| 日韩精品亚洲aⅴ在线影院| 亚洲精品免费在线| 无遮挡呻吟娇喘视频免费播放| 91精品国产亚洲爽啪在线影院| 日韩免费电影在线观看|