Blinking SWT Window in Windows 7 taskbar

287 views Asked by At

I have an SWT window and I want that it can blink when it is minimized in the taskbar.

A friend of mine told me that I should try to activate (setActive()) the minimized window and then it should blink.. But that doesn't work.. Do you have any suggestions?

Thanks for your help!

Kind regards!

1

There are 1 answers

0
Brian_Entei On

After much experimentation, I eventually came up with my own method of doing this. It even continues to flash after 5 times if the shell does not have focus!

    public static final void flashTaskBar(Runnable code) {
        TaskItem item = getTaskBarItem();
        long startTime = System.currentTimeMillis();
        int state = item.getProgressState();
        int progress = item.getProgress();
        item.setProgressState(SWT.PAUSED);
        item.setProgress(100);
        boolean flashOn = true;
        int flashes = 0;
        int maxFlashes = 5;
        while(true) {
            long now = System.currentTimeMillis();
            if(now - startTime >= 500L) {
                startTime = now;
                item.setProgressState(flashOn ? SWT.DEFAULT : SWT.PAUSED);
                flashOn = !flashOn;
                if(!flashOn) {
                    flashes++;
                }
                if(flashes >= maxFlashes) {
                    item.setProgressState(state);
                    item.setProgress(progress);
                    return;
                }
            }
            if(code == null) {
                try {
                    Thread.sleep(10L);
                } catch(InterruptedException ignored) {
                    Thread.currentThread().interrupt();
                }
            } else {
                try {
                    code.run();
                } catch(Throwable e) {
                    e.printStackTrace();
                    item.setProgressState(state);
                    item.setProgress(progress);
                    return;
                }
            }
        }
    }

And for getTaskBarItem():

public static TaskItem getTaskBarItem() {
    TaskBar bar = display.getSystemTaskBar();
    if(bar == null) return null;
    TaskItem item = bar.getItem(shell);
    if(item == null) item = bar.getItem(null);
    return item;
}