Board index » delphi » HEEELLLLPP!!! intercepting windows messages....

HEEELLLLPP!!! intercepting windows messages....

I would like to intercept some window messages (normally
managed by the standard WndProc).
I tried using a procedure method, with the "message" directive etc.etc.
The application doesn't execute that code! (I saw it using
a stop in the code).

I tried also overriding the WndProc.
Nothing! The program doesn't pass there.
Why?

Please help me!!
(source code excepts are welcome)

massimo - pisa, italy
*********************

 

Re:HEEELLLLPP!!! intercepting windows messages....


Massimo Magrini <L.Tarabe...@cnuce.cnr.it> wrote in article
<32ABF04B.4...@cnuce.cnr.it>...

Quote
> I would like to intercept some window messages (normally
> managed by the standard WndProc).
> I tried using a procedure method, with the "message" directive etc.etc.
> The application doesn't execute that code! (I saw it using
> a stop in the code).

> I tried also overriding the WndProc.
> Nothing! The program doesn't pass there.
> Why?

> Please help me!!
> (source code excepts are welcome)

Here's a sample component of a data-aware edit field.  It accepts three
custom messages: DB_NEW, DB_SAVE and DB_LOAD from its parent.  Note that I
didn't have to touch WndProc, since I don't want to do anything to the
default handler.

Watch to make sure your component's getting the message:  typically they're
GUI-type messages, so follow the parent-child relationship, not the
owner-child relationship (I've bungled that one up many a time).

Good luck!

sid

type TDataState = (
        dsUnloaded,
        dsLoaded,
        dsLoading,
        dsModified,
        dsSaving,
        dsDeleting
        );

type TDataServer = class (TObject)
        public
                class function ParentDataSource (sender: TWinControl): TChDataSource;
                class function StateAsString (value: TDataState): String;
        end;

type TCdEdit = class (TChEdit)
        private
                fDataSource: TChDataSource;
                fDefaultValue: string;
                fField: string;
                fLoadedValue: string;
                fParentDataSource: boolean;
                fState: TDataState;
                procedure SetState (value: TDataState);
                procedure dbLoad (var Msg: TDBLoad); message DB_LOAD;
                procedure dbSave (var Msg: TDBSave); message DB_SAVE;
                procedure dbNew (var Msg: TDBNew); message DB_NEW;
        protected
                procedure SetDataSource (value: TChDataSource); virtual;
                function GetDataSource: TChDataSource; virtual;
                procedure DataChange (sender: TObject); virtual;
                procedure Load; virtual;
                procedure Save; virtual;
        public
                constructor create (AOwner: TComponent);override;
                property State: TDataState read fState write SetState;
                property LoadedValue:string read fLoadedValue;
        published
                property DataSource: TChDataSource read GetDataSource write SetDataSource
stored true;
                property Field: string read fField write fField stored true;
                property ParentDataSource: boolean read fParentDataSource write
fParentDataSource;
                property DefaultValue: string read fDefaultValue write fDefaultValue
stored true;
end;

implementation

uses BaseDataContainers;

{
        TDataServer

Quote
}

class function TDataServer.ParentDataSource (sender: TWinControl):
TChDataSource;
        begin
                if (sender.Parent = nil) then
                        result := nil
                else if sender.Parent is TCdForm then
                        result := TCdForm(sender.Parent).DataSource
                else if sender.Parent is TCdPanel then
                        result := TCdPanel(sender.Parent).DataSource
                else
                        result := nil
                ;
        end;

class function TDataServer.StateAsString (value: TDataState): String;
        begin
                case value of
                        dsUnloaded: result := 'Empty';
                        dsLoaded:       result := 'Unmodified';
                        dsLoading:  result := 'Loading';
                        dsModified: result := 'Modified';
                        dsSaving:               result := 'Saving';
                else
                        result := 'Unknown';
                end;
        end;

{
                TCdEdit

Quote
}

constructor TCdEdit.create (AOwner: TComponent);
        begin
                inherited;
                ParentDataSource := true;
                OnChange := DataChange;
                fState := dsUnloaded;
        end;

procedure TCdEdit.DataChange (sender: TObject);
        begin
                if ((state in [dsLoaded]) and (text <> LoadedValue)) then begin
                        Parent.perform (DB_MODIFIED, 0, 0);
                        fState := dsModified;
                end;
        end;

procedure TCdEdit.dbLoad (var Msg: TDBLoad);
        begin
                fState := dsLoading;
                load;
                fLoadedValue := text;
                fState := dsLoaded;
        end;

procedure TCdEdit.dbNew (var Msg: TDBNew);
        begin
                fState := dsLoading;
                text := DefaultValue;
                fLoadedValue := text;
                fState := dsLoaded;
        end;

procedure TCdEdit.dbSave (var Msg: TDBSave);
        begin
                fState := dsSaving;
                save;
                fState := dsLoaded;
        end;

function TCdEdit.GetDataSource: TChDataSource;
        begin
                if ParentDataSource then
                        result := TDataServer.ParentDataSource (self)
                else
                        result := fDataSource
        end ;

procedure TCdEdit.Load;
        begin
                if ((DataSource <> nil) and (field <> '')) then
                        text := DataSource.DataSet.FieldByName(field).AsString;
        end;

procedure TCdEdit.Save;
        begin
                if ((DataSource <> nil) and (field <> '')) then
                        DataSource.DataSet.FieldByName(field).AsString := text;
        end;

procedure TCdEdit.SetDataSource (value: TChDataSource);
        begin
                fDataSource := value;
                fParentDataSource := (value = nil)
        end;

procedure TCdEdit.SetState (value: TDataState);
        begin
        fState := value;
  end;

Re:HEEELLLLPP!!! intercepting windows messages....


Perhaps it would help if you said what messages you were trying to
intercept.
I've never had any probs using the message directive. But it can be
confusing knowing where messages are sent to:
It is the delphi Application object which receives all messages I believe.
This then delegates the messages to the appropriate forms and components.
(Delphi also uses custom messages which you will have to get involved with
if you are making a component.)
        You could always use Winsight to make sure that the messages you are
trying to intercept are actually getting sent to your application.

Massimo Magrini <L.Tarabe...@cnuce.cnr.it> wrote in article
<32ABF04B.4...@cnuce.cnr.it>...

Quote
> I would like to intercept some window messages (normally
> managed by the standard WndProc).
> I tried using a procedure method, with the "message" directive etc.etc.
> The application doesn't execute that code! (I saw it using
> a stop in the code).

