Windows set active window

Win32 API. Функции и сообщения ввода информации с клавиатуры
Страница 20. Функция SetActiveWindow

Функция SetActiveWindow

Функция SetActiveWindow активизирует окно.

Параметры
hWnd
Идентифицирует окно верхнего уровня, которое будет активизировано.

Возвращаемые значения
Если функция завершается успешно, величина возвращаемого значения — дескриптор окна, которое было до этого активно.

Замечания
Функция SetActiveWindow активизирует окно, но только не в том случае, когда прикладная программа в фоновом режиме. Окно будет приведено в активный режим (на вершину Z-последовательности), если прикладная программа являлась высокоприоритетной, когда ему устанавливали активность.
Если окно, идентифицированное параметром hWnd, было создано вызывающим потоком, состояние активного окна вызывающего потока устанавливается в hWnd. Иначе, состояние активного окна вызывающего потока устанавливается в значение ПУСТО (NULL).
С другой стороны, функция окна SetForegroundWindow, активизирует окно и переводит его в разряд приоритетных. Прикладная программа должна вызвать SetForegroundWindow только тогда, если требуется отобразить критические ошибки или информацию, которая нуждается в немедленном внимании пользователя.

Смотри также
GetActiveWindow, SetForegroundWindow, WM_ACTIVATE

Размещение и совместимость SetActiveWindow

Activate window

i have a QMainWindow. It has this parameters:

After showEvent calles my window is shown but unactivated. I tried to overload show function:

But it doesn’t help me.

EDIT: When i commented line

everything worked fine, but i need in tool-flag. Any ideas?

EDIT:

  • OS: Linux
  • Programming language: c++
  • Qt version: 4.5.1

7 Answers 7

The Windows Manager Decides

Before I start: As pointed out by elcuco and Javier, focus policy and other aspects of the windows layout (e.g. the title bar) belongs to a substantial extend to the respective windows manager, and Qt might have limited control. To see this, just look at a user interface that has a «focus follows mouse» policy. In these cases, the windows manager might ignore Qt’s focus request. For this reasons, the Qt documentation calls many of the respective flags «hints». Consequently, some of the suggested solutions might or might not work for you.

Читайте также:  Hide the windows taskbar

QApplication::setActiveWindow()

This not withstanding, e.tadeu’s solution to use QApplication::setActiveWindow() works for me for both Windows and Ubuntu with Gnome. I tested it with the following code. Apologies that it is Python using PyQt (I use questions like these to learn a bit about PyQt). It should be fairly easy for you to read it and translate it into C++.

Note that you have to add some handling of the close event of the testWindow , because the app does not exit automatically if you close a Qt::Tool window.

The grabKeyboard() Hack

If this does not work for you, the following hack might. I assume that you have a window in your application that is active. You can then use grabKeyboard() to redirect the input. The Qt::Tool window doesn’t get the focus, but receives the input. The following main code demonstrates it (the other code remains unchanged).

Basically, while the window testWindow2 is the active one, all text entered shows up in testWindow.textEdit . It is not nice, I know.

Creating Your Own Window

You gain the most flexibility (and create the most work for yourself) by rolling out your own window layout. The idea is described in the following FAQ.

Other «Solutions»

You could directly call the respective window manager’s API function to get the desired result (clearly against the very reason for using Qt in the first place). You could also hack the Qt source code. For example, on Windows, Qt uses the ShowWindow() function with a SW_SHOWNOACTIVATE flag, to show a window with style WS_EX_TOOLWINDOW if you set the Qt::Tool flag. You could easily replace the SW_SHOWNOACTIVATE with whatever you want. Linux should be the same. Clearly also not recommended.

Читайте также:  Linux как узнать процесс слушающий порт

Thread: SetActiveWindow

Thread Tools
Display

SetActiveWindow

In a window, i try to set the focus on another window here’s my code :

But for a reason or another the focus stays on the window in wich i called this function. is there something wrong here? thanks

In other words, the window you are messing with has to belong to your current application.

Try this where:
hWnd = other window
Me.hWnd — current window’s hWnd

Can this function work with MDI childs

Yes, it will work on any window.

it’s weird because when I use those functions, it execute the activate of the form i want the focus on but it doesn’t set the focus. No mather when during that I print the MDIMother.ActiveFOrm.Name it gives me the name of the form from the one I want focus is called

well there must be soimething weird in my code cuz
all the above don’t work + setfocus (API) don’t work

Post all of your code maxl, not just a portion of it, and we will take a look. I ask this because the responses I’ve seen from Megatron and jim mcnamara make sense, based on your original question. If their code/suggestions are not working then me thinks their might be variables in your equation that haven’t been shown.

Last edited by Hack; May 8th, 2002 at 06:05 PM .

Anyway, I found a way to make it work without api so Thanks everybody for your response there must have been something weird in my code somewhere. Thanks again

I hope it’s not too late, but you could also use VB’s ZOrder method too (is this what you’re using now?).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  • BB code is On
  • Smilies are On
  • [IMG] code is On
  • [VIDEO] code is On
  • HTML code is Off
Читайте также:  Управление размером дисков windows

Click Here to Expand Forum to Full Width

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.

Оцените статью