Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Making a component

Posted on 1998-01-15
9
Medium Priority
?
221 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 200 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

636 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