Delphi: Show window without activation
Solution 1
There has to be something wrong with your code.
I tested this code, it works:
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowWindow(Form2.Handle, SW_SHOWNOACTIVATE);
Form2.Visible := True;
end;
Be careful to use Visible
, not Show
! Otherwise it'll override the SW_SHOWNOACTIVATE
.
Solution 2
You can show the window (non modal) and reset the focus to the mainwindow.
procedure TMainForm.ButtonClick(Sender: TObject);
begin
OtherForm.Show;
SetFocus;
end;
Tested on 2006.
This does not show the other form on top. But it is very counter intuitive to have a window on top that does not have the focus.
Solution 3
I've used this in the past
SetWindowPos(WindowHandle, HWND_TOP, 0, 0, 0, 0, SWP_NOACTIVATE or SWP_NOSIZE or SWP_NOMOVE);
I've not tested this with recent versions of Delphi though...
Solution 4
If possible, you should considered using some sort of tool tip window to display the notification information. A tool tip will not steal focus from you main window when it is displayed or when a user clicks on it. A regular form will have a border by default and if the user clicks on that border your main form will loose focus.
Here is some basic code to do this. The tip disappears when free is called; however you would be better off setting a timer than using sleep.
with THintWindow.Create(nil) do
try
ActivateHint(MyRect, 'My Notification');
Sleep(DisplayTime);
finally
Free;
end
Solution 5
Here you are:
// you have set your 2nd form as non resizable, without border nor title etc...
Form2.Enabled := False; // prevent the 2nd form to grab focus even when clicked
SetWindowPos(Form2.Handle, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW or SWP_NOACTIVATE or SWP_NOSIZE or SWP_NOMOVE);
// be sure to hide it automatically when done as it is disabled...
Comments
-
Vegar almost 2 years
I struggle to show a second form above the main form without losing focus.
I have tried
ShowWindow(second.handle, SW_SHOWNOACTIVATE)
, but the mainform loses focus. If I setVisible := false
on the second window, the call toShowWindow
does not activate the second form, but the windows is empty when shown...Does anyone have a good recipe for this?
UPDATE: What I'm trying to do, is showing a notify-window at a given event. It's crucial that the main form does not lose focus at any time.
-
Vegar about 15 yearsBut this would be enough to trigger events and datastorage etc, and I don't want that...
-
Vegar about 15 yearsI'm sorry, but it's still activated :-(
-
Daniel Rikowski about 15 yearsI modified my answer and tested it :)
-
Vegar about 15 yearsMaybe this is the easiest way to go, but I would have to customize the hintwindow to show more than just simple text...
-
Vegar about 15 yearsWow, it's amazing :-) The tricks seems to be to let visible be false at designtime, and set it to true after the call to showwindow( ). Thanks!
-
DeveloperChris almost 14 yearsthis doesnt work under various circumstances, for example if you are working (say editing text) in a child form of the main form and the main form needs to popup a notification. you lose any characters you are in the process of typing plus setting the focus back to the main form does not set the focus back to the original child form you were editing in. This problem haunted me for a long time until I found DR's simple (I should have thought of that) fix.
-
DeveloperChris almost 14 yearsWonderful. I tried everything to get this to work for me except the setting the Visible flag. It also helped fix the render offscreen and reposition before display problem I had. Up one from me :)
-
Edwin Yip about 12 yearsJust in need for such an example :)
-
Sean Ferris almost 9 yearsThis worked perfectly for me when used in conjunction with Marius's answer to ensure the form appeared on top. Thanks all!
-
Warren P about 8 yearsThis makes it actually visible without setting the Form Visible?