/*
1inst.cpp
Demostrates the use of global atoms and registered messages to pass
command line information to a single application instance.
01 April 1997 by Rich Goldstein, MD
*/
#include <owl\pch.h>
#pragma hdrstop
const
char
*szUniqueAppString =
"TestOneInstance"
;
UINT WM_MY_ACTIVATEAPP = 0
;
class
TMyMainWindow :
public
TDecoratedMDIFrame
{
public
:
TMyMainWindow(const
char
* title, TResId menuResId, TMDIClient& clientWnd,
bool
track, TModule* module =
0
) :
TDecoratedMDIFrame(title, menuResId, clientWnd, track, module)
{}
void
SetupWindow()
{
TDecoratedMDIFrame::SetupWindow();
// Add our unique property
::SetProp(GetHandle(), szUniqueAppString, (HANDLE)1
);
// value must not be zero
}
// This method is responsible for our activation
LRESULT EvNewInstance(WPARAM atom, LPARAM);
DECLARE_RESPONSE_TABLE(TMyMainWindow);
};
DEFINE_RESPONSE_TABLE1(TMyMainWindow, TDecoratedMDIFrame)
EV_REGISTERED(szUniqueAppString, EvNewInstance),
END_RESPONSE_TABLE;
LRESULT TMyMainWindow::EvNewInstance(WPARAM atom, LPARAM)
{
char
buff[
256
];
if
(::GlobalGetAtomName(atom, buff,
256
))
{
// free up the atom space
::GlobalDeleteAtom(atom);
// Process the command line!
TMDIClient* client = GetClientWindow();
if
(client)
{
if
(::IsIconic(GetHandle()))
::SendMessage(GetHandle(), WM_SYSCOMMAND, SC_RESTORE, 0
);
#ifdef __WIN32__
::SetForegroundWindow(GetHandle());
#else
::BringWindowToTop(GetHandle());
#endif
TMDIChild* child = new
TMDIChild(*client, buff);
child->Create();
::PostMessage(client->GetHandle(), WM_MDIACTIVATE, (WPARAM)child->GetHandle(), 0
);
}
}
return
0
;
}
class
TTestApp :
public
TApplication
{
public
:
TTestApp() :
TApplication("OneInstance"
)
{}
void
InitMainWindow()
{
SetMainWindow(new
TMyMainWindow(
"OneInstance"
,
0
, *
new
TMDIClient,
false
));
}
};
int
OwlMain (
int
,
char
* argv[])
{
TTestApp app;
WM_MY_ACTIVATEAPP = ::RegisterWindowMessage(szUniqueAppString);
HWND desktop = ::GetDesktopWindow();
HWND child= ::GetWindow(desktop, GW_CHILD);
while
(child)
{
if
(::GetProp(child, szUniqueAppString))
{
// found a window with the property
// store the current command line as an atom...
ATOM atom = GlobalAddAtom(app.GetCmdLine().c_str());
// send the message to the currently running app
::SendMessage(child, WM_MY_ACTIVATEAPP, atom, 0
);
// don't let this instance continue now
return
0
;
}
child = ::GetWindow(child, GW_HWNDNEXT);
}
// No prior instance found, so proceed.
return
app.Run();
}