[DUG] Component creation

Gary T. Benner gary at 123.co.nz
Tue Sep 5 17:19:50 NZST 2006


[Reply]

HI Robert and others.

Try this code - it is a TEdit descendent that changes it's size when it has focus, and returns to normal when the mouse leaves and passes over it's parent control.

This works assuming that the control does not occupy the client area of it's parent.

Anyone have a better idea .... Max, Paul, I thought you guys ate this stuff for breakfast!!


HTH

cheers

Gary

unit Edit1;

interface

uses
  Windows,Forms,Messages,SysUtils, Classes, Controls, StdCtrls, Graphics;

type
  TEdit1 = class(TEdit)
  private
    { Private declarations }
    parentForm : TControl;
    FFormHandle: Hwnd;
    FWndHandlerPtr: Pointer;
    FOldWndHandler: Pointer;
    FNormalSize: integer;
    FSizeFocused: integer;

    procedure MyExit(var Message: TCMExit); message CM_EXIT;
    procedure MyEnter(var Message: TCMEnter); message CM_ENTER;
    procedure MyMousedown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
    procedure MyWndProc(var Messg: TMessage);
    procedure SetNormalSize(const Value: integer);
    procedure SetFocusedSize(const Value: integer);


  protected
    { Protected declarations }
    procedure Loaded; override;
  public
    { Public declarations }
    constructor create(Aowner:Tcomponent); override;
    destructor destroy; override;
  published
    { Published declarations }
    property SizeNormal : integer read FNormalSize write SetNormalSize;
    property SizeFocused : integer read FSizeFocused write SetFocusedSize;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Gary', [TEdit1]);
end;

{ TEdit1 }


constructor TEdit1.create(Aowner: Tcomponent);
begin
  inherited;
//

end;

procedure TEdit1.Loaded;
begin
  if FNormalSize=0 then
    FNormalSize:=width else
    width:=FNormalSize;
  if FSizeFocused=0 then
    FSizeFocused:=width;
      parentForm:=TWinControl(self.parent);
  FFormHandle := TWinControl(parentForm).Handle;
  FOldWndHandler := Pointer(GetWindowLong(FFormHandle, GWL_WNDPROC));
  FWndHandlerPtr:= MakeObjectInstance(MyWndProc);
  if FWndHandlerPtr = nil then
      raise EOutOfResources.Create('Windows Resources exhausted');
  SetWindowLong(FFormHandle, GWL_WNDPROC, Longint(FWndHandlerPtr));
end;


destructor TEdit1.destroy;
begin
  SetWindowLong(FFormHandle, gwl_WndProc, Longint(FOldWndHandler));
  if FWndHandlerPtr <> nil then
    FreeObjectInstance(FWndHandlerPtr);
  inherited Destroy;
end;

procedure TEdit1.MyEnter(var Message: TCMEnter);
begin
  width:=FSizeFocused;
end;

procedure TEdit1.MyExit(var Message: TCMExit);
begin
  ReleaseCapture;
  width:=FNormalSize;
end;

procedure TEdit1.MyMousedown(var Message: TWMLButtonDown);
begin
  setcapture(parent.Handle);
  inherited;
end;


procedure TEdit1.MyWndProc(var Messg: TMessage);
begin
  with Messg do begin
    case Msg of
      WM_MOUSEMOVE:
        if (not (csDesigning in ComponentState)) then begin
         if width<>FNormalSize then
           width:=FNormalSize;
         end;
    end;
    Result := CallWindowProc(FOldWndHandler, FFormHandle, Msg, wParam, lParam);
  end;
end;


procedure TEdit1.SetNormalSize(const Value: integer);
begin
  FNormalSize := Value;
end;

procedure TEdit1.SetFocusedSize(const Value: integer);
begin
  FSizeFocused := Value;
end;

end.





