Solved

WM_ACTIVATE ???

Posted on 2002-04-28
5
4,375 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

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

816 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

8 Experts available now in Live!

Get 1:1 Help Now