Java 对操作系统任务栏进行操作

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

本程序是SWT Win32 Extension的一个实例

http://feeling.sourceforge.net/index.php?

 

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.sf.feeling.swt.win32.extension.Win32;
import org.sf.feeling.swt.win32.extension.shell.ApplicationBar;
import org.sf.feeling.swt.win32.extension.shell.Windows;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class TaskbarSnippet {
	private Shell shell;

	private Button onTopBtn;

	private Button autoHideBtn;

	public TaskbarSnippet() {
		shell = new Shell(Display.getDefault());
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		shell.setLayout(layout);
		shell.setText("Task Bar Snippet");

		new Label(shell, SWT.NONE).setText("Current TaskBar State:");
		final Label statusLabel = new Label(shell, SWT.NONE);
		statusLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		statusLabel.setText(getTaskbarState());

		Composite taskBarArea = new Composite(shell, SWT.NONE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		taskBarArea.setLayoutData(gd);

		layout = new GridLayout();
		layout.numColumns = 2;
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		taskBarArea.setLayout(layout);

		if (Win32.getWin32Version() <= Win32.VERSION(5, 0)) {
			new Label(taskBarArea, SWT.NONE).setText("Check TaskBar State:");
			Button autoHideBtn = new Button(taskBarArea, SWT.CHECK);
			autoHideBtn.setEnabled(false);
			autoHideBtn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			autoHideBtn.setText("Auto-hide the taskbar");
			new Label(taskBarArea, SWT.NONE);
			Button onTopBtn = new Button(taskBarArea, SWT.CHECK);
			onTopBtn.setEnabled(false);
			onTopBtn.setText("Keep the taskbar on top of other windows");
			new Label(taskBarArea, SWT.NONE);
			Button applyBtn = new Button(taskBarArea, SWT.PUSH);
			applyBtn.setText("Refresh");
			gd = new GridData();
			gd.widthHint = 60;
			applyBtn.setLayoutData(gd);
			applyBtn.addSelectionListener(new SelectionAdapter() {

				public void widgetSelected(SelectionEvent e) {
					refresh();
					statusLabel.setText(getTaskbarState());
					Display.getDefault().asyncExec(new Runnable(){
						public void run() {
							shell.pack();
						}
					});
				}

			});
		} else {
			new Label(taskBarArea, SWT.NONE).setText("Setup TaskBar State:");
			autoHideBtn = new Button(taskBarArea, SWT.CHECK);
			autoHideBtn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			autoHideBtn.setText("Auto-hide the taskbar");
			new Label(taskBarArea, SWT.NONE);
			onTopBtn = new Button(taskBarArea, SWT.CHECK);
			onTopBtn.setText("Keep the taskbar on top of other windows");

			new Label(taskBarArea, SWT.NONE);
			Button applyBtn = new Button(taskBarArea, SWT.PUSH);
			applyBtn.setText("Apply");
			gd = new GridData();
			gd.widthHint = 60;
			applyBtn.setLayoutData(gd);
			applyBtn.addSelectionListener(new SelectionAdapter() {

				public void widgetSelected(SelectionEvent e) {
					if (onTopBtn.getSelection() && autoHideBtn.getSelection()) {
						ApplicationBar.setAppBarState(shell.handle,
								Win32.STATE_AUTOHIDE_ALWAYSONTOP);
					} else if (onTopBtn.getSelection()) {
						ApplicationBar.setAppBarState(shell.handle,
								Win32.STATE_ALWAYSONTOP);
					} else if (autoHideBtn.getSelection()) {
						ApplicationBar.setAppBarState(shell.handle,
								Win32.STATE_AUTOHIDE);
					} else {
						ApplicationBar.setAppBarState(shell.handle,
								Win32.STATE_NONE);
					}
					statusLabel.setText(getTaskbarState());
					Display.getDefault().asyncExec(new Runnable(){
						public void run() {
							shell.pack();
						}
					});
				}

			});
		}

		shell.layout();

		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!Display.getDefault().readAndDispatch())
				Display.getDefault().sleep();
		}
		Display.getDefault().dispose();
	}

	private String getTaskbarState() {
		int barState = ApplicationBar.getAppBarState(Windows.getSystemTray());
		switch (barState) {
		case Win32.STATE_ALWAYSONTOP:
			return "Taskbar is always on top";
		case Win32.STATE_AUTOHIDE:
			return "Taskbar auto-hides when inactive";
		case Win32.STATE_AUTOHIDE_ALWAYSONTOP:
			return "Taskbar is always on top and auto-hides when inactive";
		default:
			return "Taskbar is not always on top";
		}
	}

	private void refresh() {
		onTopBtn
				.setSelection(ApplicationBar.getAppBarState(shell.handle) == Win32.STATE_ALWAYSONTOP
						|| ApplicationBar.getAppBarState(shell.handle) == Win32.STATE_AUTOHIDE_ALWAYSONTOP);
		autoHideBtn
				.setSelection(ApplicationBar.getAppBarState(shell.handle) == Win32.STATE_AUTOHIDE
						|| ApplicationBar.getAppBarState(shell.handle) == Win32.STATE_AUTOHIDE_ALWAYSONTOP);
	}

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

}

转载于:https://my.oschina.net/crooner/blog/655980

你可能感兴趣的:(Java 对操作系统任务栏进行操作)