Solved

WM_ACTIVATE ???

Posted on 2002-04-28
5
4,414 Views
Last Modified: 2011-10-03
Hi,

I want to use this code to always set my windows activate, but it doesn't work???


...
Private
  Procedure ToujoursActif(var Msg :TMessage);
   message WM_ACTIVATE;

...
Procedure TMainForm.ToujoursActif(var Msg :TMessage);
begin
if Msg.WParam = WA_INACTIVE then SendMessage(handle, WM_ACTIVATE, WA_ACTIVE,0);
end;



logicaly, this code work, cause if i put SendMessage(handle, WM_ACTIVATE, WA_ACTIVE,0); in a button, it's ok
0
Comment
Question by:DelphiCool
  • 3
  • 2
5 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 6975097
hi,
use TWMActivate mesage instead,declare it like this:

private
    { Private declarations }
    procedure WMActivate( Var msg: TWMActivate );
      message WM_ACTIVATE;
 public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.WMActivate( Var msg: TWMActivate );
begin
  If msg.Active = WA_INACTIVE Then
PostMessage(handle, WM_ACTIVATE, WA_ACTIVE,0);
  inherited;
end;


the above will only make the window active and not nessesarily at the top though,so maybe you may want to add setforegroundwindow etc as well.
0
 

Author Comment

by:DelphiCool
ID: 6975124
It doesn't work inthe

The form isn't bringged to top

I try your code and add
    setforegroundwindow(handle);
    BringWindowToTop(handle);

but no way

Why did you prefer  procedure WMActivate( Var msg: TWMActivate );
instead of Procedure ToujoursActif(var Msg :TMessage);

to work only with WM_activate message ??
0
 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
ID: 6975200
>>Why did you prefer  procedure WMActivate( Var msg: TWMActivate );
>>instead of Procedure ToujoursActif(var Msg :TMessage);
>>to work only with WM_activate message ??

exactly..

ok the following is getting silly but im guesssing your using 2k or something else setforegroundwindow would be working.
its not small but is nessesary ,it came form the borland newsgroups with parts from different sources.
http://groups.google.com/groups?q=forceforegroundwindow+delphi+ray&hl=en&selm=3A62F81B.7790A088%40ia.comweb.nl&rnum=1


private
    { Private declarations }
    procedure WMActivate( Var msg: TWMActivate );
      message WM_ACTIVATE;
 public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

function ForceForeground(AppHandle:HWND): boolean;
const
SPI_GETFOREGROUNDLOCKTIMEOUT = $2000;
SPI_SETFOREGROUNDLOCKTIMEOUT = $2001;
var
ForegroundThreadID: DWORD;
ThisThreadID      : DWORD;
timeout           : DWORD;
OSVersionInfo     : TOSVersionInfo;
Win32Platform     : Integer;
begin
if IsIconic(AppHandle) then ShowWindow(AppHandle, SW_RESTORE);
if (GetForegroundWindow = AppHandle) then Result := true else
begin
  Win32Platform := 0;
  OSVersionInfo.dwOSVersionInfoSize := SizeOf(OSVersionInfo);
  if GetVersionEx(OSVersionInfo) then Win32Platform := OSVersionInfo.dwPlatformId;

  { Windows 98/2000 doesn't want to foreground a window when some other window has keyboard focus}

  if ((Win32Platform = VER_PLATFORM_WIN32_NT) and (OSVersionInfo.dwMajorVersion > 4)) or
     ((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and ((OSVersionInfo.dwMajorVersion > 4) or
     ((OSVersionInfo.dwMajorVersion = 4) and (OSVersionInfo.dwMinorVersion > 0)))) then
  begin
    Result := false;
    ForegroundThreadID := GetWindowThreadProcessID(GetForegroundWindow,nil);
    ThisThreadID := GetWindowThreadPRocessId(AppHandle,nil);
    if AttachThreadInput(ThisThreadID, ForegroundThreadID, true) then
    begin
      BringWindowToTop(AppHandle);
      SetForegroundWindow(AppHandle);
      AttachThreadInput(ThisThreadID, ForegroundThreadID, false);
      Result := (GetForegroundWindow = AppHandle);
    end;
    if not Result then
    begin
      SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, @timeout, 0);
      SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(0), SPIF_SENDCHANGE);
      BringWindowToTop(AppHandle);
      SetForegroundWindow(AppHandle);
      SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(timeout), SPIF_SENDCHANGE);
      Result := (GetForegroundWindow = AppHandle);
      if not Result then
        begin
        ShowWindow(AppHandle,SW_HIDE);
        ShowWindow(AppHandle,SW_SHOWMINIMIZED);
        ShowWindow(AppHandle,SW_SHOWNORMAL);
        BringWindowToTop(AppHandle);
        SetForegroundWindow(AppHandle);
        end;
    end;
  end else
  begin
    BringWindowToTop(AppHandle);
    SetForegroundWindow(AppHandle);
  end;
  Result := (GetForegroundWindow = AppHandle);
end;
end;



procedure TForm1.WMActivate( Var msg: TWMActivate );
begin
  If msg.Active = WA_INACTIVE Then
ForceForeground(handle);
 inherited;
  end;


see here for details of a new win2k api allowsetforegroundwindow:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/windows_5fw7.asp
0
 

Author Comment

by:DelphiCool
ID: 6975223
thanks inthe

you get the points

It's bizare, when I click on another window, my programme is bring to top, but if i click another time on the other window, my prog doesn't came on top


PS I'm using windows ME

Thanks
0
 
LVL 17

Expert Comment

by:inthe
ID: 6975236
yeh i noticed that also.
there are other ways around this but they can get long and messy.i guess thats why they made this new api AllowSetFor..() , the way ms made all the windows versions with a different mechanism for putting a window to top is not good (IMHO :-)

also maybe using modal forms etc and different formstyles (ie FsStayOnTop) can improve what your trying to do.

good luck
Barry
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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 …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

840 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