Solved

focusing window by his handle

Posted on 2002-06-28
3
148 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

863 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

25 Experts available now in Live!

Get 1:1 Help Now