Solved

Making a component

Posted on 1998-01-15
9
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 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