home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / nccomp / ncform.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-12-22  |  7.8 KB  |  314 lines

  1. unit Ncform;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, NonClt;
  8.  
  9. type
  10.   TNCComponentForm = class(TForm)
  11.   private
  12.     { Private declarations }
  13.     MouseCapture : boolean;
  14.     CaptureButton : integer;
  15.     MinWidth : integer;
  16.     LeftOffSet,
  17.     RightOffSet,
  18.     CaptionHt,
  19.     VerOffSet : integer;
  20.     FNewCaption : TCaption;
  21.     WindowActive : boolean;
  22.     procedure WMNCPaint(var Msg: TMessage); message WM_NCPAINT;
  23.     procedure WMNCActivate(var Msg: TMessage); message WM_NCACTIVATE;
  24.     procedure WMNCLButtonDown(var Msg: TMessage); message WM_NCLBUTTONDOWN;
  25.     procedure WMLButtonUp(var Msg: TMessage); message WM_LBUTTONUP;
  26.     procedure WMNCLButtonDblClk(var Msg: TMessage); message WM_NCLBUTTONDBLCLK;
  27.     procedure WMGetMinMaxInfo(var Msg: TMessage); message WM_GETMINMAXINFO;
  28.     procedure WMMouseMove(var Msg: TMessage); message WM_MOUSEMOVE;
  29.     procedure WMActivate(var Msg: TMessage); message WM_ACTIVATE;
  30.     procedure PaintNCComponents;
  31.     procedure PaintCaption;
  32.     function GetNewCaption : TCaption;
  33.     procedure SetNewCaption(value : TCaption);
  34.   public
  35.     { Public declarations }
  36.     constructor Create(AOwner : TComponent); override;
  37.     destructor Destroy; override;
  38.   published
  39.     property Caption read GetNewCaption write SetNewCaption;
  40.   end;
  41.  
  42. var
  43.   NCComponentForm: TNCComponentForm;
  44.  
  45. implementation
  46.  
  47. {$R *.DFM}
  48. constructor TNCComponentForm.Create(AOwner : TComponent);
  49. var
  50.   i : integer;
  51.   BorderValid : boolean;
  52.   Wth : integer;
  53. begin
  54.   inherited Create(AOwner);
  55.   SetWindowText(Handle,'');
  56.   WindowActive := False;
  57.   BorderValid := (BorderStyle = bsSizeable) or (BorderStyle = bsSingle);
  58.   LeftOffSet := 0;
  59.   RightOffSet := 0;
  60.   MinWidth := 0;
  61.   wth := GetSystemMetrics(sm_cySize);
  62.   CaptionHt := GetSystemMetrics(sm_cyCaption);
  63.   if biSystemMenu in BorderIcons then inc(LeftOffSet,wth);
  64.   if biMinimize in BorderIcons then inc(RightOffSet,wth+1);
  65.   if biMaximize in BorderIcons then inc(RightOffSet,wth+1);
  66.   case BorderStyle of
  67.     bsSizeable    : VerOffSet := GetSystemMetrics(sm_cyFrame);
  68.     bsSingle      : VerOffSet := GetSystemMetrics(sm_cyBorder);
  69.   else
  70.     VerOffSet := 0;
  71.   end;
  72.   inc(LeftOffSet,VerOffSet);
  73.   inc(RightOffSet,VerOffSet);
  74.   for i := 0 to ComponentCount -1 do
  75.   begin
  76.     wth := TNCComponent(Components[i]).Width;
  77.     if Components[i] is TNCComponent then
  78.     with TNCComponent(Components[i]) do
  79.     begin
  80.       if Position = bpLeft then
  81.       begin
  82.         if BorderValid then
  83.         begin
  84.           ParentRegister(Handle,LeftOffSet,VerOffSet,CaptionHt);
  85.           ParentState(WindowActive);
  86.           inc(LeftOffSet,Wth);
  87.           inc(MinWidth,Wth);
  88.         end
  89.         else
  90.           ParentRegister(Handle,-1,VerOffSet,CaptionHt);
  91.       end
  92.       else
  93.       begin
  94.         if BorderValid then
  95.         begin
  96.           inc(RightOffSet,Wth);
  97.           inc(MinWidth,Wth);
  98.           ParentRegister(Handle,RightOffSet,VerOffSet,CaptionHt);
  99.         end
  100.         else
  101.           ParentRegister(Handle,-1,VerOffSet,CaptionHt);
  102.       end;
  103.     end;
  104.   end;
  105.   MouseCapture := False;
  106.   CaptureButton := -1;
  107. end;
  108.  
  109. destructor TNCComponentForm.Destroy;
  110. begin
  111.   inherited Destroy;
  112. end;
  113.  
  114. procedure TNCComponentForm.WMActivate(var Msg: TMessage);
  115. begin
  116.   if Msg.wParam = wa_InActive then WindowActive := False
  117.                               else WindowActive := True;
  118.   inherited;
  119. end;
  120.  
  121. procedure TNCComponentForm.WMNCPaint(var Msg: TMessage);
  122. begin
  123.   inherited;
  124.   PaintNCComponents;
  125. end;
  126.  
  127. procedure TNCComponentForm.WMNCActivate(var Msg: TMessage);
  128. begin
  129.   inherited;
  130.   WindowActive := bool(Msg.wParam);
  131.   PaintNCComponents;
  132. end;
  133.  
  134. procedure TNCComponentForm.PaintNCComponents;
  135. var
  136.   i : integer;
  137. begin
  138.   for i := 0 to ComponentCount -1 do
  139.   begin
  140.     if Components[i] is TNCComponent then
  141.     begin
  142.       TNCComponent(Components[i]).ParentState(WindowActive);
  143.       TNCComponent(Components[i]).RePaint;
  144.     end;
  145.   end;
  146.   PaintCaption;
  147. end;
  148.  
  149. procedure TNCComponentForm.PaintCaption;
  150. var
  151.   Wth : integer;
  152.   TCol,BCol,OldTCol,OldBCol : TColorRef;
  153.   R : TRect;
  154.   WndDC : hDC;
  155.   Buffer : PChar;
  156.   Brush : HBrush;
  157. begin
  158.   if WindowActive then
  159.   begin
  160.     BCol := GetSysColor(Color_ActiveCaption);
  161.     TCol := GetSysColor(Color_CaptionText);
  162.   end
  163.   else
  164.   begin
  165.     BCol := GetSysColor(Color_InActiveCaption);
  166.     TCol := GetSysColor(Color_InActiveCaptionText);
  167.   end;
  168.   GetWindowRect(Handle,R);
  169.   wth := R.Right - R.Left;
  170.   R.Right := wth - RightOffSet - 1;
  171.   R.Left := LeftOffSet + 1;
  172.   R.Top := VerOffset;
  173.   R.Bottom := CaptionHt + 2;
  174.   try
  175.     WndDC := GetWindowDC(Handle);
  176.     GetMem(Buffer,256);
  177.     StrPCopy(Buffer,FNewCaption);
  178.     OldTCol := SetTextColor(WndDC,TCol);
  179.     OldBCol := SetBkColor(WndDC,BCol);
  180.     Brush := CreateSolidBrush(BCol);
  181.     FillRect(WndDC,R,Brush);
  182.     DrawText(WndDC,Buffer,-1,R,DT_SINGLELINE or DT_VCENTER or DT_CENTER);
  183.   finally
  184.     FreeMem(Buffer,256);
  185.     SetTextColor(WndDC,OldTCol);
  186.     SetBkColor(WndDC,OldBCol);
  187.     DeleteObject(Brush);
  188.     ReleaseDC(Handle,WndDC);
  189.   end;
  190. end;
  191.  
  192. procedure TNCComponentForm.WMNCLButtonDown(var Msg: TMessage);
  193. var
  194.   i : integer;
  195.   Found : boolean;
  196.   R : TRect;
  197. begin
  198.   Found := False;
  199.   if (Msg.wParam = HTCAPTION) and (IsIconic(Handle) = False) then
  200.   begin
  201.     i := 0;
  202.     GetWindowRect(Handle,R);
  203.     while (i < ComponentCount) and (Found = False) do
  204.     begin
  205.       if Components[i] is TNCComponent then
  206.         Found := TNCComponent(Components[i]).
  207.                     IsCovered(Msg.LParamLo-R.Left,Msg.LParamHi-R.Top);
  208.       inc(i);
  209.     end;
  210.   end;
  211.   if Found then
  212.   begin
  213.     Dec(i);
  214.     MouseCapture := True;
  215.     CaptureButton := i;
  216.     TNCComponent(Components[i]).LButton(csDown);
  217.     if TNCComponent(Components[i]).DragBy then inherited;
  218.   end
  219.   else
  220.     inherited;
  221. end;
  222.  
  223. procedure TNCComponentForm.WMLButtonUp(var Msg: TMessage);
  224. var
  225.    Hit : longint;
  226.    R : TRect;
  227.    P : TPoint;
  228. begin
  229.   if MouseCapture then
  230.   begin
  231.     MouseCapture := False;
  232.     GetWindowRect(Handle,R);
  233.     P.X := Msg.LParamLo;
  234.     P.Y := Msg.LParamHi;
  235.     P := ClientToScreen(P);
  236.     TNCComponent(Components[CaptureButton]).LButton(csUp)
  237.   end
  238.   else
  239.     inherited;
  240. end;
  241.  
  242. procedure TNCComponentForm.WMNCLButtonDblClk(var Msg: TMessage);
  243. var
  244.   i : integer;
  245.   Found : boolean;
  246.   R : TRect;
  247. begin
  248.   Found := False;
  249.   if (Msg.wParam = HTCAPTION) and (IsIconic(Handle) = False) then
  250.   begin
  251.     i := 0;
  252.     GetWindowRect(Handle,R);
  253.     while (i < ComponentCount) and (Found = False) do
  254.     begin
  255.       if Components[i] is TNCComponent then
  256.         Found := TNCComponent(Components[i]).
  257.                     IsCovered(Msg.LParamLo-R.Left,Msg.LParamHi-R.Top);
  258.       inc(i);
  259.     end;
  260.   end;
  261.   if Found then
  262.   begin
  263.     Dec(i);
  264.     MouseCapture := True;
  265.     CaptureButton := i;
  266.     TNCComponent(Components[i]).LButton(csDown);
  267.     if TNCComponent(Components[i]).DragBy then inherited;
  268.   end
  269.   else
  270.     inherited;
  271. end;
  272.  
  273. procedure TNCComponentForm.WMGetMinMaxInfo(var Msg: TMessage);
  274. var
  275.   Info : ^TMinMaxInfo;
  276. begin
  277.   inherited;
  278.   Info := Pointer(Msg.lParam);
  279.   inc(Info^.ptMinTrackSize.X,MinWidth);
  280. end;
  281.  
  282. procedure TNCComponentForm.WMMouseMove(var Msg: TMessage);
  283. var
  284.   R : TRect;
  285.   P : TPoint;
  286. begin
  287.   if MouseCapture then
  288.   begin
  289.     with TNCComponent(Components[CaptureButton]) do
  290.     begin
  291.       GetWindowRect(Handle,R);
  292.       P.X := Msg.LParamLo;
  293.       P.Y := Msg.LParamHi;
  294.       P := ClientToScreen(P);
  295.       MouseMove(P.X-R.Left,P.Y-R.Top);
  296.     end;
  297.   end
  298.   else
  299.     inherited;
  300. end;
  301.  
  302. procedure TNCComponentForm.SetNewCaption(Value : TCaption);
  303. begin
  304.   FNewCaption := Value;
  305.   PaintCaption;
  306. end;
  307.  
  308. function TNCComponentForm.GetNewCaption : TCaption;
  309. begin
  310.   Result := FNewCaption;
  311. end;
  312.  
  313. end.
  314.