using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using SharpDX.Direct3D; using SharpDX.Direct2D1; using Factory2D1 = SharpDX.Direct2D1.Factory; using FactoryType2D1 = SharpDX.Direct2D1.FactoryType; using System.Text; using System.Windows.Forms; using SharpDX.DXGI; using SharpDX; using Colors = SharpDX.Color; using SharpDX.Windows; using System.Globalization; using SharpDX.DirectWrite; namespace PackingPlanVisualizations { public partial class PackingPlan2D : UserControl { WindowRenderTarget wndRender = null; Factory2D1 factory = new Factory2D1(FactoryType2D1.SingleThreaded); SharpDX.DirectWrite.Factory dwFactory = new SharpDX.DirectWrite.Factory(SharpDX.DirectWrite.FactoryType.Shared); RenderTargetProperties rndTargProperties = new RenderTargetProperties(new PixelFormat(Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied)); HwndRenderTargetProperties hwndProperties = new HwndRenderTargetProperties(); #region Packing members BEGIN SolidColorBrush containerBrush; SolidColorBrush containerFillingBrush; SolidColorBrush itemBrush; SolidColorBrush itemFillingBrush; SolidColorBrush labelBrush; TextFormat textFormat; CenteredContainer2D container; #endregion Packing members END //Initialization-Methods public PackingPlan2D() { InitializeComponent(); this.SetStyle( ControlStyles.ResizeRedraw, true); } private void InitializeRenderTarget() { hwndProperties.Hwnd = this.Handle; hwndProperties.PixelSize = new DrawingSize(this.Width, this.Height); hwndProperties.PresentOptions = PresentOptions.None; textFormat = new TextFormat(dwFactory, "arial", 14); wndRender = new WindowRenderTarget(factory, rndTargProperties, hwndProperties); wndRender.AntialiasMode = AntialiasMode.Aliased; containerBrush = new SolidColorBrush(wndRender, Colors.Black); containerFillingBrush = new SolidColorBrush(wndRender, Colors.DarkGray); itemBrush = new SolidColorBrush(wndRender, Colors.Black); itemFillingBrush = new SolidColorBrush(wndRender, new Color4 (Color3.White, 0.5f)); labelBrush = new SolidColorBrush(wndRender, Colors.Black); } //Packing-Methods public void InitializeContainer (float width, float height) { container = new CenteredContainer2D(new Vector2 (this.Width, this.Height), new Vector2 (width, height)); } public void AddItemToContainer(float width, float height, float x, float y, string label) { container.AddItem(new Vector2(width, height), new Vector2 (x, y), label); } //Event-Listeners private void PackingPlan2D_Load(object sender, EventArgs e) { InitializeRenderTarget(); } private void PackingPlan2D_Paint(object sender, PaintEventArgs e) { wndRender.BeginDraw(); var controlColor = Colors.White;//System.Drawing.SystemColors.Control; wndRender.Clear(new Colors (controlColor.R, controlColor.G, controlColor.B)); if (container != null) { container.UpdateContainer(new Vector2(this.Width, this.Height)); wndRender.FillRectangle(container.GetContainerData(), containerFillingBrush); //foreach (PackingPlanVisualizations.CenteredContainer2D.PackingItem item in container.GetPackingItems()) { // SharpDX.RectangleF rect = item.GetRectangle(container.ContainerTopLeft); // wndRender.DrawRectangle(rect, itemBrush, 2f); // wndRender.FillRectangle(rect, itemFillingBrush); // wndRender.DrawText(item.GetLabel(), textFormat, rect, labelBrush); //} foreach (PackingPlanVisualizations.CenteredContainer2D.LabeledRectangle item in container.GetItemRectangles()) { wndRender.DrawRectangle(item.rectangle, itemBrush, 2f); wndRender.FillRectangle(item.rectangle, itemFillingBrush); wndRender.DrawText(item.label, textFormat, item.rectangle, labelBrush); } wndRender.DrawRectangle(container.GetContainerData(), containerBrush, 2.0f); } wndRender.Flush(); wndRender.EndDraw(); } private void PackingPlan2D_Resize(object sender, EventArgs e) { if (wndRender != null) wndRender.Resize(new DrawingSize(this.Width, this.Height)); //this.Refresh(); } private void PackingPlan2D_MouseClick(object sender, MouseEventArgs e) { Console.WriteLine(e.X + " - " + e.Y); } } }