Solved

focusing window by his handle

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TVirtualStringTree  search using TEdit 7 121
Find and Replace Stream with 0s 8 62
Adoquery sql  left join does not work 25 89
how to resize animated Gif image in delphi ? 1 30
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 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