How a non-windowed component can receive messages from Windows

Source: Internet
Author: User

Why does it?

Sometimes we need a non-windowed component (i.e. one that isn ' t derived from TWinControl ) to receive Windows messages. To-receive messages the component needs a window handle, but a non-windowed component hasn ' t got one! This article was about what to enable such a component to use a hidden window to receive messages.

How it's Doneexamplemy Clipboard Viewer Component is a non-visual Component that uses the hidden window techniques describ Ed here. The window receives Windows messages that provide information on changes to the Clipboard.

The Delphi library function is AllocateHWnd used to create a hidden window for us and the related DeallocateHWnd disposes of the window whe N we ' ve finished with it.

The Hidden window requires window procedure. AllocateHWnd Enables us to use a method as a window procedure where Windows normally requires a stdcall function. We Pass a reference to the required method to and it takes care of the AllocateHWnd problem of registering the method as a window Procedure for us. Inside the registered method we handle the messages we is interested in and hand the rest off to Windows using DefWindowProc the API call.

Listing 2 below provides a skeleton AllocateHWnd . First though, Listing 1shows A outline definition for our component class:

Type{Our class derived from tcomponentor another ancestor class}Tmyclass = Class (tcomponent)private Fhwnd:hwnd; {field to store the window handle} ...  protected procedure Wndmethod (var msg:tmessage); virtual; {window proc-called by Windows to handle messages passed to our hidden window} ... Public   Constructor Create (aowner:tcomponent); Override; {Create hidden window Here:store handle in Fhwnd} destructor Destroy; Override; {free Hidden window-here} ... end;                  
Listing 1

And here is the implementation details:

Constructor Tmyclass.create (aowner:tcomponent);BegininheritedCreate (Aowner); ...//Create hidden window using Wndmethod as window proc Fhwnd: = Allocatehwnd (Wndmethod);...Enddestructor Tmyclass.destroy;Begin ...//Destroy hidden window Deallocatehwnd (fhwnd);...inheritedDestroy;EndProcedure Tmyclass.wndmethod (var msg:tmessage);var Handled:boolean;BeginAssume we handle message Handled: = True;Case msg.msg of wm_something:dosomething; //Code to handle a message wm_somethingelse:dosomethingelse; //Code to handle another message //Handle other messages here else //We didn ' t handle message Handled: = False; end; if Handled then //We Handled message-record in message result msg.result: = 0 Else //We di DN ' t handle message //pass to DefWindowProc and record result msg.result: = DefWindowProc (Fhwnd, msg.msg, Msg.wparam , Msg.lparam); end;               
Listing 2

Of course, we could just use the Windows APIs to create a window the hard and provide a Windows procedure. But it's more difficult to use a method as a window procedure if we do it the This method. The clever features about is AllocateHWnd (a) It creates the hidden window for us and (b) It allows us-use a method, rat Her than a simple procedure, as the window Procedure–and a method are more useful since it had access to the class ' s PRIV ATE data.

How a non-windowed component can receive messages from Windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.