At 13:30 on 5/09/2006 you wrote 
>To  : delphi at ns3.123.co.nz
>CC  : >From: Robert martin, rob at wildsoft.co.nz
>Content Type: text/plain
>Attached: >
>Hi Todd
>
>I would love to do this but the WM_LBUTTONDOWN message only triggers >when clicking in my component (not outside) so It is pretty useless.  I >know I could override the forms events and do something but as this is a >component (to be used in many forms / applications) that isn't feasible.
>
>The frustrating thing is that TComboBox (and lots of other combos) >already do this.  If the combo is dropped down, clicking on the form >closes the drop down list.  I just cant see code that does it !
>
>
>Rob Martin
>Software Engineer
>
>phone +64 03 377 0495
>fax   +64 03 377 0496
>web www.chreos.com
>
>Wild Software Ltd
>
>
>
>Todd Martin wrote:
>> Hi Wayne
>>
>> The general idea is to trap the MouseDown event on your own component >> and check the position of the mouse,
>> Control.ScreenToClient(Mouse.CursorPos),
>> when you do. If the cursor is outside the boundary of your control, >> perform the clean up operation.
>>
>> If the user clicks on a focusable component, the focus shifts to that >> object and the MouseDown event is not fired, so instead you perform >> your clean up in your component's OnExit event.
>>
>> Todd.
>>
>> ----- Original Message ----- From: "Wayne Roser" <rr at kristin.school.nz>
>> To: "NZ Borland Developers Group - Delphi List" <delphi at ns3.123.co.nz>
>> Cc: <delphi at ns3.123.co.nz>
>> Sent: Tuesday, September 05, 2006 12:05 PM
>> Subject: Re: Re: [DUG] Component creation
>>
>>
>>> Hi Gary
>>> 1. Am I correct in thinking that I would need to trap mousedown on >>> every non-focusable object and
>>> call MyMouseDown for each TEdit1 that might have focus?
>>> 2. Is this the usual way to pass on the message. (I called the >>> component a TFocusEdit)
>>>
>>> procedure TForm1.Panel1MouseDown(Sender: TObject; Button: >>> TMouseButton; Shift: TShiftState; X, Y:
>>> Integer);
>>> var
>>>  AMessage: TWMLButtonDown;
>>> begin
>>>  FocusEdit1.MyMousedown(AMessage);
>>>  //All other FocusEdits and FocusCombos etc get a MyMousedown here; >>> recursion?
>>> end;
>>>
>>> Wayne
>>>
>>> NZ Borland Developers Group - Delphi List <delphi at ns3.123.co.nz> on >>> Tuesday, 5 September 2006 at
>>> 11:37 a.m. +0000 wrote:
>>>> [Reply]
>>>>
>>>> HI Rob et all,
>>>>
>>>> Here's a mod to the previous code that makes the change when you >>>> click on the form.
>>>>
>>>> Note that when you click off the component on to a non-focusable >>>> object, the original component
>>>> still has focus, so you can trap the mousedown event and go from there.
>>>>
>>>> If you click on a focusable component then focus leaves and you can >>>> trap the onexit event.
>>>>
>>>> HTH
>>>>
>>>> cheers
>>>>
>>>> Gary
>>>>
>>>> unit Edit1;
>>>>
>>>> interface
>>>>
>>>> uses
>>>>
>>>> Messages,SysUtils, Classes, Controls, StdCtrls, Graphics;
>>>>
>>>> type
>>>>
>>>> TEdit1 = class(TEdit)
>>>>
>>>> private
>>>>
>>>> { Private declarations }
>>>>
>>>> procedure MyExit(var Message: TCMExit); message CM_EXIT;
>>>>
>>>> procedure MyEnter(var Message: TCMEnter); message CM_ENTER;
>>>>
>>>> procedure MyMousedown(var Message: TWMLButtonDown); message >>>> WM_LBUTTONDOWN;
>>>>
>>>> protected
>>>>
>>>> { Protected declarations }
>>>>
>>>> public
>>>>
>>>> { Public declarations }
>>>>
>>>> published
>>>>
>>>> { Published declarations }
>>>>
>>>> end;
>>>>
>>>> procedure Register;
>>>>
>>>> implementation
>>>>
>>>> procedure Register;
>>>>
>>>> begin
>>>>
>>>> RegisterComponents('GCS', [TEdit1]);
>>>>
>>>> end;
>>>>
>>>> { TEdit1 }
>>>>
>>>> procedure TEdit1.MyEnter(var Message: TCMEnter);
>>>>
>>>> begin
>>>>
>>>> color:=clYellow;
>>>>
>>>> end;
>>>>
>>>> procedure TEdit1.MyExit(var Message: TCMExit);
>>>>
>>>> begin
>>>>
>>>> //
>>>>
>>>> color:=clWindow;
>>>>
>>>> end;
>>>>
>>>> procedure TEdit1.MyMousedown(var Message: TWMLButtonDown);
>>>>
>>>> begin
>>>>
>>>> color:=clWindow;
>>>>
>>>> inherited;
>>>>
>>>> end;
>>>>
>>>> end.
>>>>
>>>> At 10:55 on 5/09/2006 you wrote
>>>>
>>>>> To : delphi at ns3.123.co.nz
>>>>
>>>>> CC :
>>>>
>>>>> From: Robert martin, rob at wildsoft.co.nz
>>>>
>>>>> Content Type: text/plain
>>>>
>>>>> Attached:
>>>>
>>>>>
>>>>
>>>>> Thought I had tried it but my implementation mat have been wrong, will
>>>>
>>>>> have another crack :)
>>>>
>>>>>
>>>>
>>>>> Rob Martin
>>>>
>>>>> Software Engineer
>>>>
>>>>>
>>>>
>>>>> phone +64 03 377 0495
>>>>
>>>>> fax +64 03 377 0496
>>>>
>>>>> web www.chreos.com
>>>>
>>>>>
>>>>
>>>>> Wild Software Ltd
>>>>
>>>>>
>>>>
>>>>>
>>>>
>>>>>
>>>>
>>>>> James Sugrue wrote:
>>>>
>>>>>> WM_KILLFOCUS?
>>>>
>>>>>>
>>>>
>>>>>> Never used it, but apparently it can be a bit dodgy.
>>>>
>>>>>>
>>>>
>>>>>> -----Original Message----- >>>>
>>>>>> From: delphi-bounces at ns3.123.co.nz >>>>>> [mailto:delphi-bounces at ns3.123.co.nz] On
>>>>
>>>>>> Behalf Of Robert martin
>>>>
>>>>>> Sent: Tuesday, September 05, 2006 10:11 AM
>>>>
>>>>>> To: NZ Borland Developers Group - Delphi List
>>>>
>>>>>> Subject: [DUG] Component creation
>>>>
>>>>>>
>>>>
>>>>>> Hi
>>>>
>>>>>>
>>>>
>>>>>> I have a custom component that expands to show data. When focus >>>>>> changes
>>>>
>>>>>> it reverts back to its shrunken state. However I also want it to >>>>>> revert
>>>>
>>>>>> to small if the user clicks the mouse anywhere outside of the >>>>>> component
>>>>
>>>>>> (the way that TComboBox does if it is dropped down).
>>>>
>>>>>>
>>>>
>>>>>> I assume I need to trap a windows message to handle this but I >>>>>> just cant
>>>>
>>>>>> see which.
>>>>
>>>>>>
>>>>
>>>>>> Any suggestions ?
>>>>
>>>>>>
>>>>
>>>>>>
>>>>
>>>>> _______________________________________________
>>>>
>>>>> Delphi mailing list
>>>>
>>>>> Delphi at ns3.123.co.nz
>>>>
>>>>> http://ns3.123.co.nz/mailman/listinfo/delphi
>>>>
>>>>>
>>>>
>>>>
>>>>
>>>> ---------------------------------------------------------------------------------------------------- >>>>
>>>>
>>>> Gary Benner
>>>> e-Engineer, Lecturer, and Software Developer
>>>>
>>>> [ http://www.123.co.nz ]123 Internet Limited
>>>> [ http://www.waiariki.ac.nz ]Waiariki Institute of Technology
>>>> [ http://www.sunshinebags.co.nz ]Sunshine Garden Bag Co.
>>>> [ http://www.sommnet.com ]Sommnet.com Limited
>>>> Mob: 021 966 992
>>>> Email: [ mailto:gary at 123.co.nz ]gary at 123.co.nz
>>>>
>>>>
>>>>
>>>> Ref#: 41006
>>>>
>>>> _______________________________________________
>>>> Delphi mailing list
>>>> Delphi at ns3.123.co.nz
>>>> http://ns3.123.co.nz/mailman/listinfo/delphi
>>>
>>>
>>> _______________________________________________
>>> Delphi mailing list
>>> Delphi at ns3.123.co.nz
>>> http://ns3.123.co.nz/mailman/listinfo/delphi
>>>
>>>
>>>
>>> -- >>> No virus found in this incoming message.
>>> Checked by AVG Free Edition.
>>> Version: 7.1.405 / Virus Database: 268.11.7/432 - Release Date: >>> 29/08/2006
>>>
>>>
>>
>>
>>
>_______________________________________________
>Delphi mailing list
>Delphi at ns3.123.co.nz
>http://ns3.123.co.nz/mailman/listinfo/delphi
>

Ref#: 41006

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://ns3.123.co.nz/pipermail/delphi/attachments/20060905/05813188/attachment-0001.html


More information about the Delphi mailing list