以编程方式检测窗口最大化/最小化??? Eclipse RCP
正如标题所示,我想向我的 rcp 用户界面添加一个侦听器,以便检测最大化和最小化。其实这不是我真正的目的,但我认为这是解决我的问题的一种方法。我有一个中心有一些形状的视图,即使调整窗口大小,我也不会将绘图精确地保持在中心。为此,我使用了以下侦听器:
public void createPartControl(final Composite parent) {
display = parent.getDisplay();
white= display.getSystemColor(SWT.COLOR_WHITE);
parent.setLayout(new FillLayout(SWT.VERTICAL));
final ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinHeight(100);
sc.setMinWidth(100);
sc.setSize(565, 305);
final Composite child = new Composite(sc,SWT.NONE);
child.setLayout(new FillLayout());
// Set child as the scrolled content of the ScrolledComposite
sc.setContent(child);
child.setBackground(white);
gc = new GC(child);
parent.addListener (SWT.Resize, new Listener () {
public void handleEvent (Event e) {
x = child.getBounds().width/2;
y = child.getBounds().height/2;
child.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent event) {
dessin(gc); // draw my shapes
}
});
}
一切都很顺利,除了当我最大化窗口然后最小化它时,在这种情况下我松开了绘图(它位于角落)。 有什么想法吗?我的想法正确吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检测最小化和非最小化(不一定是最大化)的两个事件是
Iconify
和Deiconify
,它们仅发生在 Shell 上。请参阅 javadocs 外壳。The two events to detect minimization and un-minimization (not necessarily maximization) are
Iconify
andDeiconify
which only occur on the Shell. See the javadocs for Shell.考虑移动父级的调整大小事件,因为子级不一定需要调整大小。
Consider moving the resize event is seen for the parent, as the child need not necessarily be resized yet.
为了将某些内容保持在其他内容的中心,您所需要的只是
SWT.Resize
事件,因此这个问题是 XY 问题。 (除了这种情况下的OP似乎已经怀疑这可能是一个XY问题。)但是,许多人提出这个问题时都有合理的需要以编程方式检测窗口最小化/最大化/恢复事件,原因如下
:如果您希望能够在退出时保存应用程序窗口的边界,则不能只保存 Shell.getBounds() 返回的任何内容,因为您的应用程序可能会在最小化、最大化或全屏时终止,在这种情况下,其边界不应该是坚持下来了。应该保留的是 shell 的最小化/正常/最大化/全屏状态(我称之为“姿势”),以及 shell 上次姿势为“正常”时的边界。因此,本质上,您需要跟踪姿势何时“正常”,为此您需要有一个“姿势改变”事件。
问题是,当 SWT 发出“deiconified”事件时,它尚未计算 shell 的边界,因此在这种情况下获得的值是伪造的。
因此,这里是解决方案:
您将需要一种重新计算姿势的方法,如下所示:
为了生成“最大化”、“恢复(从最大化)”和“全屏”事件,您可以使用
Shell.addListener()
用于侦听SWT.Move
和SWT.Resize
事件,并在它们发生时调用recalculatePosture()
发生。为了生成“最小化”事件,您可以使用
ShellListener
的shellIconified()
方法,如 @the.duckman 所说,然后再次调用recalculatePosture( )
。为了生成“恢复(从最小化)”事件,您需要在
ShellListener
中执行以下操作:这将导致在之后的短时间内重新计算姿势 'deiconified' 事件,此时 SWT 将开始正确计算 shell 的边界。
In order to keep something in the center of something else all you need is the
SWT.Resize
event, so this question is a classic case of the XY Problem. (Except that the OP in this case seems to already suspect that this may be an XY Problem.)However, many people arrive at this question with a legitimate need to programmatically detect window minimized / maximized / restored events, for the following reason:
If you want to be able to save the bounds of your application window on exit, you cannot just save whatever is returned by Shell.getBounds(), because your application may be terminated while minimized or maximized or fullscreen, in which case its bounds should not be persisted. What should be persisted is the minimized/normal/maximized/fullscreen state of the shell, (I call it "posture",) and the bounds of the shell last time its posture was "normal". So, essentially, you need to keep track of when the posture is "normal", and for that you need to have a "posture changed" event.
The problem is that when SWT issues the "deiconified" event, it has not calculated the bounds of the shell yet, so the value that you get in that case is bogus.
So, here is the solution to that:
You are going to need a method which recalculates the posture as follows:
In order to generate the "maximized", "restored (from maximized)" and "fullscreen" events you can use
Shell.addListener()
to listen for theSWT.Move
andSWT.Resize
event, and invokerecalculatePosture()
when they occur.In order to generate the "minimized" event you can use the
shellIconified()
method of theShellListener
as @the.duckman said, and again, invokerecalculatePosture()
.In order to generate the "restored (from minimized)" event, you need to do the following in your
ShellListener
:This will cause the recalculation of posture a short time after the 'deiconified' event, at which point SWT will have gotten around to properly calculating the bounds of the shell.