Solved

focusing window by his handle

Posted on 2002-06-28
3
144 Views
Last Modified: 2010-04-04
How can i focus window if i got his handle?
i mean, like u use alt+tab to focus other window.

Thanks in advance,
        Daniel Leicht.
0
Comment
Question by:tomerlei
3 Comments
 
LVL 8

Expert Comment

by:TOndrej
ID: 7116261
See SetForegroundWindow API in MSDN.
Note the differences on various Windows platforms.
0
 
LVL 4

Author Comment

by:tomerlei
ID: 7116291
what is the exact procedure?
0
 
LVL 7

Accepted Solution

by:
Cynna earned 50 total points
ID: 7116396
tomerlei,

Here's slightly modyfied version of Yorai Aminov's code:


function ForceForegroundWindow(Const hWnd: THandle): Boolean;
var hCurWnd: THandle;
begin
    Result := False;
    if not isWindow(hWnd) then Exit;
    hCurWnd := GetForegroundWindow;
    if (hCurWnd=hWnd) then begin
        Result := True;
        Exit;
    end;
    if (GetWindowLong( hWnd, GWL_STYLE ) and WS_MINIMIZE)<>0 then ShowWindow(hWnd, SW_SHOWNOACTIVATE);
    AttachThreadInput(GetWindowThreadProcessId(hCurWnd, nil),GetCurrentThreadId,True);
    Application.ProcessMessages; // !!!
    SetForegroundWindow(hWnd);
    AttachThreadInput(    GetWindowThreadProcessId(hCurWnd, nil),GetCurrentThreadId, False);
    Result := GetForegroundWindow=hWnd;
end;




DEMO:
-------
(place one Button on the form and start Notepad)


procedure TForm1.Button11Click(Sender: TObject);
begin
  ForceForegroundWindow(FindWindow('Notepad',nil));
end;
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now