Solved

Making a component

Posted on 1998-01-15
9
200 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
Comment Utility
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
Comment Utility
Maybe you can filtrate the messages that are sent to the host app? (Application.onmessage or smth)
0
 
LVL 3

Expert Comment

by:Matvey
Comment Utility
Coulod you see my new question about BMP?
0
 
LVL 3

Expert Comment

by:Matvey
Comment Utility
Could you see my new question about BMP?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Expert Comment

by:AK
Comment Utility
(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
Comment Utility
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
Comment Utility
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_
Comment Utility
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
Comment Utility
Thanks _art_.

c.u. Zif.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now