 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Nate Lockwood Guest
|
Posted: Fri Feb 24, 2006 11:03 pm Post subject: PostMessage not being received |
|
|
After determining that there is another instance of the application
running I try to post a message but I'm not receiving it.
String sMessage = "USDA.FS.FireScience.GooleEarthTool.Awake";
WM_MYMSG = RegisterWindowMessage(sMessage.c_str());
and then
void __fastcall TFormMain::WndProc(TMessage& message)
{
if(message.Msg == WM_MYMSG)
FormMain->Show();
else
TForm::WndProc(message); //pass on to parent
}
Is this right? Do I need to implement a message map?
Nate |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Sat Feb 25, 2006 12:03 am Post subject: Re: PostMessage not being received |
|
|
"Nate Lockwood" <RNLockwoodREMOVE (AT) attglobal (DOT) net> wrote in message
news:43ff8a0d$1 (AT) newsgroups (DOT) borland.com...
| Quote: | After determining that there is another instance of the
application running I try to post a message but I'm not
receiving it.
|
I already addressed that issue in the other discussion.
Gambit |
|
| Back to top |
|
 |
Nate Lockwood Guest
|
Posted: Sat Feb 25, 2006 1:03 am Post subject: Re: PostMessage not being received |
|
|
Remy Lebeau (TeamB) wrote:
| Quote: |
I already addressed that issue in the other discussion.
|
Must have passed each other in the aether,
Thanks |
|
| Back to top |
|
 |
