本文转自:http://www.neugls.info/?p=97
JVCL
The JEDI Visual Component Library (JVCL) consists of a large collection (currently ca 500) visual and non-visual components which can be instantly reused in your Delphi, Kylix and C++ Builder projects. You can visit this website to know more about JVCL: http://jvcl.delphi-jedi.org/; you can download JVCL here.Introduce
TJvInspector is a component which can let you display properties of an object, it is like delphi's inspect. It may look like this: The control is completely separated from the editing layer and the editing layer is completely separated from the data layer. This means the control is able to "inspect" a wide variety of data types, without the need of creating a new descendant of the inspector control. It also means you can mix various data sources in one view. The control does not handle the painting itself. This is left to a painter object. Each of the properties is called an item, a instance of TJvCustomInspectorItem. TJvCustomInspectorItem is the base inspector item. An item is responsible for drawing and editing the underlying data object, as well as managing the sub items. The base class introduces methods and properties to:- display and edit the data object
- manage child items
- Ordinals
- Enumerations
- Sets
- Strings
- Floats
In addition it provides an item class that functions as a category holder. A category holder also has no data instance to provide values. Categories can be nested, but each item can belong to only one category.
Usage
- How to edit properties of a component?
- How to add a category?
- How to edit a property?
- How to edit a string?
- How to edit font?
- How to edit font name?
- How to edit simple data types?
- How to edit color?
How to edit properties of a component?
By using the AddComponent method of TJvCustomInspector you can edit the properties of the component, like:procedure TSimpleMainForm.FormShow(Sender: TObject); begin JvInspector1.Clear; JvInspector1.AddComponent(Self, 'A Form Inspecting Itself', True); end;You can also do it like this:
procedure TForm1.FormCreate(Sender: TObject); var InspCat: TJvInspectorCustomCategoryItem; begin InspCat:=TJvInspectorCustomCategoryItem.Create(self.JvInspector1.Root,nil); InspCat.DisplayName:='Form1'; TJvInspectorPropData.New(InspCat,Self); end;
How to add a category?
You can also do it like this:procedure TForm1.FormCreate(Sender: TObject); var InspCat: TJvInspectorCustomCategoryItem; begin InspCat:=TJvInspectorCustomCategoryItem.Create(self.JvInspector1.Root,nil); InspCat.DisplayName:='Form1'; TJvInspectorPropData.New(InspCat,Self); end;
How to edit a property?
By using the New method of TJvInspectorPropData, we can edit a property of an object, such as:TJvInspectorPropData.New(InspCat, JvInspector1, GetPropInfo(JvInspector1, PropArray[I, 0])).
Detial usage you can consult the help
How to edit a string?
TJvInspectorVarData.New(InspCat,'DisplayName',TypeInfo(String),@MyString);
How to edit font?
var Form1: TForm1; FColor:Integer=clRed; MyString:string='aa'; Font:TFont; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); var InspCat: TJvInspectorCustomCategoryItem; begin InspCat:=TJvInspectorCustomCategoryItem.Create(self.JvInspector1.Root,nil); InspCat.DisplayName:='Property'; Font:=TFont.Create; TJvInspectorVarData.New(InspCat,'DisplayName',TypeInfo(String),@MyString); TJvInspectorVarData.New(InspCat,'Font',TypeInfo(TFont),@Font); InspCat.Expanded:=True; end;
Note: The 4th parameters of the procedure TJvInspectorVarData.New could not be local variables.
How to edit font name?
MyFontName:string='Tahoma'; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); var InspCat: TJvInspectorCustomCategoryItem; begin InspCat:=TJvInspectorCustomCategoryItem.Create(self.JvInspector1.Root,nil); InspCat.DisplayName:='Property'; TJvInspectorVarData.ItemRegister.Add(TJvInspectorTypeInfoRegItem.Create(TJvInspectorFontNameItem,TypeInfo(String))); TJvInspectorVarData.New(InspCat,'FontName',TypeInfo(String),@MyFontName); InspCat.Expanded:=True; end;
How to edit simple data types?
With TJvInspectorVarData, we can edit Simple data types, such as:TJvInspectorVarData.New(InspCat,'FontName',TypeInfo(String),@MyFontName);TJvInspectorVarData is the inspector data layer that obtains its data from a simple variable or some value on the heap. The class implements all access properties, but the type info will be used to check the legality of the access (ie. a float can't be accessed as Int64).
How to edit color?
JVCL provider us a class called TJvInspectorColorItem which can let'us edit TColor properties. You can use it like this:FColor:TColor implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); var InspCat: TJvInspectorCustomCategoryItem; begin InspCat:=TJvInspectorCustomCategoryItem.Create(self.JvInspector1.Root,nil); InspCat.DisplayName:='Property'; TJvInspectorVarData.ItemRegister.Add(TJvInspectorTypeInfoRegItem.Create(TJvInspectorColorItem,TypeInfo(TColor))); TJvInspectorVarData.New(InspCat,'Color',TypeInfo(TColor),@FColor); InspCat.Expanded:=True; end;
How to make your own item?
In this section, I will show you an example to demonstrate how to make your own item. Following shows the code:(* * Author : Neugls. * Website: Http://www.neugls.info * Email : NeuglsWorkStudio@gmail.com *) unit NuColorInspectItem; interface uses JvInspector,Graphics,Types; type TJvInspectorColorItem = class(TJvCustomInspectorItem) private protected function NameForColor(const Color: TColor): string; procedure PaintValue(const Color: TColor; const ColorName: string;const ACanvas: TCanvas; const ARect: TRect); procedure SetFlags(const Value: TInspectorItemFlags); override; procedure SetRects(const RectKind: TInspectorPaintRect; Value: TRect); override; procedure ButtonClick(Sender: TObject); override; function GetDisplayValue: string; override; procedure SetDisplayValue(const Value: string); override; public constructor Create(const AParent: TJvCustomInspectorItem; const AData: TJvCustomInspectorData); override; procedure BeforeDestruction; override; procedure DrawValue(const ACanvas: TCanvas); override; class procedure RegisterAsDefaultItem; class procedure UnregisterAsDefaultItem; end; implementation uses JvResources,SysUtils,Dialogs,Windows; function Color2RGB(Color:TColor):Integer; begin Result:=0; Result:=Result or((Color and $FF) shl 16); //Result:=Result or(Color shl 16); Result:=Result or(((Color shr 8) and $FF) shl 8); //Result:=Result or(Color and $0000FF00); Result:=Result or(((Color shr 16) and $FF)); end; //=== { TJvInspectorColorItem } ============================================== procedure TJvInspectorColorItem.ButtonClick(Sender: TObject); var ColorDlg:TColorDialog; begin ColorDlg:=TColorDialog.Create(nil); try if ColorDlg.Execute then begin //DisplayValue:=NameForColor(FColor); Data.AsOrdinal:=ColorDlg.Color; end; finally ColorDlg.Free; end; inherited; end; constructor TJvInspectorColorItem.Create(const AParent: TJvCustomInspectorItem; const AData: TJvCustomInspectorData); begin inherited Create(AParent, AData); Flags := [iifVisible, iifEditButton,iifEditFixed]; end; function TJvInspectorColorItem.NameForColor(const Color: TColor): string; var RGBColor:Integer; begin RGBColor:=Color2RGB(Color); Result := '0x'+IntToHex(RGBColor,6); end; procedure TJvInspectorColorItem.PaintValue(const Color: TColor; const ColorName: string; const ACanvas: TCanvas; const ARect: TRect); var TH: Integer; BoxRect: TRect; bc: TColor; pc: TColor; txtRect: TRect; begin TH := Rects[iprValue].Bottom - Rects[iprValue].Top - 2; BoxRect.Left := ARect.Left + (ARect.Bottom - ARect.Top - TH) div 2; BoxRect.Top := ARect.Top + BoxRect.Left - ARect.Left; BoxRect.Right := BoxRect.Left + TH; BoxRect.Bottom := BoxRect.Top + TH; with ACanvas do begin if Color <> clNone then begin bc := Brush.Color; pc := Pen.Color; try Brush.Color := Color; //Pen.Color := BorderColor(bc, Color); Rectangle(BoxRect); finally Pen.Color := pc; Brush.Color := bc; end; end; txtRect := ARect; txtRect.Left := txtRect.Left + (txtRect.Bottom-txtRect.Top)+ 1; TextRect(txtRect, txtRect.Left, BoxRect.Top, ColorName); end; end; procedure TJvInspectorColorItem.SetDisplayValue(const Value: string); begin inherited; OutputDebugString(PWideChar(Value)); end; procedure TJvInspectorColorItem.SetFlags(const Value: TInspectorItemFlags); begin inherited SetFlags(Value + [iifEditButton,iifEditFixed]); end; procedure TJvInspectorColorItem.SetRects(const RectKind: TInspectorPaintRect; Value: TRect); begin if RectKind = iprValue then Value.Left := Value.Left + (Value.Bottom - Value.Top) + 2; inherited SetRects(RectKind, Value); end; procedure TJvInspectorColorItem.BeforeDestruction; begin inherited BeforeDestruction; end; procedure TJvInspectorColorItem.DrawValue(const ACanvas: TCanvas); var Color: TColor; S: string; ARect: TRect; SafeColor: TColor; begin Color := clNone; if Data = nil then S := RsJvInspItemUnInitialized else try if not Data.IsInitialized then S := RsJvInspItemUnInitialized else if not Data.HasValue then S := RsJvInspItemNoValue else if not Data.IsAssigned then S := RsJvInspItemUnassigned else begin S := DisplayValue; Color := Data.AsOrdinal; end; except S := RsJvInspItemValueException + ExceptObject.ClassName + ': ' + Exception(ExceptObject).Message; end; ARect := Rects[iprValueArea]; SafeColor := ACanvas.Brush.Color; if Editing then ACanvas.Brush.Color := clWindow; try ACanvas.FillRect(ARect); PaintValue(Color, S, ACanvas, ARect); if Editing then DrawEditor(ACanvas); finally if Editing then ACanvas.Brush.Color := SafeColor; end; end; function TJvInspectorColorItem.GetDisplayValue: string; begin Result:=NameForColor(Data.AsOrdinal); end; class procedure TJvInspectorColorItem.RegisterAsDefaultItem; begin with TJvCustomInspectorData.ItemRegister do if IndexOf(Self) = -1 then Add(TJvInspectorTypeInfoRegItem.Create(Self, TypeInfo(TColor))); end; class procedure TJvInspectorColorItem.UnregisterAsDefaultItem; begin TJvCustomInspectorData.ItemRegister.Delete(Self); end; end.
Effect image
DownLoad the Unit