Solved

focusing window by his handle

Posted on 2002-06-28
3
154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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