VrControls.pas
上传用户:hbszzs
上传日期:2008-08-20
资源大小:628k
文件大小:16k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. {*****************************************************}
  2. {                                                     }
  3. {     Varian Component Workshop                       }
  4. {                                                     }
  5. {     Varian Software NL (c) 1996-2000                }
  6. {     All Rights Reserved                             }
  7. {                                                     }
  8. {*****************************************************}
  9. unit VrControls;
  10. {$I VRLIB.INC}
  11. interface
  12. uses
  13. {$IFDEF VRSHARE}VrShareWin,{$ENDIF}
  14.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  15.   VrConst, VrTypes, VrSysUtils, ExtCtrls;
  16. type
  17.   TGraphicControlCanvas = class(TGraphicControl)
  18.   public
  19.     property Canvas;
  20.   end;
  21.   TCustomControlCanvas = class(TCustomControl)
  22.   public
  23.     property Canvas;
  24.   end;
  25.   TVrComponent = class(TComponent)
  26.   private
  27.     FVersion: TVrVersion;
  28.   public
  29.     constructor Create(AOwner: TComponent); override;
  30.   published
  31.     property Version: TVrVersion read FVersion write FVersion stored false;
  32.   end;
  33.   TVrCustomControl = class(TCustomControl)
  34.   private
  35.     FVersion: TVrVersion;
  36.     FUpdateCount: Integer;
  37.   protected
  38.     function Designing: Boolean;
  39.     function Loading: Boolean;
  40.     procedure ClearClientCanvas;
  41.     procedure UpdateControlCanvas; virtual;
  42.   public
  43.     constructor Create(AOwner: TComponent); override;
  44.     procedure BeginUpdate;
  45.     procedure EndUpdate;
  46.   published
  47.     property Version: TVrVersion read FVersion write FVersion stored false;
  48.   end;
  49.   TVrCustomImageControl = class(TVrCustomControl)
  50.   private
  51.     FBitmapImage: TBitmap;
  52.     function GetBitmapCanvas: TCanvas;
  53.   protected
  54.     DestCanvas: TCanvas;
  55.     procedure ClearBitmapCanvas; virtual;
  56.     procedure Paint; override;
  57.     property BitmapImage: TBitmap read FBitmapImage;
  58.     property BitmapCanvas: TCanvas read GetBitmapCanvas;
  59.   public
  60.     constructor Create(AOwner: TComponent); override;
  61.     destructor Destroy; override;
  62.     procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
  63.   end;
  64.   TVrGraphicControl = class(TGraphicControl)
  65.   private
  66.     FVersion: TVrVersion;
  67.     FUpdateCount: Integer;
  68.   protected
  69.     function Designing: Boolean;
  70.     function Loading: Boolean;
  71.     procedure ClearClientCanvas;
  72.     procedure UpdateControlCanvas; virtual;
  73.     procedure ShowDesignFrame(Dest: TCanvas);
  74.   public
  75.     constructor Create(AOwner: TComponent); override;
  76.     procedure BeginUpdate;
  77.     procedure EndUpdate;
  78.   published
  79.     property Version: TVrVersion read FVersion write FVersion stored false;
  80.   end;
  81.   TVrGraphicImageControl = class(TVrGraphicControl)
  82.   private
  83.     FOverlay: TBitmap;
  84.     FBitmapImage: TBitmap;
  85.     FRefreshOverlay: Boolean;
  86.     FTransparent: Boolean;
  87.     function GetBitmapCanvas: TCanvas;
  88.     procedure SetTransparent(Value: Boolean);
  89.     procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
  90.   protected
  91.     DestCanvas: TCanvas;
  92.     procedure ClearBitmapCanvas; virtual;
  93.     procedure Paint; override;
  94.     procedure CopyParentImage;
  95.     procedure CopyOverlayImage;
  96.     procedure UpdateControlCanvas; override;
  97.     property BitmapImage: TBitmap read FBitmapImage;
  98.     property BitmapCanvas: TCanvas read GetBitmapCanvas;
  99.     property Transparent: Boolean read FTransparent write SetTransparent;
  100.   public
  101.     constructor Create(AOwner: TComponent); override;
  102.     destructor Destroy; override;
  103.     procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
  104.   end;
  105.   TVrHyperLinkControl = class(TVrGraphicImageControl)
  106.   private
  107.     FOnMouseEnter: TNotifyEvent;
  108.     FOnMouseLeave: TNotifyEvent;
  109.     procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
  110.     procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
  111.   protected
  112.     procedure MouseEnter; virtual;
  113.     procedure MouseLeave; virtual;
  114.     property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
  115.     property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;
  116.   end;
  117.   TVrChangeLink = class;
  118.   TVrSharedComponent = class(TVrComponent)
  119.   private
  120.     FClients: TList;
  121.   protected
  122.     procedure NotifyClients;
  123.   public
  124.     constructor Create(AOwner: TComponent); override;
  125.     destructor Destroy; override;
  126.     procedure InsertLink(Value: TVrChangeLink);
  127.     procedure RemoveLink(Value: TVrChangeLink);
  128.   end;
  129.   TVrChangeLink = class(TObject)
  130.   private
  131.     FSender: TVrSharedComponent;
  132.     FOnChange: TNotifyEvent;
  133.   public
  134.     destructor Destroy; override;
  135.     procedure Change; dynamic;
  136.     property OnChange: TNotifyEvent read FOnChange write FOnChange;
  137.     property Sender: TVrSharedComponent read FSender write FSender;
  138.   end;
  139. { Obsolete
  140.   TVrThumbStates = 1..4;
  141.   TVrCustomThumb = class(TVrGraphicImageControl)
  142.   private
  143.     FGlyph: TBitmap;
  144.     FThumbStates: TVrThumbStates;
  145.     FDown: Boolean;
  146.     FHasMouse: Boolean;
  147.     procedure SetGlyph(Value: TBitmap);
  148.     procedure SetThumbStates(Value: TVrThumbStates);
  149.     procedure SetDown(Value: Boolean);
  150.     procedure AdjustBoundsRect; virtual;
  151.     procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
  152.     procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
  153.   protected
  154.     procedure Paint; override;
  155.     procedure LoadFromResourceName(const ResName: string);
  156.     function GetImageIndex: Integer; virtual;
  157.   public
  158.     constructor Create(AOwner: TComponent); override;
  159.     destructor Destroy; override;
  160.     property Glyph: TBitmap read FGlyph write SetGlyph;
  161.     property ThumbStates: TVrThumbStates read FThumbStates write SetThumbStates;
  162.     property Down: Boolean read FDown write SetDown;
  163.     property HasMouse: Boolean read FHasMouse;
  164.   end;}
  165. implementation
  166. { TVrComponent }
  167. constructor TVrComponent.Create(AOwner: TComponent);
  168. begin
  169.   inherited Create(AOwner);
  170.   FVersion := VrLibVersion;
  171. {$IFDEF VRSHARE}
  172.   if not (csDesigning in ComponentState) then
  173.     ShowRegWin;
  174. {$ENDIF}
  175. end;
  176. { TVrCustomControl }
  177. constructor TVrCustomControl.Create(AOwner: TComponent);
  178. begin
  179.   inherited Create(AOwner);
  180.   FVersion := VrLibVersion;
  181. {$IFDEF VRSHARE}
  182.   if not Designing then
  183.     ShowRegWin;
  184. {$ENDIF}
  185. end;
  186. function TVrCustomControl.Designing: Boolean;
  187. begin
  188.   Result := (csDesigning in ComponentState);
  189. end;
  190. function TVrCustomControl.Loading: Boolean;
  191. begin
  192.   Result := (csLoading in ComponentState);
  193. end;
  194. procedure TVrCustomControl.ClearClientCanvas;
  195. begin
  196.   with inherited Canvas do
  197.   begin
  198.     Brush.Style := bsSolid;
  199.     Brush.Color := Self.Color;
  200.     FillRect(ClientRect);
  201.   end;
  202. end;
  203. procedure TVrCustomControl.UpdateControlCanvas;
  204. begin
  205.   if not Loading then
  206.     if FUpdateCount = 0 then Repaint;
  207. end;
  208. procedure TVrCustomControl.BeginUpdate;
  209. begin
  210.   Inc(FUpdateCount);
  211. end;
  212. procedure TVrCustomControl.EndUpdate;
  213. begin
  214.   Dec(FUpdateCount);
  215.   UpdateControlCanvas;
  216. end;
  217. { TVrCustomImageControl }
  218. constructor TVrCustomImageControl.Create(AOwner: TComponent);
  219. begin
  220.   inherited Create(AOwner);
  221.   FBitmapImage := TBitmap.Create;
  222.   DestCanvas := Self.Canvas;
  223. end;
  224. destructor TVrCustomImageControl.Destroy;
  225. begin
  226.   FBitmapImage.Free;
  227.   inherited Destroy;
  228. end;
  229. procedure TVrCustomImageControl.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
  230. begin
  231.   with FBitmapImage do
  232.   begin
  233.     Width := AWidth;
  234.     Height := AHeight;
  235.   end;
  236.   inherited SetBounds(ALeft, ATop, AWidth, AHeight);
  237. end;
  238. procedure TVrCustomImageControl.ClearBitmapCanvas;
  239. begin
  240.   with FBitmapImage do
  241.   begin
  242.     Canvas.Brush.Style := bsSolid;
  243.     Canvas.Brush.Color := Self.Color;
  244.     Canvas.FillRect(Bounds(0, 0, Width, Height));
  245.   end;
  246. end;
  247. procedure TVrCustomImageControl.Paint;
  248. begin
  249.   BitBlt(Canvas.Handle, 0, 0, Width, Height,
  250.     FBitmapImage.Canvas.Handle, 0, 0, SRCCOPY);
  251. end;
  252. function TVrCustomImageControl.GetBitmapCanvas: TCanvas;
  253. begin
  254.   Result := FBitmapImage.Canvas;
  255. end;
  256. { TVrGraphicControl }
  257. constructor TVrGraphicControl.Create(AOwner: TComponent);
  258. begin
  259.   inherited Create(AOwner);
  260.   FVersion := VrLibVersion;
  261. {$IFDEF VRSHARE}
  262.   if not Designing then
  263.     ShowRegWin;
  264. {$ENDIF}
  265. end;
  266. function TVrGraphicControl.Designing: Boolean;
  267. begin
  268.   Result := (csDesigning in ComponentState);
  269. end;
  270. function TVrGraphicControl.Loading: Boolean;
  271. begin
  272.   Result := (csLoading in ComponentState);
  273. end;
  274. procedure TVrGraphicControl.ClearClientCanvas;
  275. begin
  276.   with inherited Canvas do
  277.   begin
  278.     Brush.Style := bsSolid;
  279.     Brush.Color := Self.Color;
  280.     FillRect(ClientRect);
  281.   end;
  282. end;
  283. procedure TVrGraphicControl.ShowDesignFrame(Dest: TCanvas);
  284. begin
  285.   if Designing then
  286.     with Dest do
  287.     begin
  288.       Pen.Style := psDot;
  289.       Brush.Style := bsClear;
  290.       Rectangle(0, 0, Width, Height);
  291.     end;
  292. end;
  293. procedure TVrGraphicControl.UpdateControlCanvas;
  294. begin
  295.   if not Loading then
  296.     if FUpdateCount = 0 then Repaint;
  297. end;
  298. procedure TVrGraphicControl.BeginUpdate;
  299. begin
  300.   Inc(FUpdateCount);
  301. end;
  302. procedure TVrGraphicControl.EndUpdate;
  303. begin
  304.   Dec(FUpdateCount);
  305.   UpdateControlCanvas;
  306. end;
  307. { TVrGraphicImageControl }
  308. constructor TVrGraphicImageControl.Create(AOwner: TComponent);
  309. begin
  310.   inherited Create(AOwner);
  311.   FOverlay := TBitmap.Create;
  312.   FRefreshOverlay := True;
  313.   FBitmapImage := TBitmap.Create;
  314.   FTransparent := false;
  315.   DestCanvas := Self.Canvas;
  316. end;
  317. destructor TVrGraphicImageControl.Destroy;
  318. begin
  319.   FOverlay.Free;
  320.   FBitmapImage.Free;
  321.   inherited Destroy;
  322. end;
  323. procedure TVrGraphicImageControl.WMPaint(var Message: TWMPaint);
  324. begin
  325.   if Message.DC <> 0 then
  326.   begin
  327.     Canvas.Lock;
  328.     BitmapCanvas.Lock;
  329.     try
  330.       Canvas.Handle := Message.DC;
  331.       try
  332.         Paint;
  333.       finally
  334.         Canvas.Handle := 0;
  335.       end;
  336.     finally
  337.       BitmapCanvas.Unlock;
  338.       Canvas.Unlock;
  339.     end;
  340.   end;
  341. end;
  342. procedure TVrGraphicImageControl.UpdateControlCanvas;
  343. begin
  344.   if (not Designing) then
  345.     if (Transparent) and (not FOverlay.Empty) then
  346.     begin
  347.       FRefreshOverlay := false;
  348.       ControlStyle := ControlStyle + [csOpaque];
  349.     end;
  350.   inherited UpdateControlCanvas;
  351. end;
  352. procedure TVrGraphicImageControl.CopyParentImage;
  353. var
  354.   DC: HDC;
  355.   X, Y, W, H: Integer;
  356.   P: TPoint;
  357. begin
  358.   FOverlay.Width := Self.Width;
  359.   FOverlay.Height := Self.Height;
  360.   DC := GetDC(Parent.Handle);
  361.   try
  362.     with Self.Canvas do
  363.     begin
  364.       X := ClipRect.Left;
  365.       Y := ClipRect.Top;
  366.       W := WidthOf(ClipRect);
  367.       H := HeightOf(ClipRect);
  368.       P := Point(X, Y);
  369.       P := Parent.ScreenToClient(ClientToScreen(P));
  370.     end;
  371.     BitBlt(FOverlay.Canvas.Handle, X, Y, W, H, DC, P.X, P.Y, SRCCOPY);
  372.   finally
  373.     ReleaseDC(Parent.Handle, DC);
  374.   end;
  375. end;
  376. procedure TVrGraphicImageControl.CopyOverlayImage;
  377. begin
  378.   BitBlt(BitmapCanvas.Handle, 0, 0, BitmapImage.Width, BitmapImage.Height,
  379.     FOverlay.Canvas.Handle, 0, 0, SRCCOPY);
  380. end;
  381. procedure TVrGraphicImageControl.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
  382. begin
  383.   with FBitmapImage do
  384.   begin
  385.     Width := AWidth;
  386.     Height := AHeight;
  387.   end;
  388.   inherited SetBounds(ALeft, ATop, AWidth, AHeight);
  389. end;
  390. procedure TVrGraphicImageControl.SetTransparent(Value: Boolean);
  391. begin
  392.   if FTransparent <> Value then
  393.   begin
  394.     FTransparent := Value;
  395.     if Value then ControlStyle := ControlStyle - [csOpaque]
  396.     else ControlStyle := ControlStyle + [csOpaque];
  397.     if Designing then Invalidate
  398.     else UpdateControlCanvas;
  399.   end;
  400. end;
  401. procedure TVrGraphicImageControl.ClearBitmapCanvas;
  402. begin
  403.   with FBitmapImage do
  404.   begin
  405.     Canvas.Brush.Style := bsSolid;
  406.     if FTransparent then
  407.     begin
  408.       if FRefreshOverlay then CopyParentImage;
  409.       CopyOverlayImage;
  410.     end
  411.     else
  412.     begin
  413.       Canvas.Brush.Color := Self.Color;
  414.       Canvas.FillRect(Bounds(0, 0, Width, Height));
  415.     end;
  416.   end;
  417. end;
  418. procedure TVrGraphicImageControl.Paint;
  419. begin
  420.   BitBlt(Canvas.Handle, 0, 0, Width, Height,
  421.     BitmapCanvas.Handle, 0, 0, SRCCOPY);
  422.   if Transparent then
  423.   begin
  424.     FRefreshOverlay := True;
  425.     ControlStyle := ControlStyle - [csOpaque];
  426.   end;
  427. end;
  428. function TVrGraphicImageControl.GetBitmapCanvas: TCanvas;
  429. begin
  430.   Result := FBitmapImage.Canvas;
  431. end;
  432. { TVrHyperLinkControl }
  433. procedure TVrHyperLinkControl.MouseEnter;
  434. begin
  435.   if Assigned(FOnMouseEnter) then
  436.     FOnMouseEnter(Self);
  437. end;
  438. procedure TVrHyperLinkControl.MouseLeave;
  439. begin
  440.   if Assigned(FOnMouseLeave) then
  441.     FOnMouseLeave(Self);
  442. end;
  443. procedure TVrHyperLinkControl.CMMouseEnter(var Message: TMessage);
  444. begin
  445.   inherited;
  446.   MouseEnter;
  447. end;
  448. procedure TVrHyperLinkControl.CMMouseLeave(var Message: TMessage);
  449. begin
  450.   inherited;
  451.   MouseLeave;
  452. end;
  453. { TVrSharedComponent }
  454. constructor TVrSharedComponent.Create(AOwner: TComponent);
  455. begin
  456.   inherited Create(AOwner);
  457.   FClients := TList.Create;
  458. end;
  459. destructor TVrSharedComponent.Destroy;
  460. begin
  461.   while FClients.Count > 0 do
  462.     RemoveLink(TVrChangeLink(FClients.Last));
  463.   FClients.Free;
  464.   inherited Destroy;
  465. end;
  466. procedure TVrSharedComponent.NotifyClients;
  467. var
  468.   I: Integer;
  469. begin
  470.   for I := 0 to FClients.Count - 1 do
  471.     TVrChangeLink(FClients[I]).Change;
  472. end;
  473. procedure TVrSharedComponent.InsertLink(Value: TVrChangeLink);
  474. begin
  475.   Value.Sender := Self;
  476.   FClients.Add(Value);
  477. end;
  478. procedure TVrSharedComponent.RemoveLink(Value: TVrChangeLink);
  479. var
  480.   I: Integer;
  481. begin
  482.   I := FClients.IndexOf(Value);
  483.   if I <> -1 then
  484.   begin
  485.     Value.Sender := nil;
  486.     FClients.Delete(I);
  487.   end;
  488. end;
  489. { TVrChangeLink }
  490. destructor TVrChangeLink.Destroy;
  491. begin
  492.   if Sender <> nil then
  493.     Sender.RemoveLink(Self);
  494.   inherited Destroy;
  495. end;
  496. procedure TVrChangeLink.Change;
  497. begin
  498.   if Assigned(FOnChange) then FOnChange(Sender);
  499. end;
  500. { TVrCustomThumb }
  501. {constructor TVrCustomThumb.Create(AOwner: TComponent);
  502. begin
  503.   inherited Create(AOwner);
  504.   ControlStyle := ControlStyle + [csOpaque];
  505.   FGlyph := TBitmap.Create;
  506.   FTransparent := True;
  507.   FThumbStates := 1;
  508. end;
  509. destructor TVrCustomThumb.Destroy;
  510. begin
  511.   FGlyph.Free;
  512.   inherited Destroy;
  513. end;
  514. procedure TVrCustomThumb.SetGlyph(Value: TBitmap);
  515. begin
  516.   FGlyph.Assign(Value);
  517.   AdjustBoundsRect;
  518. end;
  519. procedure TVrCustomThumb.SetThumbStates(Value: TVrThumbStates);
  520. begin
  521.   if FThumbStates <> Value then
  522.   begin
  523.     FThumbStates := Value;
  524.     AdjustBoundsRect;
  525.   end;
  526. end;
  527. procedure TVrCustomThumb.AdjustBoundsRect;
  528. begin
  529.   if not FGlyph.Empty then
  530.     BoundsRect := Bounds(Left, Top, FGlyph.Width div ThumbStates, FGlyph.Height)
  531. end;
  532. procedure TVrCustomThumb.LoadFromResourceName(const ResName: string);
  533. begin
  534.   FGlyph.LoadFromResourceName(hInstance, ResName);
  535.   AdjustBoundsRect;
  536. end;
  537. procedure TVrCustomThumb.Paint;
  538. var
  539.   Index, Offset: Integer;
  540. begin
  541.   ClearBitmapCanvas;
  542.   Index := GetImageIndex;
  543.   if Succ(Index) > ThumbStates then Index := 0;
  544.   if (not FGlyph.Empty) then
  545.   begin
  546.     Offset := Index * Width;
  547.     with BitmapCanvas do
  548.     begin
  549.       Brush.Style := bsClear;
  550.       BrushCopy(ClientRect, FGlyph,
  551.         Bounds(Offset, 0, FGlyph.Width div ThumbStates, FGlyph.Height), clOlive);
  552.     end;
  553.   end;
  554.   inherited Paint;
  555. end;
  556. function TVrCustomThumb.GetImageIndex: Integer;
  557. begin
  558.   Result := 0;
  559. end;
  560. procedure TVrCustomThumb.SetDown(Value: Boolean);
  561. begin
  562.   if FDown <> Value then
  563.   begin
  564.     FDown := Value;
  565.     UpdateControlCanvas;
  566.   end;
  567. end;
  568. procedure TVrCustomThumb.CMMouseEnter(var Message: TMessage);
  569. begin
  570.   inherited;
  571.   FHasMouse := True;
  572.   UpdateControlCanvas;
  573. end;
  574. procedure TVrCustomThumb.CMMouseLeave(var Message: TMessage);
  575. begin
  576.   inherited;
  577.   FHasMouse := false;
  578.   UpdateControlCanvas;
  579. end;}
  580. end.