|
In an application I am developing, I had a class inherited from WFrame. I could capture most key-stokes in the KeyDown Event, but there was always an annoying beep. Tracing it down, I noticed that it was always receiving a WM_GETDLGCODE code message before the key-stroke.
The Win32 SDK docs indicate:
"The WM_GETDLGCODE message is sent to the dialog box procedure associated with a control. Normally, Windows handles all arrow-key and TAB-key input to the control. By responding to the WM_GETDLGCODE message, an application can take control of a particular type of input and process the input itself."
Thus to handle this in your control in Power++, use the following:
virtual WBool MyFrame::ProcessMessage( const WMessage & msg, WLong
& returns )
{
if( msg.msg == WM_GETDLGCODE ) {
returns = ( DLGC_WANTARROWS // Direction keys.
| DLGC_WANTCHARS // WM_CHAR messages.
| DLGC_WANTTAB ) ; // TAB key. ;
return FALSE;
} else {
// note that my class is inherited from WFrame
return WFrame::ProcessMessage( msg, returns );
};
}
Note that this solution requires use of Win32 SDK. See the Win32 Documentation on WM_GETDLGCODE to determine what keys you want to handle. Now your KeyDown Event can do whatever you want it to without interruptions.
Several ideas and utilities have been posted regarding the problem of preventing an application from executing when another instance of that application is already running.
The method I have been using, is to create an event semaphore with a name unique to this application. I then Wait on that semaphore for zero seconds. If it returns FALSE, then the semaphore has never been "set" (this is the first instance of the application) and I set it. If it returns TRUE, then it has already been set and I end the application.
The code can be placed in the Main Form Create event or in the Application StartHandler as I have done here.
NOTE: Make sure that the string "_APPNAME_" is unique for every application you create.
WBool ApplicationClass::StartHandler( WObject *, WStartEventData
*event )
{
WEventSemaphore * sem;
WBool semWaitCode;
WMessageBox msg_1;
sem = new WEventSemaphore("_APPNAME_"); // Set the unique
string.
semWaitCode = sem->Wait(0);
if (semWaitCode == FALSE) { // Semaphore is not
signaled
sem->Set();
}
else {
msg_1.Message( NULL, WMBLevelInfo, WMBButtonOk, "Application
Start", "Application is already running"
);
}
event->exitCode = semWaitCode ? -1 : 0; // Set to 0 to continue.
event->abortRun = semWaitCode; // TRUE if this is not instance
#1
return semWaitCode; // TRUE will end the app.
}