U++ framework
Do not panic. Ask here before giving up.

Home » Developing U++ » U++ Developers corner » Open file in already opened app instead of launching a second copy
Re: Open file in already opened app instead of launching a second copy [message #25973 is a reply to message #25971] Mon, 22 March 2010 20:49 Go to previous messageGo to previous message
koldo is currently offline  koldo
Messages: 3461
Registered: August 2008
Senior Veteran
koldo wrote on Mon, 22 March 2010 19:36

mdelfede wrote on Mon, 22 March 2010 17:39

I wonder how that can be done...
I have a multi-document application; when it's already running I'd like, if double clicking on another document, to have the app loading the second document instead of launching a new app instance.
On Linux I did (some time ago) with my Bazaar Signals package, to tell opened app instance to load the new file before exiting.
On Windows I've no hints about....

Ciao

Max



Hello Massimo

From this post.

First version (Windows only):

#include <CtrlLib/CtrlLib.h>
using namespace Upp;

class UniqueWindow : public TopWindow
{
public:
	typedef UniqueWindow CLASSNAME;
	UniqueWindow() {
		Title("SingleApp Test");
	}
	bool IsSingleApp() {
		if(::FindWindow(NULL, GetTitle().ToString())) 
			return false;
		return true;
	}
};

GUI_APP_MAIN
{
	if (!UniqueWindow().IsSingleApp()) {
		Exclamation("Another instance of application already exists!");
		return;
	}	
	UniqueWindow().Run();
}



Second version (Windows and Linux). It requires SysInfo:

#include <CtrlLib/CtrlLib.h>
#include <SysInfo/SysInfo.h>
using namespace Upp;

class UniqueWindow : public TopWindow
{
public:
	typedef UniqueWindow CLASSNAME;
	UniqueWindow() {
		Title("SingleApp Test");
	}
	bool IsSingleApp() {
		if(GetWindowIdFromCaption(GetTitle().ToString()) > 0) 
			return false;
		return true;
	}
};

GUI_APP_MAIN
{
	if (!UniqueWindow().IsSingleApp()) {
		Exclamation("Another instance of application already exists!");
		return;
	}	
	UniqueWindow().Run();
}


Only change is to add #include <SysInfo/SysInfo.h> and instedad of FindWindow() I have used GetWindowIdFromCaption().

This could be improved using GetWindowsList(), so it is possible to compare our program file name and caption with all running processes.


Best regards
IƱaki
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: libpng1.4
Next Topic: [SOLVED] Xmlize LineEdit with line breaks inside
Goto Forum:
  


Current Time: Sat Sep 12 02:14:02 GMT+2 2026

Total time taken to generate the page: 0.00707 seconds