Solved

Making a component

Posted on 1998-01-15
9
210 Views
Last Modified: 2010-04-04
Hi,

I made a program that reacts on a certain message.
I now made a component of it.

Why in the hell (excuse my language) doesn't get this component the message? The source code is the same. In the program, the computer reacts on the message. But if I put this component on a form. It doesn't react on the message!

1) Why ?
2) How can this be fixed?
0
Comment
Question by:ZifNab
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 3

Expert Comment

by:Matvey
ID: 1356765
Well, for now I only think I know why, and It's because the message is sent only to top level windows.

I'll try to find out how to direct it to the component...

Matvey
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1356766
Maybe you can filtrate the messages that are sent to the host app? (Application.onmessage or smth)
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1356767
Coulod you see my new question about BMP?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 3

Expert Comment

by:Matvey
ID: 1356768
Could you see my new question about BMP?
0
 

Expert Comment

by:AK
ID: 1356769
(excuse my language)
I think, main form get this message and must pass it
for your component. I think, components not detting
external messages.
0
 

Accepted Solution

by:
_art_ earned 50 total points
ID: 1356770
Because your message being sent to form not to component. The technique named "subclassing" will help you:

  TNewComp = class(TComponent)
  private
    FNewProc, FDefProc: Pointer;
  protected
    procedure HookWnd;
    procedure UnHookWnd;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure WndProc(var Msg: TMessage); virtual;
  end;

procedure TNewComp.HookWnd;
begin
  FNewProc := MakeObjectInstance(WndProc);
  FDefProc := Pointer(SetWindowLong(TWinControl(Owner).Handle, GWL_WNDPROC, LongInt(FNewProc)));
end;

procedure TNewComp.UnHookWnd;
begin
  if TWinControl(Owner) <> nil then
  begin
    SetWindowLong(TWinControl(Owner).Handle, GWL_WNDPROC, LongInt(FDefProc));
    FreeObjectInstance(FNewProc);
  end;
end;

constructor TNewComp.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  HookWnd;
end;

destructor TNewComp.Destroy;
begin
  UnHookWnd;
  inherited Destroy;
end;

//now you handle owner form's messages...
procedure TNewComp.WndProc(var Msg: TMessage);
begin
  try
    case Msg.msg of
      WM_SYSCOMMAND: ...
      WM_ANYTHING  : ...
    end;

    with Msg do
      Result := CallWindowProc(FDefProc, TWinControl(Owner).Handle, Msg, WParam, LParam);
  except
    Application.HandleException(Self);
  end;
end;

what about 50 points? ;-)
0
 
LVL 8

Author Comment

by:ZifNab
ID: 1356771
Hi _art_,

 Thanks a lot it works!! Can you give me a little bit more information about what your doing in each procedure? And subclassing. Thanks.

About the points, well, I don't know if I'll raise them ....


;-) SURE I'll RAISE THEM, SUCH A GOOD AND BRILLIANT ANSWER (WITH EXAMPLE!) NEEDS MORE POINTS!!
0
 

Expert Comment

by:_art_
ID: 1356772
Well, here is some info from Delphi help about subclassing:

-------------------------------------------------------------
Subclassing Windows controls

See also

Windows has a concept called a window class that is somewhat similar to the object-oriented programming concept of object or class. A window class is a set of information shared between different instances of the same sort of window or control in Windows.
When you create a new kind of control (usually called a custom control) in traditional Windows programming, you define a new window class and register it with Windows. You can also base a new window class on an existing class, which is called subclassing.

In traditional Windows programming, if you wanted to create a custom control, you had to write it in a dynamic-link library (DLL), much like the standard Windows controls, and provide an interface to it.
Using Delphi, you can create a component "wrapper" around any existing Windows class. So if you already have a library of custom controls that you want to use in your Delphi applications, you can create Delphi components that let you use your existing controls and derive new controls from them just as you would any other component.

-------------------------------------------------------------

This is info from API help about SetWindowLong() It's more useful and understandable.

If you use the SetWindowLong function and the GWL_WNDPROC index to replace the window procedure, the window procedure must conform to the guidelines specified in the description of the WindowProc callback function.
Calling SetWindowLong with the GWL_WNDPROC index creates a subclass of the window class used to create the window. An application should not subclass a window created by another process. The SetWindowLong function creates the window subclass by changing the window procedure associated with a particular window, causing Windows to call the new window procedure instead of the previous one. An application must pass any messages not processed by the new window procedure to the previous window procedure by calling CallWindowProc. This allows the application to create a chain of window procedures.
---------------------------------------------------------------

To be short many messages are sent to window that owns your controls and components thus your component simple does not receive them. If you want to hook them you need to subclass (hook) your forms window's procedure. Read help on:


MakeObjectInstance();
SetWindowLong();


to learn more.

0
 
LVL 8

Author Comment

by:ZifNab
ID: 1356773
Thanks _art_.

c.u. Zif.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi cmd execution 6 67
Delphi: ForceDirectory plain function 7 42
Wincontrol not (correctly) drawn 15 42
Browsing a TTreeView in Delphi 5 9
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question