unit WPF; {$reference 'PresentationFramework.dll'} {$reference 'WindowsBase.dll'} {$reference 'PresentationCore.dll'} {$apptype windows} uses System.Windows; uses System.Windows.Controls; uses System.Windows.Controls.Primitives; uses System.Windows.Data; uses System.Windows.Media; uses System.Windows.Shapes; uses System.Windows.Markup; type TControl = Control; Panel = System.Windows.Controls.Panel; // Это для других модулей Grid = System.Windows.Controls.Grid; Canvas = System.Windows.Controls.Canvas; StackPanel = System.Windows.Controls.StackPanel; DockPanel = System.Windows.Controls.DockPanel; WrapPanel = System.Windows.Controls.WrapPanel; //TPanel = Panel; // Это для себя //TGrid = Grid; //TCanvas = Canvas; //TStackPanel = StackPanel; //TDockPanel = DockPanel; //TWrapPanel = WrapPanel; // Типы элементов управления Button = System.Windows.Controls.Button; &Label = System.Windows.Controls.Label; TextBlock = System.Windows.Controls.TextBlock; TextBox = System.Windows.Controls.TextBox; CheckBox = System.Windows.Controls.CheckBox; Slider = System.Windows.Controls.Slider; ComboBox = System.Windows.Controls.ComboBox; Border = System.Windows.Controls.Border; Rectangle = System.Windows.Shapes.Rectangle; RadioButton = System.Windows.Controls.RadioButton; //TButton = Button; TLabel = &Label; //TTextBlock = TextBlock; //TTextBox = TextBox; //TCheckBox = CheckBox; //TSlider = Slider; //TComboBox = ComboBox; DrawingVisual = DrawingVisual; Thickness = System.Windows.Thickness; //TThickness = Thickness; FontStyle = FontStyle; FontStyles = FontStyles; GridLength = GridLength; GridUnitType = GridUnitType; Brushes = Brushes; Brush = System.Windows.Media.Brush; Pen = Pen; Point = Point; Colors = Colors; SolidColorBrush = SolidColorBrush; Orientation = Orientation; //TOrientation = Orientation; VerticalAlignment = System.Windows.VerticalAlignment; HorizontalAlignment = System.Windows.HorizontalAlignment; HA = HorizontalAlignment; VA = VerticalAlignment; Dock = Dock; Color = Color; //TColor = Color; TickPlacement = TickPlacement; Binding = Binding; PropertyPath = PropertyPath; EventArgs = System.EventArgs; RoutedEventArgs = RoutedEventArgs; RoutedEventHandler = RoutedEventHandler; EventHandler = RoutedEventHandler; SizeToContent = SizeToContent; FontWeight = FontWeight; FontWeights = FontWeights; WindowStartupLocation = WindowStartupLocation; var Application := new Application; var MainWindow: Window; var Content: UIElement; var CurrentParent: FrameworkElement := nil; type Drawing = class(FrameworkElement) public children: VisualCollection; protected function GetVisualChild(index: integer): Visual; override; begin if (index < 0) or (index >= children.Count) then raise new System.ArgumentOutOfRangeException(); Result := children[index]; end; public constructor := children := new VisualCollection(Self); property VisualChildrenCount: integer read children.Count; override; procedure Add(v: DrawingVisual) := children.Add(v); end; function RGB(r,g,b: byte) := Color.Fromrgb(r, g, b); function ARGB(a,r,g,b: byte) := Color.FromArgb(a, r, g, b); function GrayColor(b: byte): Color := RGB(b, b, b); function RandomColor := RGB(PABCSystem.Random(256), PABCSystem.Random(256), PABCSystem.Random(256)); function EmptyColor: Color := ARGB(0,0,0,0); function Pnt(x,y: real) := new Point(x,y); function Rect(x,y,w,h: real) := new System.Windows.Rect(x,y,w,h); function MainPanel: Panel := MainWindow.Content as Panel; function CreateDrawing := Drawing.Create; function operator implicit(n: integer): Thickness; extensionmethod; begin Result := new Thickness(n); end; function operator implicit(n: real): Thickness; extensionmethod; begin Result := new Thickness(n); end; function operator implicit(a: array of real): Thickness; extensionmethod; begin case a.Length of 1: Result := new Thickness(a[0]); 2: Result := new Thickness(a[0],a[1],0,0); 3: Result := new Thickness(a[0],a[1],a[2],0); 4: Result := new Thickness(a[0],a[1],a[2],a[3]); end; end; function operator implicit(a: array of integer): Thickness; extensionmethod; begin case a.Length of 1: Result := new Thickness(a[0]); 2: Result := new Thickness(a[0],a[1],0,0); 3: Result := new Thickness(a[0],a[1],a[2],0); 4: Result := new Thickness(a[0],a[1],a[2],a[3]); end; end; function operator implicit(name: string): FontFamily; extensionmethod; begin Result := new FontFamily(name); end; procedure SetPosition(Self: Control; Left,Top: real); extensionmethod; begin Canvas.SetLeft(Self,Left); Canvas.SetTop(Self,Top); end; procedure Add(Self: Panel; c: FrameworkElement); extensionmethod := Self.Children.Add(c); procedure Add(Self: Grid; c: FrameworkElement; row,col: integer); extensionmethod; begin Self.Children.Add(c); Grid.SetRow(c,row); Grid.SetColumn(c,col); end; function AddToCell(Self: T; gr: Grid; row,col: integer): T; extensionmethod; where T: FrameworkElement; begin Result := Self; if gr<>nil then gr.Add(Result,row,col); end; function AddTo(Self: T; gr: Panel): T; extensionmethod; where T: FrameworkElement; begin Result := Self; if gr<>nil then gr.Add(Result); end; function AddTo(Self: DrawingVisual; dr: Drawing): DrawingVisual; extensionmethod; begin Result := Self; if dr<>nil then dr.children.Add(Result); end; function GBrush(c: Color): Brush := new SolidColorBrush(c); function GBrush(r,g,b: integer): Brush := new SolidColorBrush(Color.FromRgb(r,g,b)); function GBrush(a,r,g,b: integer): Brush := new SolidColorBrush(Color.FromARgb(a,r,g,b)); function GPen(c: Color; width: real := 1) := new Pen(GBrush(c),width); var CurrentPen := GPen(Colors.Black,1); var CurrentBrush := GBrush(Colors.White); // Декораторы function &With(Self: Grid; ShowGridLines: boolean := True): Grid; extensionmethod; begin Self.ShowGridLines := ShowGridLines; Result := Self; end; function &With(Self: T; Margin: Thickness := 0; HA: HorizontalAlignment := WPF.HA.Stretch; VA: VerticalAlignment := WPF.VA.Stretch ): T; extensionmethod; where T: FrameworkElement; begin Self.Margin := Margin; Self.HorizontalAlignment := HA; Self.VerticalAlignment := VA; Result := Self; end; function &With(Self: T; Background: Brush := nil; Color: Color := Colors.White): T; extensionmethod; where T: Panel; begin if Background <> nil then Self.Background := Background else Self.Background := GBrush(color); Result := Self; end; {function &With(Self: Canvas; Background: Brush := nil; Color: TColor := Colors.White): Canvas; extensionmethod; begin if Background <> nil then Self.Background := Background else Self.Background := GBrush(color); Result := Self; end;} function AsMainContent(Self: T): T; extensionmethod; where T: FrameworkElement; begin MainWindow.Content := Self; Result := Self; CurrentParent := Self; end; procedure Render(Self: DrawingVisual; draw: DrawingContext -> ()); extensionmethod; begin var dc := Self.RenderOpen(); draw(dc); dc.Close; end; function CreateBinding(Source: FrameworkElement; PropertyName: string; Mode: BindingMode := BindingMode.Default): Binding; begin var bind := new Binding(); bind.Source := Source; bind.Path := new PropertyPath(PropertyName); bind.Mode := Mode; Result := bind; end; function SetBinding(Self: FrameworkElement; dp: DependencyProperty; dest: FrameworkElement; PropertyName: string; Mode: BindingMode := BindingMode.Default): BindingExpressionBase; extensionmethod := Self.SetBinding(dp,CreateBinding(dest,PropertyName,Mode)); // Методы расширения панелей procedure DockPanel.Add(c: FrameworkElement; dock: WPF.Dock); begin Self.Add(c); DockPanel.SetDock(c,dock); end; {procedure StackPanel.Add(c: FrameworkElement); begin Self.Children.Add(c); end;} procedure Panel.AddElements(params aa: array of FrameworkElement); begin foreach var c in aa do Self.Children.Add(c); end; procedure Panel.AddElements(elements: sequence of FrameworkElement; Width: real := real.NaN; Height: real := real.NaN; Padding: Thickness := -1; Margin: Thickness := -1); begin foreach var b in elements do begin if Margin <> -1 then b.Margin := Margin; if Padding <> -1 then begin var prop := b.GetType.GetProperty('Padding'); if prop <> nil then prop.SetValue(b,Padding); end; if not real.IsNaN(Width) then b.Width := Width; if not real.IsNaN(Height) then b.Height := Height; Self.Add(b); end; end; function Elements(params cc: array of FrameworkElement) := Lst(cc); function Elements(cc: sequence of FrameworkElement) := Lst(cc); function Text(Self: Button): string; extensionmethod := Self.Content as string; function Text(Self: &Label): string; extensionmethod := Self.Content as string; function Text(Self: CheckBox): string; extensionmethod := Self.Content as string; function AddTo(Self: T; gr: DockPanel; dock: WPF.Dock): T; extensionmethod; where T: FrameworkElement; begin Result := Self; if gr<>nil then gr.Add(Result,dock); end; function Init(Self: FrameworkElement; Width: real := real.NaN; Height: real := real.NaN; Margin: Thickness := -1): FrameworkElement; extensionmethod; begin if not real.IsNaN(Width) then Self.Width := Width; if not real.IsNaN(Height) then Self.Height := Height; if Margin <> -1 then Self.Margin := Margin; Result := Self; end; function ParseXaml(s: string): object; begin var context := new ParserContext(); context.XmlnsDictionary.Add('','http://schemas.microsoft.com/winfx/2006/xaml/presentation'); context.XmlnsDictionary.Add('x', 'http://schemas.microsoft.com/winfx/2006/xaml'); Result := System.Windows.Markup.XamlReader.Parse(s,context); end; function LoadFromXaml(fname: string): object; begin var s := ReadAllText(fname); Result := ParseXaml(s); end; //-----------------------------------------------------------------------// // Функции создания элементов управления //-----------------------------------------------------------------------// function CreateElement(Width: real := real.NaN; Height: real := real.NaN; Margin: Thickness := -1): T; where T: FrameworkElement, constructor; begin Result := new T; Result.Init(Width,Height,Margin); end; /// Элемент управления Кнопка. Основное событие - Click function CreateButton(Text: string; Width: real := real.NaN; Height: real := real.NaN; Margin: Thickness := -1; Padding: Thickness := -1): Button; begin Result := CreateElement&