Home » Extra libraries, Code snippets, applications etc. » U++ users applications in progress and useful code snippets, including reference examples! » Just one instance of application running (SingleApp)
|
Re: Just one instance of application running (SingleApp) [message #24609 is a reply to message #24603] |
Mon, 25 January 2010 16:11   |
 |
koldo
Messages: 3432 Registered: August 2008
|
Senior Veteran |
|
|
Sc0rch wrote on Mon, 25 January 2010 14:42 | Header:
#ifndef SINGLE_APP_H
#define SINGLE_APP_H
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
class UniqueWindow : public TopWindow
{
public:
typedef UniqueWindow CLASSNAME;
UniqueWindow(){}
};
inline bool CreateSingleApp(String name, String unique, bool message = true)
{
#ifdef PLATFORM_WIN32
name << unique;
if(::FindWindow(NULL, name))
{
if (message)
Exclamation("Another instance of application already exists!");
return false;
}
Single<UniqueWindow>().SetRect(-1, -1, 1, 1);
Single<UniqueWindow>().Hide();
Single<UniqueWindow>().Title(name);
Single<UniqueWindow>().Open();
#endif
return true;
}
#endif // SINGLE_APP_H
Using:
GUI_APP_MAIN
{
if (!CreateSingleApp("SingleApp Test", "##SingleApp##1.0"))
return;
MainWindow().Run();
}
|
Hello ScOrch
If you use GetWindowIdFromCaption() (SysInfo package) instead of FindWindow, this code will serve you for Linux too.
Sorry for the propaganda 
Best regards
Koldo
Best regards
Iñaki
|
|
|
|
|
Re: Just one instance of application running (SingleApp) [message #24634 is a reply to message #24627] |
Tue, 26 January 2010 05:41   |
Sc0rch
Messages: 99 Registered: February 2008 Location: Russia, Rubtsovsk
|
Member |

|
|
You said:
koldo wrote on Tue, 26 January 2010 03:52 |
Hello Sc0rch
GetWindowIdFromCaption(name, true) tries to find a window with title == name.
GetWindowIdFromCaption(name, false) will get the window handle of the window with a title that just contains name.
If it does not work could you give me more details ?.
Best regards
Koldo
|
I've tested both variants, works well for direct use, like:
TopWindow wnd;
wnd.Title("Test");
wnd.Open();
DUMP(GetWindowIdFromCaption("Test"));
But, not working in this code:
#include <CtrlLib/CtrlLib.h>
#include <SysInfo/SysInfo.h>
using namespace Upp;
class UniqueWindow : public TopWindow
{
public:
UniqueWindow() {}
};
inline bool CreateSingleApp(const String& unique, const String& message)
{
if (GetWindowIdFromCaption(unique, false) >= 0)
{
if (!message.IsEmpty())
Exclamation(message);
return false;
}
Single<UniqueWindow>().Title(unique).SetRect(-1, -1, 1, 1);
Single<UniqueWindow>().Hide();
Single<UniqueWindow>().Open();
return true;
}
GUI_APP_MAIN
{
if (!CreateSingleApp("SingleApp Test##SingleApp##1.0", "Another instance of application "
"already exists!"))
{
return;
}
TopWindow wnd;
wnd.SetRect(Size(200, 100));
wnd.Run();
}
Test, and say, please, maybe it not works only for me.
|
|
|
|
|
|
Goto Forum:
Current Time: Wed Apr 30 03:49:51 CEST 2025
Total time taken to generate the page: 0.04618 seconds
|