📄 utils.java
字号:
/*
* Copyright (c) 2007 Software Wizards Pty Ltd, Victoria, Australia.
* mailto:enquires@swz.com.au. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted. See the GNU General Public License
* for more details.
*
* Redistribution of the SWTtoCOM software is not permitted as part of any
* commercial product. Licensing terms for such distribution may be
* obtained from the copyright holder.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package au.com.swz.swttocom.example.win32explorer.views;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.internal.win32.TCHAR;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
public abstract class Utils {
/**
* Returns the icon that is associated with the specified file.
*
* @param path the file for which the associated icon is required.
* @return the associated image or null if no image is associated
* with the specified file.
*/
public static Image getAssociatedImage(String path) {
int i = path.lastIndexOf('.');
if (i != -1) {
String extension = path.substring(i+1);
Program program = Program.findProgram(extension);
if (program != null) {
ImageData imageData = program.getImageData();
if (imageData != null) {
return new Image(Display.getDefault(), imageData);
}
}
}
return null;
}
private static final String SHELL32 = "C:\\WINDOWS\\SYSTEM32\\SHELL32.DLL";
public static Image extractShell32Image(int nIconIndex) {
return extractFileImage(SHELL32, nIconIndex);
}
public static Image extractFileImage(String path, int nIconIndex) {
TCHAR lpszFile = new TCHAR (0, path, true);
int [] phiconSmall = new int[1], phiconLarge = null;
OS.ExtractIconEx (lpszFile, nIconIndex, phiconLarge, phiconSmall, 1);
if (phiconSmall [0] != 0) {
return Image.win32_new (null, SWT.ICON, phiconSmall[0]);
}
return null;
}
public static Image getKnownShell32Image(String extension) {
if (extension.equalsIgnoreCase("TTF")) {
return extractShell32Image(74);
} else if (extension.equalsIgnoreCase("HTML")) {
return extractShell32Image(13);
}
return null;
}
public static void main(String[] args) {
Display display = new Display ();
Shell shell = new Shell (display);
final Table table = new Table (shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);
int idx = 0;
while (true) {
Image image = extractFileImage("C:\\WINDOWS\\SYSTEM32\\SHELL32.DLL", idx);
if (image == null) {
break;
}
TableItem tableItem = new TableItem(table, SWT.NONE);
tableItem.setText("Index " + idx);
tableItem.setImage(image);
idx++;
}
table.setBounds (0, 0, 300, 600);
shell.pack ();
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -