java中如何实现在1个窗体上显示一个上浮的半透明窗体

悬赏:20 发布时间:2008-06-27 提问人:still_rain (初级程序员)

RT~大致的场景是这样的,例如1个播放器窗口正播放电影,然后沿这个窗口的右下角慢慢上浮1个半透明效果的窗体,显示获得的文字消息。最好是基于swt或者swing的。

采纳的答案

2008-06-28 lggege (资深程序员)

解答1: 上浮

你直接for循环的setlocation就可以了

while (getLocation().x < 300) {
setLocation(new Point(getLocation().x + 5, 100));
// try {
// Thread.currentThread().sleep(100); // 如果你觉得它跑太快的话
// }catch (InterruptedException e) {
// e.printStackTrace();
// }
}

提问者对于答案的评价:
嗯。谢谢所给的思路。

其他回答

解答2: 透明
	private int fun = 0;
	private int hInst = 0;

	private boolean firstInvoke = true;

	private void resetShell() {
		// 窗口透明
		if (firstInvoke) {
			OS.SetWindowLong(this.handle, OS.GWL_EXSTYLE, OS.GetWindowLong(this.handle, OS.GWL_EXSTYLE) ^ 0x80000);

			TCHAR lpLibFileName = new TCHAR(0, "User32.dll", true);
			hInst = OS.LoadLibrary(lpLibFileName);
			if (hInst != 0) {
				String name = "SetLayeredWindowAttributes\0";
				byte[] lpProcName = new byte[name.length()];
				for (int i = 0; i < lpProcName.length; i++) {
					lpProcName[i] = (byte) name.charAt(i);
				}
				fun = OS.GetProcAddress(hInst, lpProcName);
			}

			firstInvoke = false;
		}

		if (fun != 0) {
			int red = this.getBackground().getRed();
			int green = this.getBackground().getGreen();
			int blue = this.getBackground().getBlue();

			// 最后一位是1的话, 则前面的 999参数被忽略, 窗体是根据前面指定的颜色,将该颜色全透明,其他颜色不透明.
			// 最后一位是2的话, 则前面的 999参数有效, 是指整个窗体的透明度.
			OS.CallWindowProc(fun, this.handle, 0x02000000 | (red & 0xFF) | ((green & 0xFF) <<| ((blue & 0xFF) << 16), 30, 2);
		}
	}


在for循环的setLoction后,调用这句:
OS.FreeLibrary(hInst);


请注意: java去调用lib很费时间,所以,需要将调用lib的动作只做一次,在所有都结束后再释放lib.
lggege (资深程序员) 2008-06-28
最后一段代码显示有问题,重贴:
OS.CallWindowProc(fun, this.handle, 0x02000000 | (red & 0xFF) | ((green & 0xFF) << 8 ) | ((blue & 0xFF) << 16 ), 30, 2);
lggege (资深程序员) 2008-06-28
所以, 你的代码将会是这样的
		while (getLocation().x < 300) {
			setLocation(new Point(getLocation().x + 5, 100));
			resetShell();
		}

		this.resetShell();

		OS.FreeLibrary(hInst);
lggege (资深程序员) 2008-06-28