Recent

Author Topic: Application on background  (Read 11313 times)

jpremor

  • New Member
  • *
  • Posts: 11
Application on background
« on: February 22, 2010, 07:46:58 pm »
Hi all,

I need to open it and send it to background without notice on initialization. The form is needed to configure and etc... So, when you run it for the second time it shouldnt open another instance but open the old program showing the hidden form.

First my problem is send it to background. I know that the X button at the top do not close and send it to background as i desire, but i tried this next code for sending to background without success.

1) Form.hide - it send the program to background but it is not shown on task manager as a application but as a process. It was not possible to see the form again.

2) Form.WindowState := wsMinimized; - dont cause any effect. I dont know if minimize is the logical operation that i need.

3) there is a function function SetForegroundWindow(hWnd:HWND):WINBOOL; external KernelDLL name 'SetForegroundWindow'; but it does not have the SetBackgroundWindow. Otherwise i cannot find my window handler too. I'll explain it on two instances problem.



About the two instances i tried to use mutex. Tere still happening the next instances

Code: [Select]
    Mutex := CreateMutex(nil, True, 'DynDNSInstance');
    Error := GetLastError;
    if Error = ERROR_ALREADY_EXISTS then begin
        EnumWindows(@EnumWindowsProc, Longint(@W)); //enumwindowsproc must compare with self as in
        CloseHandle(H);
        Halt;
    end;
    else if (Mutex <> 0) and (Error = 0) then begin
        Application.Initialize;
        Application.Run;
        if Mutex <> 0 then
            CloseHandle(Mutex);
    end;


And tried to find the window. Again without success, it aways run another instance.

Code: [Select]
    Hwnd:= FindWindow('TApplication', 'RemorDynDNS');
    if Hwnd = 0 then begin Application.Run
    else begin
        if not IsWindowVisible(Hwnd) then begin
            ShowWindow(Hwnd, SW_SHOWNORMAL);
            PostMessage(Hwnd, WM_USER, 0, 0);
        end;
    end;

maybe if i can find the window i can minimize it with hide command and return when running again the application, right?

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Application on background
« Reply #1 on: February 22, 2010, 09:33:31 pm »
Maybe this can help you: Unique Instance.

Another way is to create a simple IPC server: use a client to talk to server.
If there is no response, create the server. Else close the (second instance of the) program.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

jpremor

  • New Member
  • *
  • Posts: 11
Re: Application on background
« Reply #2 on: February 23, 2010, 05:35:03 pm »
 I solved the problem with findwindow:

Code: [Select]
{ KOL MCK } // Do not remove this line!
{$DEFINE KOL_MCK}
{$ifdef FPC} {$mode delphi} {$endif}
program MyTest;

uses
  KOL,
   Princ, windows;


{$IFDEF WINDOWS}{$R MyTest.rc}{$ENDIF}
var
    WindowHandler : THandle;

begin // PROGRAM START HERE -- Please do not remove this comment

// this program should always have this caption
WindowHandler := FindWindow(nil,'MyTestFormCaption');
if WindowHandler > 0 then begin
                  SetForegroundWindow(WindowHandler);
                  Halt;
end;

{$IFNDEF LAZIDE_MCK} {$I MyTest.inc} {$ELSE}

    Application.Initialize;
    Application.Run;

{$ENDIF}

end.


I tried passing some information at the first parameter of findwindow, now just passing the form caption was enough. This code was added to .lpr file. The SetForegroundWindow(WindowHandler); takes the background window to top.


now the event of the X buttons remains a problem. I cannot find a way to get it.

thanks!
« Last Edit: February 23, 2010, 07:11:20 pm by jpremor »

jpremor

  • New Member
  • *
  • Posts: 11
Re: Application on background
« Reply #3 on: February 24, 2010, 11:37:55 am »
I found a solution to X button. Form.SendToBack;
i tried the sendmessage to the windows with WM_COMMAND and IDOK without success. Just the WM_CLOSE execute something with sendmessage, i dont know why.

About the code to not allow multiple instances, take care to use it because this part of the program is automatically generated and from time to time the lazarus take this part inside de else statement ({$IFNDEF LAZIDE_MCK} {$I MyTest.inc} {$ELSE}).

KOL have some includes files to use on initialization but no one is before the form creation, which not allow you to put some code before. I dont understand why "include 1" and "include 2" is in sequence. Maybe include 1 can be before NewFormPrinc.


 

TinyPortal © 2005-2018