> I tried also overriding the WndProc.
> Nothing! The program doesn't pass there.
> Why?

> Please help me!!
> (source code excepts are welcome)

> massimo - pisa, italy
> *********************

Re:HEEELLLLPP!!! intercepting windows messages....


Quote
Massimo Magrini wrote:

> I would like to intercept some window messages (normally
> managed by the standard WndProc).

..snip..

To intercept all messages for your application, you can assign the
OnMessage event of the TApplication instance to a procedure.  Before any
message is processed by your application, the assigned procedure will be
called for it.  Check out the Delphi help files with the subject
TApplication, event=OnMessage.  There is a simple example there.

Dave Heinecke
Intel Corporation

Re:HEEELLLLPP!!! intercepting windows messages....


Quote
>  > I would like to intercept some window messages (normally
>  > managed by the standard WndProc).
>  To intercept all messages for your application, you can assign the
>  OnMessage event of the TApplication instance to a procedure.  Before any
>  message is processed by your application, the assigned procedure will be
>  called for it.

I have tried just this for my Delphi 2.01 application that need to respond to messages from a VB16 program.
But it seems as if the messages only get through randomly.
The vb16 programs does an applicationactivate on my Delphi program and then sends F7.
I can see that the activation message is always sent but the F7 keypress is lost in roughtly 1/3 of the times.
When that happens the Delphi app is left in front but with the title bar gray, not responding to keypresses until you click on it with the
mouse.

Please cc any answers to jo...@lindgren.pp.se

thanks

johan

Other Threads