Solved

Making a component

Posted on 1998-01-15
9
214 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Print Graphic and Text to Epson TM-T88v 12 548
JAudiorecorder record freezing the app 29 92
PHP preg_replace code convert to Delphi 14 98
TAction.OnAfterExecute? 2 31
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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