发送一个消息,将遮盖的窗口放到最前面。而不是标题闪烁。(100分)

  • 主题发起人 主题发起人 xxy888
  • 开始时间 开始时间
X

xxy888

Unregistered / Unconfirmed
GUEST, unregistred user!
为了避免程序运行二次,第二次运行时自动关闭,并发送一个消息,
使第一次运行的程序获得焦点,并将遮盖的窗口放到最前面。而不
是标题闪烁。
现主要问题是:并将遮盖的窗口放到最前面。而不是标题闪烁。
 
var
hMutex,hApp:HWND;
begin
Application.Initialize;
//建立互斥对象
hMutex:=CreateMutex(nil,False,PChar(SAppName));
If GetLastError=ERROR_ALREADY_EXISTS then
begin
//已经运行,找到句柄
hApp:=FindWindow('TApplication',PChar(SAppName));
//激活
if IsIconic(hApp) then
PostMessage(hApp,WM_SYSCOMMAND,SC_RESTORE,0);
SetForegroundWindow(hApp);
//释放互斥对象
ReleaseMutex(hMutex);
Application.Terminate;
exit;
end;
Application.CreateForm(TMDIMainForm, MDIMainForm);
Application.Run;
//释放互斥对象
ReleaseMutex(hMutex);
 
既然是发送了消息给原先的程序,为什么不在原先的程序接收到这个消息的时候
if IsIconic(Application.Handle) then
Application.Restore;
Application.BringToFront;
这样就可以了。
 
Application.BringToFront;
可能会失败!
 
procedure TForm1.Button1Click(Sender: TObject);
var
TheWindow: HWND;
begin
{find a handle to the Windows Explorer window}
TheWindow:=FindWindow('ExploreWClass',nil);
{bring it into the foreground}
SetForegroundWindow(TheWindow);
end;

使用上面的方法试一下吧。
还可以使用下面的方法得到最上面的窗口。
procedure TForm1.Timer1Timer(Sender: TObject);
var
TheWindowText: array[0..255] of char;
TheForegroundWindow: HWND;
begin
{get a handle to the foreground window}
TheForegroundWindow:=GetForegroundWindow;
{get it's caption text}
GetWindowText(TheForegroundWindow, TheWindowText, 255);
{display the foreground window's caption}
Label2.Caption:='Foreground Window Text: '+TheWindowText;
end;
 
下面的函数可以帮你,支持98/me/nt/2000/xp
function ForceForegroundWindow(hwnd: THandle): boolean;
const
SPI_GETFOREGROUNDLOCKTIMEOUT = $2000;
SPI_SETFOREGROUNDLOCKTIMEOUT = $2001;
var
ForegroundThreadID: DWORD;
ThisThreadID: DWORD;
timeout: DWORD;
begin
if IsIconic(hwnd) then
ShowWindow(hwnd, SW_RESTORE);
if GetForegroundWindow = hwnd then
Result := true
else
begin
if ((Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion > 4))
or
((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and
((Win32MajorVersion > 4) or ((Win32MajorVersion = 4) and
(Win32MinorVersion > 0)))) then
begin
Result := false;
ForegroundThreadID :=
GetWindowThreadProcessID(GetForegroundWindow, nil);
ThisThreadID := GetWindowThreadPRocessId(hwnd, nil);
if AttachThreadInput(ThisThreadID, ForegroundThreadID, true) then
begin
BringWindowToTop(hwnd);
// IE 5.5 related hack
SetForegroundWindow(hwnd);
AttachThreadInput(ThisThreadID, ForegroundThreadID, false);
Result := (GetForegroundWindow = hwnd);
end;
if not Result then
begin
SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, @timeout, 0);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, TObject(0),
SPIF_SENDCHANGE);
BringWindowToTop(hwnd);
// IE 5.5 related hack
SetForegroundWindow(hWnd);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0,
TObject(timeout), SPIF_SENDCHANGE);
end;
end
else
begin
BringWindowToTop(hwnd);
// IE 5.5 related hack
SetForegroundWindow(hwnd);
end;
Result := (GetForegroundWindow = hwnd);
end;
end;
{ ForceForegroundWindow }
 
问题还没有解决。
 
后退
顶部