JD Guest
|
Posted: Sat Feb 25, 2006 8:03 am Post subject: Re: PostMessage not being received |
|
|
Nate Lockwood <RNLockwoodREMOVE (AT) attglobal (DOT) net> wrote:
| Quote: | After determining that there is another instance of the application
running I try to post a message but I'm not receiving it.
|
I wrote TSingleInstance which is a component that does all of
that for you. Just drop it on the main form or dynamically
allocate it in the main form's ctor.
The only thing that you need to do to make it work is to give
the main form a unique name (because that's how it finds the
first instance). Something like CompanyNameApplicationName:
//-------------------------------------------------------------
#ifndef Unit2H
#define Unit2H
//-------------------------------------------------------------
#include <Classes.hpp>
//-------------------------------------------------------------
class TSingleInstance : public TComponent
{
private:
bool FMinimized,
FMainWindowHooked;
HANDLE hMutex;
HWND FHandle;
HHOOK FMainThreadHook;
UINT SI_RESTORE,
SI_INITIALIZE;
TForm* FParentForm;
protected:
bool __fastcall MainWindowHook( TMessage &Message );
virtual void __fastcall WndProc( TMessage &Message );
static int __stdcall MainThreadWndProcRetHook( int Code, WPARAM WParam, LPARAM LParam );
public:
__fastcall TSingleInstance(TComponent* Owner);
__fastcall ~TSingleInstance();
__published:
};
//-------------------------------------------------------------
#endif
//-------------------------------------------------------------
#include <vcl.h>
#include <basepch.h>
//-------------------------------------------------------------
#pragma hdrstop
//-------------------------------------------------------------
#include "Unit2.h"
#pragma package(smart_init)
//-------------------------------------------------------------
TSingleInstance* ThisSingleInstance = NULL;
HWND hFirstInstance, hSecondInstance;
//-------------------------------------------------------------
BOOL CALLBACK EnumWindowsCallBack( HWND hWnd, LPARAM lParam )
{
char WindowClassName[256] = {0};
if( ::GetClassName(hWnd, WindowClassName, 255) )
{
if( stricmp(WindowClassName, (char*)lParam) == 0)
{
if( hFirstInstance )
{
hSecondInstance = hWnd;
return FALSE;
}
else hFirstInstance = hWnd;
}
}
return TRUE;
}
//-------------------------------------------------------------
__fastcall TSingleInstance::TSingleInstance(TComponent* Owner) : TComponent(Owner)
{
FParentForm = dynamic_cast<TForm*>(Owner);
if( !Owner )
{
throw Exception("TSingleInstance : You must assign an Owner.");
}
if( !FParentForm )
{
throw Exception("TSingleInstance : The Owner must be a TForm.");
}
if( ThisSingleInstance )
{
throw Exception("TSingleInstance : Only one instance of TSingleInstance is allowed per application.");
}
hMutex = NULL;
FHandle = NULL;
FMainThreadHook = NULL;
FMainWindowHooked = false;
ThisSingleInstance = this;
if( !ComponentState.Contains(csDesigning) )
{
char ThisClassName[ 256 ] = { 0 };
SI_RESTORE = ::RegisterWindowMessage( "TSingleInstanceRestoreMessage" );
SI_INITIALIZE = ::RegisterWindowMessage( "TSingleInstanceInitializeMessage" );
if( ! SI_RESTORE || ! SI_INITIALIZE )
{
throw Exception("TSingleInstance : RegisterWindowMessage failed.");
}
::GetClassName( FParentForm->Handle, ThisClassName, 255 );
hMutex = ::CreateMutex( NULL, TRUE, ThisClassName );
if( GetLastError() == ERROR_ALREADY_EXISTS )
{
::ShowWindow( Application->Handle, SW_HIDE );
Application->ShowMainForm = false;
hFirstInstance = hSecondInstance = NULL;
::EnumWindows((WNDENUMPROC)EnumWindowsCallBack, (LPARAM)ThisClassName );
if( hFirstInstance == FParentForm->Handle )
{
hFirstInstance = hSecondInstance;
}
::SendMessage( hFirstInstance, SI_RESTORE, 0, 0 );
::SetForegroundWindow( hFirstInstance );
Application->Terminate();
}
else
{
// Need to monitor main form window state
Application->HookMainWindow( MainWindowHook );
FMainWindowHooked = true;
// To catch messages sent to the HWND found by the above win32 API EnumWindows
FMainThreadHook = ::SetWindowsHookEx( WH_CALLWNDPROCRET, (int (__stdcall*)())MainThreadWndProcRetHook, HInstance, ::GetCurrentThreadId() );
if( !FMainThreadHook )
{
throw Exception("TSingleInstance : Hooking Main Thread failed.");
}
// Still need some final initialization but it needs to be delayed until Application::MainForm is valid.
FHandle = AllocateHWnd( WndProc );
if( !FHandle )
{
throw Exception("TSingleInstance : AllocateHWnd failed.");
}
::PostMessage( FHandle, SI_INITIALIZE, 0, 0 );
}
}
}
//-------------------------------------------------------------
__fastcall TSingleInstance::~TSingleInstance()
{
if( FHandle ) DeallocateHWnd( FHandle );
if( FMainWindowHooked ) Application->UnhookMainWindow( MainWindowHook );
if( FMainThreadHook ) ::UnhookWindowsHookEx( FMainThreadHook );
if( hMutex ) ::CloseHandle( hMutex );
ThisSingleInstance = NULL;
}
//-------------------------------------------------------------
void __fastcall TSingleInstance::WndProc( TMessage &Message )
{
if( Message.Msg == SI_INITIALIZE )
{
if( FParentForm != Application->MainForm )
{
MessageDlg("TSingleInstance : Object can only be placed on the Main Form.", mtError, TMsgDlgButtons() << mbOK, 0);
Application->Terminate();
}
else
{
if( Application->MainForm->WindowState == wsMinimized ) FMinimized = true;
else FMinimized = false;
}
}
Message.Result = DefWindowProc( FHandle, Message.Msg, Message.WParam, Message.LParam );
}
//-------------------------------------------------------------
bool __fastcall TSingleInstance::MainWindowHook( TMessage &Message )
{
if( Message.Msg == WM_SYSCOMMAND )
{
switch( Message.WParam )
{
case SC_RESTORE:
case SC_MAXIMIZE: FMinimized = false; break;
case SC_MINIMIZE: FMinimized = true;
}
}
return false;
}
//-------------------------------------------------------------
int __stdcall TSingleInstance::MainThreadWndProcRetHook( int Code, WPARAM WParam, LPARAM LParam )
{
if( Code == HC_ACTION )
{
CWPRETSTRUCT* cwp = (CWPRETSTRUCT *) LParam;
if( cwp->message == ThisSingleInstance->SI_RESTORE )
{
if( ThisSingleInstance->FMinimized )
{
::SendMessage( Application->Handle, WM_SYSCOMMAND, SC_RESTORE, 0 );
}
}
}
return ::CallNextHookEx( ThisSingleInstance->FMainThreadHook, Code, WParam, LParam );
}
//-------------------------------------------------------------
~ JD |
|
| Back to top |
|
 |
Nate Lockwood Guest
|
Posted: Sat Feb 25, 2006 5:03 pm Post subject: Re: PostMessage not being received |
|
|
Thanks, JD, I'll give it a shot.
Nate |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|