using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; namespace HotelPms.Share.Windows.Component.Design { public class DesignItem { public Rectangle[] SmallRect = new Rectangle[8]; public Rectangle[] BoundRect = new Rectangle[4]; public static Size Square = new Size(7, 7); //private StringFormat m_StringFormat = new StringFormat(StringFormatFlags.NoWrap); private DesignPanel.ItemType m_Appearance = DesignPanel.ItemType.Normal; /// /// 外観 /// public DesignPanel.ItemType Appearance { get { return m_Appearance; } set { m_Appearance = value; } } private bool m_DesignMode = false; public bool DesignMode { get { return m_DesignMode; } set { m_DesignMode = value; } } private string m_ImageFile = string.Empty; public string ImageFile { get { return m_ImageFile; } set { m_ImageFile = value; } } private string m_Text = string.Empty; public string Text { get { return m_Text; } set { m_Text = value; } } private float m_LineWidth = 1.0F; public float LineWidth { get { return m_LineWidth; } set { m_LineWidth = value; } } private Font m_Font = SystemFonts.MenuFont; public Font Font { get { return m_Font; } set { m_Font = value; } } public int X1 { get { return m_ClientRectangle.X; } set { m_ClientRectangle = new Rectangle(new Point(value, m_ClientRectangle.Y), m_ClientRectangle.Size); } } public int Y1 { get { return m_ClientRectangle.Y; } set { m_ClientRectangle = new Rectangle(new Point(m_ClientRectangle.X, value), m_ClientRectangle.Size); } } public int X2 { get { return m_ClientRectangle.X + m_ClientRectangle.Width; } set { m_ClientRectangle = new Rectangle(m_ClientRectangle.Location, new Size(value - m_ClientRectangle.X, m_ClientRectangle.Height)); } } public int Y2 { get { return m_ClientRectangle.Y + m_ClientRectangle.Height; } set { m_ClientRectangle = new Rectangle(m_ClientRectangle.Location, new Size(m_ClientRectangle.Width, value - m_ClientRectangle.Y)); } } private Rectangle m_ClientRectangle = new Rectangle(0, 0, 100, 100); public Rectangle ClientRectangle { get { return m_ClientRectangle; } set { m_ClientRectangle = value; } } private Color m_BackColor = Color.White; public Color BackColor { get { return m_BackColor; } set { m_BackColor = value; } } private Color m_BackGradientColor = Color.Empty; public Color BackGradientColor { get { return m_BackGradientColor; } set { m_BackGradientColor = value; } } private LinearGradientMode m_BackGradientMode = LinearGradientMode.Horizontal; public LinearGradientMode BackGradientMode { get { return m_BackGradientMode; } set { m_BackGradientMode = value; } } private Color m_ForeColor = Color.Black; public Color ForeColor { get { return m_ForeColor; } set { m_ForeColor = value; } } private TextFormatFlags m_LineAlignment = TextFormatFlags.VerticalCenter; /// /// 縦 /// public TextFormatFlags LineAlignment { get { return m_LineAlignment; } set { m_LineAlignment = value; } } private TextFormatFlags m_Alignment = TextFormatFlags.Left; /// /// 水平 /// public TextFormatFlags Alignment { get { return m_Alignment; } set { m_Alignment = value; } } public bool EnabledBorder { get; set; } = false; public string Name { get; set; } = string.Empty; public object Tag { get; set; } = null; public void Draw(Graphics g) { FillCell(g); DrawCellImage(g); //画像 DrawCellText(g); //文字 } private void DrawCellImage(Graphics g) { try { if (m_Appearance == DesignPanel.ItemType.Line) { return; } if (m_ImageFile.Length == 0) { return; } if (!File.Exists(m_ImageFile)) { return; } Bitmap img = Image.FromFile(m_ImageFile) as Bitmap; using (ImageAttributes vAttr = new ImageAttributes()) { vAttr.SetColorKey(img.GetPixel(0, 0), img.GetPixel(0, 0)); g.DrawImage(img, m_ClientRectangle, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, vAttr); } } catch { } } private Font GetScaleFont(Font orgFont, Graphics g) { return new Font(orgFont.FontFamily, orgFont.Size * g.Transform.Elements[0], orgFont.Style); } private void DrawCellText(Graphics g) { bool newflag = m_ForeColor.IsSystemColor; Color brushColor = m_ForeColor; if (m_Appearance == DesignPanel.ItemType.Normal) { if (g.Transform.Elements[0] == 1F) { TextRenderer.DrawText(g, m_Text, m_Font, m_ClientRectangle, m_ForeColor, m_Alignment | m_LineAlignment); } else { using (Font newFont = GetScaleFont(m_Font, g)) { float scale = g.Transform.Elements[0]; TextRenderer.DrawText(g, m_Text, newFont, new Rectangle((int)(m_ClientRectangle.X * scale), (int)(m_ClientRectangle.Y * scale), (int)(m_ClientRectangle.Width * scale), (int)(m_ClientRectangle.Height * scale)), m_ForeColor, m_Alignment | m_LineAlignment); } } } else { Pen pen = newflag ? SystemPens.FromSystemColor(brushColor) : new Pen(brushColor); pen.Width = m_LineWidth; SmoothingMode sm = g.SmoothingMode; g.SmoothingMode = SmoothingMode.AntiAlias; g.DrawLine(pen, new Point(X1, Y1), new Point(X2, Y2)); g.SmoothingMode = sm; if (!newflag) { pen.Dispose(); } } } private void FillCell(Graphics g) { if (m_Appearance == DesignPanel.ItemType.Normal) { if (m_BackColor.Equals(Color.Empty) || m_BackColor.Equals(Color.Transparent)) { return; } Color bColorMax = m_BackColor; Color bColorMin = m_BackGradientColor; if (m_BackGradientColor.Equals(Color.Empty)) { bool newflag = bColorMax.IsSystemColor; SolidBrush brush = newflag ? SystemBrushes.FromSystemColor(bColorMax) as SolidBrush : new SolidBrush(bColorMax); g.FillRectangle(brush, m_ClientRectangle); if (!newflag) { brush.Dispose(); } } else { using (LinearGradientBrush lgBrush = new LinearGradientBrush(m_ClientRectangle, bColorMax, bColorMin, m_BackGradientMode)) { g.FillRectangle(lgBrush, m_ClientRectangle); } } if (EnabledBorder) { ControlPaint.DrawVisualStyleBorder(g, m_ClientRectangle); } } //ControlPaint.DrawBorder(g, m_ClientRectangle, // Color.DimGray, 1, ButtonBorderStyle.Solid, //左边 // Color.DimGray, 1, ButtonBorderStyle.Solid, //上边 // Color.White, 1, ButtonBorderStyle.Solid, //右边 // Color.White, 1, ButtonBorderStyle.Solid);//底边 //その部分 if (!m_DesignMode) { return; } DrawDesignFrame(g); //if (isFocus && m_IsMouseDown) //{ //ControlPaint.DrawBorder(e.Graphics, r, // Color.DimGray, 1, ButtonBorderStyle.Solid, //左边 // Color.DimGray, 1, ButtonBorderStyle.Solid, //上边 // Color.White, 1, ButtonBorderStyle.Solid, //右边 // Color.White, 1, ButtonBorderStyle.Solid);//底边 //} } /// /// 6個点 /// /// private Rectangle[] CreateDesignSmallRect() { Rectangle[] SmallRect = new Rectangle[8]; if (m_Appearance == DesignPanel.ItemType.Line && X1 == X2) { #region 縦線 (※X1は線の中心のX) SmallRect[0] = Rectangle.Empty; //上左 SmallRect[1] = new Rectangle(new Point(X1 - Square.Width / 2, Y1 - Square.Height), Square); //上中間 SmallRect[2] = Rectangle.Empty; //上右 SmallRect[3] = Rectangle.Empty; //中間左 SmallRect[4] = Rectangle.Empty; //中間右 SmallRect[5] = Rectangle.Empty; //下左 SmallRect[6] = new Rectangle(new Point(X1 - Square.Width / 2, Y2), Square); //下中間 SmallRect[7] = Rectangle.Empty; //下右 #endregion } else if (m_Appearance == DesignPanel.ItemType.Line && Y1 == Y2) { #region 横線 (※Y1は線の中心のY) SmallRect[0] = Rectangle.Empty; //上左 SmallRect[1] = Rectangle.Empty; //上中間 SmallRect[2] = Rectangle.Empty; //上右 SmallRect[3] = new Rectangle(new Point(X1 - Square.Width, Y1 - Square.Height/ 2), Square); //中間左 SmallRect[4] = new Rectangle(new Point(X2, Y1 - Square.Height / 2), Square); //中間右 SmallRect[5] = Rectangle.Empty; //下左 SmallRect[6] = Rectangle.Empty; //下中間 SmallRect[7] = Rectangle.Empty; //下右 #endregion } else { SmallRect[0] = new Rectangle(new Point(m_ClientRectangle.X - Square.Width, m_ClientRectangle.Y - Square.Height), Square); //上左 SmallRect[1] = new Rectangle(new Point(m_ClientRectangle.X + (m_ClientRectangle.Width - Square.Width) / 2, m_ClientRectangle.Y - Square.Height), Square); //上中間 SmallRect[2] = new Rectangle(new Point(m_ClientRectangle.X + m_ClientRectangle.Width, m_ClientRectangle.Y - Square.Height), Square); //上右 SmallRect[3] = new Rectangle(new Point(m_ClientRectangle.X - Square.Width, m_ClientRectangle.Y + (m_ClientRectangle.Height - Square.Height) / 2), Square); //中間左 SmallRect[4] = new Rectangle(new Point(m_ClientRectangle.X + m_ClientRectangle.Width, m_ClientRectangle.Y + (m_ClientRectangle.Height - Square.Height) / 2), Square); //中間右 SmallRect[5] = new Rectangle(new Point(m_ClientRectangle.X - Square.Width, m_ClientRectangle.Y + m_ClientRectangle.Height), Square); //下左 SmallRect[6] = new Rectangle(new Point(m_ClientRectangle.X + (m_ClientRectangle.Width - Square.Width) / 2, m_ClientRectangle.Y + m_ClientRectangle.Height), Square); //下中間 SmallRect[7] = new Rectangle(new Point(m_ClientRectangle.X + m_ClientRectangle.Width, m_ClientRectangle.Y + m_ClientRectangle.Height), Square); //下右 } return SmallRect; } /// /// 棒 /// /// private Rectangle[] CreateDesignBoundRect() { int adding = 2; Rectangle[] BoundRect = new Rectangle[4]; if (m_Appearance == DesignPanel.ItemType.Line && X1 == X2) { int x = 0; int x1 = 0; int width = 0; if ((int)m_LineWidth < Square.Width) { x = X1 - Square.Width / 2 - Square.Width; width = Square.Width * 3; } else { x = X1 - (int)m_LineWidth / 2 - Square.Width; width = Square.Width * 2 + (int)m_LineWidth; } x1 = x + width - (Square.Width - adding); BoundRect[0] = new Rectangle(x, Y1 - Square.Height, width, Square.Height); //上 BoundRect[1] = new Rectangle(x, Y1, Square.Width - adding, Y2 - Y1);  //左 BoundRect[2] = new Rectangle(x, Y2, width, Square.Height); //下 BoundRect[3] = new Rectangle(x1, Y1, Square.Width - adding, Y2 - Y1); //右 } else if (m_Appearance == DesignPanel.ItemType.Line && Y1 == Y2) { int y = 0; int y1 = 0; int height = 0; if ((int)m_LineWidth < Square.Height) { y = Y1 - Square.Height / 2 - Square.Height; height = Square.Height * 3; } else { y = Y1 - (int)m_LineWidth / 2 - Square.Height; height = Square.Height * 2 + (int)m_LineWidth; } y1 = y + height - (Square.Height - adding); BoundRect[0] = new Rectangle(X1, y, X2 - X1, Square.Height - adding); //上 BoundRect[1] = new Rectangle(X1 - Square.Width, y, Square.Width, height);  //左 BoundRect[2] = new Rectangle(X1, y1, X2 - X1, Square.Height - adding); //下 BoundRect[3] = new Rectangle(X2, y, Square.Width, height); //右 } else { BoundRect[0] = new Rectangle(m_ClientRectangle.X, m_ClientRectangle.Y - Square.Height, m_ClientRectangle.Width, Square.Height - adding); //上 BoundRect[1] = new Rectangle(m_ClientRectangle.X - Square.Width, m_ClientRectangle.Y, Square.Width - adding, m_ClientRectangle.Height);  //左 BoundRect[2] = new Rectangle(m_ClientRectangle.X, m_ClientRectangle.Y + m_ClientRectangle.Height + adding + 1, m_ClientRectangle.Width, Square.Height - adding); //下 BoundRect[3] = new Rectangle(m_ClientRectangle.X + m_ClientRectangle.Width + adding + 1, m_ClientRectangle.Y, Square.Width - adding, m_ClientRectangle.Height); //右 } return BoundRect; } private void DrawDesignFrame(Graphics g) { try { SmallRect = CreateDesignSmallRect(); BoundRect = CreateDesignBoundRect(); g.FillRectangles(Brushes.LightGray, BoundRect); g.FillRectangles(Brushes.White, SmallRect); g.DrawRectangles(Pens.Black, SmallRect); } catch { } } } }