🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #3365

Re: AttachThreadInput

#3365
From: Mark Guerrieri
Date: Mon, 03 Jul 2006 20:20
79 lines
3575 bytes
> What exactly are you using AttachThreadInput() for?  Perhaps there is an
> alternative approach.

I have an OOP COM server that is basically a 'client' for logging in/out of 
a propriearty system.  Calling the OOP 'Login' method calls 
ForceForegroundWindow() to ensure that the modal login form becomes the 
foreground, focused form.  Upon completion, the previous forground window is 
restored to the foreground with the same method.

I'm using code found frequently on these groups (slightly modified) to force 
a window to the foreground.  Currently I am using the following code:

bool __fastcall ForceForegroundWindow(HWND hwnd)
{
  bool Result;

  DWORD nullvalue = 0;

  DWORD ForegroundThreadID;
  DWORD ThisThreadID;
  DWORD timeout;

  if(IsIconic(hwnd))
    ShowWindow(hwnd, SW_RESTORE);

  HWND hForeWnd = GetForegroundWindow();

  if(hForeWnd == hwnd)
    return true;
  else
  {
 // Windows 98/2000 doesn't want to foreground a window when some other
 // window has keyboard focus
 if( ((Win32Platform == VER_PLATFORM_WIN32_NT) && (Win32MajorVersion > 4))
   || ((Win32Platform == VER_PLATFORM_WIN32_WINDOWS)
   && ((Win32MajorVersion > 4) || ((Win32MajorVersion == 4) &&
   (Win32MinorVersion > 0)))) )
 {
   // Code from Karl E. Peterson, www.mvps.org/vb/sample.htm
   // Converted to Delphi by Ray Lischner
   // Published in The Delphi Magazine 55, page 16

   Result = false;
   ForegroundThreadID = GetWindowThreadProcessId(hForeWnd, NULL);
   ThisThreadID = GetWindowThreadProcessId(hwnd,NULL);

   if(ThisThreadID != ForegroundThreadID && GetThreadDesktop(ThisThreadID) 
!= GetThreadDesktop(ForegroundThreadID))
   {
     if(AttachThreadInput(ThisThreadID, ForegroundThreadID, true) )
    {
      BringWindowToTop(hwnd); // IE 5.5 related hack
      SetForegroundWindow(hwnd);
      AttachThreadInput(ThisThreadID, ForegroundThreadID, false);
      Result = (GetForegroundWindow() == hwnd);
    }
   }
   if(!Result)
   {
  // Code by Daniel P. Stasinski
  SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, &timeout, 0);
  SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, &nullvalue, 
SPIF_SENDCHANGE);
  BringWindowToTop(hwnd); // IE 5.5 related hack
  SetForegroundWindow(hwnd);
  SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, &timeout, 
SPIF_SENDCHANGE);
   }
 }
 else
 {
   BringWindowToTop(hwnd); // IE 5.5 related hack
   SetForegroundWindow(hwnd);
 }
 Result = (GetForegroundWindow() == hwnd);
  }
  return Result;
}
//--------------------------------------------------------------------------- 
Technical Details
Message-ID: <44a97c05$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <44a95f6d$1@newsgroups.borland.com> <44a97313$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,c5eaab2d607d9547
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Mark Guerrieri" <mguerrieri@no_spam_verizon.net>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <44a95f6d$1@newsgroups.borland.com> <44a97313$1@newsgroups.borland.com>
Subject: Re: AttachThreadInput
Date: Mon, 3 Jul 2006 16:20:20 -0400
Lines: 81
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2869
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2869
NNTP-Posting-Host: 71.248.209.167
Message-ID: <44a97c05$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1151958021 71.248.209.167 (3 Jul 2006 13:20:21 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail