Ниже, как отправить сообщение в существующий процесс, но я не знаю, как сделать основной кадр процесса оставаться на вершине всех других окон. Мне кажется, что это работа диспетчера окон, а wxWidgets не может или трудно или трудно, но вы можете сделать свое окно wxSTAY_ON_TOP
, но при его создании. Таким образом, он либо «всегда сверху», либо минимизирован.
// BUILD: g++ this_file.cpp -Wall $(wx-config --cxxflags --libs net,core,base)
#include <wx/wx.h>
#include <wx/snglinst.h>
#include <wx/ipc.h>
class CConnection : public wxConnection
{
protected:
bool OnExec(const wxString& topic, const wxString& data);
};
class CServer : public wxServer
{
public:
wxConnectionBase *OnAcceptConnection(const wxString& topic) {
return new CConnection;
}
};
class CApp : public wxApp
{
public:
bool OnInit() {
// Check if there is another process running.
if (m_one.IsAnotherRunning()) {
// Create a IPC client and use it to ask the existing process to show itself.
wxClient *client = new wxClient;
wxConnectionBase *conn = client->MakeConnection("localhost", "/tmp/a_socket" /* or a port number */, "a_topic");
conn->Execute("raise");
delete conn;
delete client;
// Don't enter the message loop.
return false;
}
// Create the main frame.
wxFrame *frm = new wxFrame(NULL, wxID_ANY, "There can be only one.");
frm->Show(true);
// Start a IPC server.
m_server = new CServer;
m_server->Create("/tmp/a_socket" /* or the same port number */);
// Enter the message loop.
return this->wxApp::OnInit();
}
int OnExit() {
delete m_server;
return this->wxApp::OnExit();
}
private:
wxSingleInstanceChecker m_one;
CServer *m_server;
};
DECLARE_APP(CApp)
IMPLEMENT_APP(CApp)
bool CConnection::OnExec(const wxString& topic, const wxString& data)
{
if (topic.compare("a_topic") == 0 && data.compare("raise") == 0) {
wxTheApp->GetTopWindow()->Show(true); // This actually won't work.
}
return true;
}