我正在尝试将应用程序切换器添加到我正在处理的更大项目中。它需要在 Windows XP/Vista/7/8 上运行。我正在使用 Java 1.7。下面是我创建的示例应用程序,用于演示我遇到的一些问题。我是 JNA 的新手。
非常感谢'Hovercraft Full of Eels' 的 this answer (以及许多其他!)构成测试应用程序的基础。
这是我的问题:
图像绘制 - 我从窗口图标中获取的图像是黑白绘制的。我从 this answer 修改了 getImageForWindow 中的代码麦克道尔(谢谢!)。有没有更好的方法将 HICON 对象转换为 java.awt.Image?我注意到 com.sun.jna.platform.win32.W32API.HICON 中有一个名为“fromNative”的方法,但我不知道如何使用它。
获取图标 - 我用来获取图标句柄的调用 GetClassLongW(hWnd, GCL_HICON) 不会从 64 位窗口返回图标。我想为此我需要 GetClassLongPtr,但我似乎无法通过 JNA 访问它。
根据 Alt-tab 弹出窗口获取正确的窗口列表 - 我试图复制在 this C++ answer 中完成的操作但我无法设法获得用 Java 实现的第二次(GetAncestor 等)和第三次(STATE_SYSTEM_INVISIBLE)检查。我使用了一个糟糕的替代品,它排除了标题为空白的窗口(忽略了一些合法的窗口)。
注意:运行此示例需要 JNA 和 Platform jar:
package test;
import static test.WindowSwitcher.User32DLL.*;
import static test.WindowSwitcher.Gdi32DLL.*;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HWND;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class WindowSwitcher
{
public static void main(String args[])
{
JFrame JF = new JFrame();
JPanel JP = new JPanel(new GridLayout(0, 1));
JF.getContentPane().add(JP);
Vector<WindowInfo> V = getActiveWindows();
for (int i = 0; i < V.size(); i++)
{
final WindowInfo WI = V.elementAt(i);
JButton JB = new JButton(WI.title);
if(WI.image != null)
{
JB.setIcon(new ImageIcon(WI.image));
}
JB.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
SetForegroundWindow(WI.hWnd);
}
});
JP.add(JB);
}
JF.setSize(600,50+V.size()*64);
JF.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JF.setAlwaysOnTop(true);
JF.setFocusableWindowState(false);
JF.setVisible(true);
}
public static Vector<WindowInfo> getActiveWindows()
{
final Vector<WindowInfo> V = new Vector();
EnumWindows(new WNDENUMPROC()
{
public boolean callback(Pointer hWndPointer, Pointer userData)
{
HWND hWnd = new HWND(hWndPointer);
// Make sure the window is visible
if(IsWindowVisible(hWndPointer))
{
int GWL_EXSTYLE = -20;
long WS_EX_TOOLWINDOW = 0x00000080L;
// Make sure this is not a tool window
if((GetWindowLongW(hWndPointer, GWL_EXSTYLE) & WS_EX_TOOLWINDOW) == 0)
{
// Get the title bar text for the window
char[] windowText = new char[512];
GetWindowTextW(hWnd, windowText, windowText.length);
String wText = Native.toString(windowText);
// Make sure the text is not null or blank
if(!(wText == null || wText.trim().equals("")))
{
// Get the icon image for the window (if available)
Image image = getImageForWindow(hWnd, wText);
// This window is a valid taskbar button, add a WindowInfo object to the return vector
V.add(new WindowInfo(wText, hWnd, image));
}
}
}
return true;
}
}, null);
return V;
}
public static Image getImageForWindow(HWND hWnd, String wText)
{
// Get an image from the icon for this window
int hicon = GetClassLongW(hWnd, GCL_HICON);
if(hicon == 0)
return null;
Pointer hIcon = new Pointer(hicon);
int width = 64;
int height = 64;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
draw(image, hIcon, DI_NORMAL);
BufferedImage mask = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
draw(mask, hIcon, DI_MASK);
applyMask(image, mask);
return image;
}
public static void draw(BufferedImage image, Pointer hIcon, int diFlags)
{
int width = image.getWidth();
int height = image.getHeight();
Pointer hdc = CreateCompatibleDC(Pointer.NULL);
Pointer bitmap = CreateCompatibleBitmap(hdc, width, height);
SelectObject(hdc, bitmap);
DrawIconEx(hdc, 0, 0, hIcon, width, height, 0, Pointer.NULL, diFlags);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < width; y++)
{
int rgb = GetPixel(hdc, x, y);
image.setRGB(x, y, rgb);
}
}
DeleteObject(bitmap);
DeleteDC(hdc);
}
private static void applyMask(BufferedImage image,
BufferedImage mask)
{
int width = image.getWidth();
int height = image.getHeight();
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
int masked = mask.getRGB(x, y);
if ((masked & 0x00FFFFFF) == 0)
{
int rgb = image.getRGB(x, y);
rgb = 0xFF000000 | rgb;
image.setRGB(x, y, rgb);
}
}
}
}
static class User32DLL
{
static
{
Native.register("user32");
}
public static native int GetWindowTextW(HWND hWnd, char[] lpString, int nMaxCount);
public static native boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);
public static interface WNDENUMPROC extends com.sun.jna.win32.StdCallLibrary.StdCallCallback
{
boolean callback(Pointer hWnd, Pointer arg);
}
public static native boolean IsWindowVisible(Pointer hWnd);
public static native boolean SetForegroundWindow(HWND hWnd);
public static native int GetWindowLongW(Pointer hWnd, int nIndex);
public static int GCL_HICON = -14;
public static int GCL_HICONSM = -34;
public static native int GetClassLongW(HWND hWnd, int nIndex);
/** @see #DrawIconEx(Pointer, int, int, Pointer, int, int, int, Pointer, int) */
public static final int DI_COMPAT = 4;
public static final int DI_DEFAULTSIZE = 8;
public static final int DI_IMAGE = 2;
public static final int DI_MASK = 1;
public static final int DI_NORMAL = 3;
public static final int DI_APPBANDING = 1;
/** http://msdn.microsoft.com/en-us/library/ms648065(VS.85).aspx */
public static native boolean DrawIconEx(Pointer hdc, int xLeft,
int yTop, Pointer hIcon, int cxWidth, int cyWidth,
int istepIfAniCur, Pointer hbrFlickerFreeDraw,
int diFlags);
}
static class Gdi32DLL
{
static
{
Native.register("gdi32");
}
/** http://msdn.microsoft.com/en-us/library/dd183489(VS.85).aspx */
public static native Pointer CreateCompatibleDC(Pointer hdc);
/** http://msdn.microsoft.com/en-us/library/dd183488(VS.85).aspx */
public static native Pointer CreateCompatibleBitmap(Pointer hdc, int nWidth, int nHeight);
/** http://msdn.microsoft.com/en-us/library/dd162957(VS.85).aspx */
public static native Pointer SelectObject(Pointer hdc, Pointer hgdiobj);
/** http://msdn.microsoft.com/en-us/library/dd145078(VS.85).aspx */
public static native int SetPixel(Pointer hdc, int X, int Y, int crColor);
/** http://msdn.microsoft.com/en-us/library/dd144909(VS.85).aspx */
public static native int GetPixel(Pointer hdc, int nXPos, int nYPos);
/** http://msdn.microsoft.com/en-us/library/dd183539(VS.85).aspx */
public static native boolean DeleteObject(Pointer hObject);
/** http://msdn.microsoft.com/en-us/library/dd183533(VS.85).aspx */
public static native boolean DeleteDC(Pointer hdc);
}
}
class WindowInfo
{
String title;
HWND hWnd;
Image image;
public WindowInfo(String title, HWND hWnd, Image image)
{
this.title = title;
this.hWnd = hWnd;
this.image = image;
}
}
最佳答案
我找到了适合我的目的的解决方法。这比我第一次尝试要简单得多!我现在使用 sun.awt.shell.ShellFolder 获取图标,不幸的是,这是一个未记录/不受支持的类,可能会在未来的 Java 版本中删除。还有另一种使用 FileSystemView 获取图标的方法,但返回的图标对于我的目的来说太小了(在我下面的示例中被注释掉了 - getImageForWindowIcon 方法)。
此解决方法基于 this SO answer by aleroot .我获取进程文件路径(用于打开窗口的 exe 文件,我将其与其他窗口详细信息存储在 WindowInfo 对象中),然后使用 ShellFolder 获取与该文件关联的图标。 注意:这并不总是提供正确的图标(例如,用于运行 Netbeans 进程的文件是 java.exe,因此您得到的是 Java 图标,而不是 Netbeans 图标!)。但在大多数情况下,它运行良好!
解决方法解决了上面的问题 1 和 2,但如果有人有更好的解决方案,请告诉我。我对问题 3 一无所获,但我现在必须做的是窗口列表。
这是我更新的代码... 注意:运行此示例需要 JNA 和 Platform jar:
package test;
import static test.WindowSwitcher.User32DLL.*;
import static test.WindowSwitcher.Kernel32.*;
import static test.WindowSwitcher.Psapi.*;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.ptr.PointerByReference;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import sun.awt.shell.ShellFolder;
public class WindowSwitcher
{
public static void main(String args[])
{
new WindowSwitcher();
}
public WindowSwitcher()
{
JFrame JF = new JFrame("Window Switcher");
JPanel JP = new JPanel(new GridLayout(0, 1));
JF.getContentPane().add(JP);
Vector<WindowInfo> V = getActiveWindows();
for (int i = 0; i < V.size(); i++)
{
final WindowInfo WI = V.elementAt(i);
JButton JB = new JButton(WI.title);
if(WI.image != null)
{
JB.setIcon(new ImageIcon(WI.image));
}
JB.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
SetForegroundWindow(WI.hWnd);
}
});
JP.add(JB);
}
JF.setSize(600,50+V.size()*64);
JF.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JF.setAlwaysOnTop(true);
JF.setFocusableWindowState(false);
JF.setVisible(true);
}
private Vector<WindowInfo> getActiveWindows()
{
final Vector<WindowInfo> V = new Vector();
EnumWindows(new WNDENUMPROC()
{
public boolean callback(Pointer hWndPointer, Pointer userData)
{
HWND hWnd = new HWND(hWndPointer);
// Make sure the window is visible
if(IsWindowVisible(hWndPointer))
{
int GWL_EXSTYLE = -20;
long WS_EX_TOOLWINDOW = 0x00000080L;
// Make sure this is not a tool window
if((GetWindowLongW(hWndPointer, GWL_EXSTYLE) & WS_EX_TOOLWINDOW) == 0)
{
// Get the title bar text for the window (and other info)
WindowInfo info = getWindowTitleAndProcessDetails(hWnd);
// Make sure the text is not null or blank
if(!(info.title == null || info.title.trim().equals("")))
{
// Get the icon image for the window (if available)
info.image = getImageForWindow(info);
// This window is a valid taskbar button, add a WindowInfo object to the return vector
V.add(info);
}
}
}
return true;
}
}, null);
return V;
}
private static final int MAX_TITLE_LENGTH = 1024;
private WindowInfo getWindowTitleAndProcessDetails(HWND hWnd)
{
if(hWnd == null)
return null;
char[] buffer = new char[MAX_TITLE_LENGTH * 2];
GetWindowTextW(hWnd, buffer, MAX_TITLE_LENGTH);
String title = Native.toString(buffer);
PointerByReference pointer = new PointerByReference();
GetWindowThreadProcessId(hWnd, pointer); //GetForegroundWindow()
Pointer process = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pointer.getValue());
GetModuleBaseNameW(process, null, buffer, MAX_TITLE_LENGTH);
String Sprocess = Native.toString(buffer);
GetModuleFileNameExW(process, null, buffer, MAX_TITLE_LENGTH);
String SprocessFilePath = Native.toString(buffer);
return new WindowInfo(title, Sprocess, SprocessFilePath, hWnd, null);
}
private Image getImageForWindow(WindowInfo info)
{
if(info.processFilePath == null || info.processFilePath.trim().equals(""))
return null;
try
{
File f = new File(info.processFilePath);
if(f.exists())
{
// https://stackoverflow.com/questions/10693171/how-to-get-the-icon-of-another-application
// http://www.rgagnon.com/javadetails/java-0439.html
ShellFolder sf = ShellFolder.getShellFolder(f);
if(sf != null)
return sf.getIcon(true);
// Image returned using this method is too small!
//Icon icon = FileSystemView.getFileSystemView().getSystemIcon(f);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
static class Psapi
{
static
{
Native.register("psapi");
}
public static native int GetModuleBaseNameW(Pointer hProcess, Pointer hmodule, char[] lpBaseName, int size);
public static native int GetModuleFileNameExW(Pointer hProcess, Pointer hmodule, char[] lpBaseName, int size);
}
static class Kernel32
{
static
{
Native.register("kernel32");
}
public static int PROCESS_QUERY_INFORMATION = 0x0400;
public static int PROCESS_VM_READ = 0x0010;
public static native Pointer OpenProcess(int dwDesiredAccess, boolean bInheritHandle, Pointer pointer);
}
static class User32DLL
{
static
{
Native.register("user32");
}
public static native int GetWindowThreadProcessId(HWND hWnd, PointerByReference pref);
public static native int GetWindowTextW(HWND hWnd, char[] lpString, int nMaxCount);
public static native boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);
public static interface WNDENUMPROC extends com.sun.jna.win32.StdCallLibrary.StdCallCallback
{
boolean callback(Pointer hWnd, Pointer arg);
}
public static native boolean IsWindowVisible(Pointer hWnd);
public static native boolean SetForegroundWindow(HWND hWnd);
public static native int GetWindowLongW(Pointer hWnd, int nIndex);
}
}
class WindowInfo
{
String title, process, processFilePath;
HWND hWnd;
Image image;
public WindowInfo(String title, String process, String processFilePath, HWND hWnd, Image image)
{
this.title = title;
this.process = process;
this.processFilePath = processFilePath;
this.hWnd = hWnd;
this.image = image;
}
}
关于Java - 使用 JNA 的 Windows 任务栏 - 如何将窗口图标 (HICON) 转换为 Java 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19279134/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。