Win32:给窗口顶部 [英] Win32: Bring a window to top

查看:168
本文介绍了Win32:给窗口顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows程序有两个2窗口:

I have a Windows program which has two 2 windows in it:

hwnd (main interface)

hwnd2 (toplevel window, no parent, created by hwnd)

当我双击hwnd ,我需要hwnd2弹出并显示一些数据,所以我使用这个函数带来hwnd2到顶部:

When I double click on hwnd, I need hwnd2 to pop up and show some data, so I use this function to bring hwnd2 to top:

BringWindowToTop(hwnd2);

hwnd2被带到顶部,但有一件事奇怪。当我再次点击hwnd2,hwnd(主界面)自动再次弹出。
我试图使用下面的函数来解决这个问题,但不是他们的工作。

hwnd2 is brought to top, but there is one thing odd. When I click on hwnd2 again, hwnd (main interface) pops itself up again automatically. I tried to use the following function to solve this problem, but non of them works.

SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
                                                                  //doesn't work

BringWindowToTop(hwnd2);    //This is the function brings hwnd2 to top

SetForegroundWindow(hwnd2); //doesn't work

SetWindowPos(hwnd2, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); 
                                                                  //doesn't work

SetWindowPos(hwnd2, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
                                       // hwnd2 "always" on top, not what I want

SetActiveWindow(hwnd2); // doesn't work too (for replying to Magnus Skog, thanks)

SwitchToThisWindow(hwnd2, TRUE);// got the same problem with BringWindowToTop function
SwitchToThisWindow(hwnd2, FALSE);

如何解决这个问题?
提前感谢。

How could I solve this problem? Thanks in advance.

(用于回复aJ,hwnd2没有父级,因为它需要是一个顶级窗口,因此可以在前/回到其他窗口)

(for replying to aJ, hwnd2 doesn't have parent because it needs to be a toplevel window so it can be in front/back of other windows)

(hwnd2是由多个窗口组成的媒体播放器,其中一个窗口用于视频显示,另外两个trackbar控件用于进度条音量栏,一个用于控制面板的工具栏控件。)

(hwnd2 is a media player which is composed of several windows, one of the windows is for video dispaly, two other trackbar controls for progress bar and volume bar, one Toolbar control for control panel.)

(有一个这可能有帮助,无论我点击hwnd2哪个窗口,hwnd自动弹出为鼠标在z顺序的hwnd之上,包括菜单栏和非客户区等)

(There is one this might help, no matter which window I click on hwnd2, hwnd pops up automatically as loong as "the mouse is on top of hwnd in Z-order", including menu bar and non-client area, etc.)

(这个媒体播放器在直接显示。我使用IVideoWindow :: put_Owner将视频窗口作为视频所有者,Direct Show内部创建一个子视频窗口作为视频窗口的子项除了这个子视频窗口,我看不到源代码,我在hwnd2中没有发现任何可疑的东西。)

(This media player is writen in Direct Show. I use IVideoWindow::put_Owner to put video window as the video owner, Direct Show internally creates a sub-video window as a child of the video window. Except for this sub-video window which I can't see the source code, I don't see any thing suspicious in hwnd2.)

我找到了原因,这是因为Direct Show。我使用多线程来执行它,然后问题的解决。但是...为什么??

I found the reason, which is because of Direct Show. I use multithread to execute it, and then the problem's solved. But...why??

这个问题可以通过使用PostMessage(而不是SendMessage)来解决。

This problem can be resolved by using PostMessage (rather than SendMessage).

推荐答案

两个都很好:

::SetForegroundWindow(wnd)

::SetWindowPos(m_hWnd,       // handle to window
            HWND_TOPMOST,  // placement-order handle
            0,     // horizontal position
            0,      // vertical position
            0,  // width
            0, // height
            SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE// window-positioning options
            );

但是请记住,最后一个设置窗口总是在顶部。

But remember that the last one sets the window always on top.

这篇关于Win32:给窗口顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