ホテル管理システム
ogi
yesterday 1a1c8e71fcd14858f595029f089b2d4a00202b32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;
using System.Drawing.Drawing2D;
using Microsoft.Win32;
using System.IO;
 
namespace HotelPms.Share.Windows.Report
{
    /// <summary>                                       
    /// ****************************** Description *******************************                                      
    /// ◇システム名称                                     
    ///  @CoreCom                                       
    /// ◇概要                                     
    ///  施設設定画面                                     
    /// ◇履歴                                     
    ///  2007/11/02 小木 勝龍  新規作成                                     
    /// ****************************** Declarations ******************************                                      
    /// </summary>                                      
    public abstract class PrintGdiPlus : ReportBase,IDisposable
    {
        #region  ★★★★★ Declartions ★★★★★
 
        public static SortedDictionary<string, string> FontFileDict = null;
        public static string SystemFontPath = string.Empty;
 
        public delegate void FetchEventHandler(object sender, FetchEventArgs e);
        public event FetchEventHandler FetchItem = null;
 
        private bool m_Disposed = false;
 
        protected StringFormat m_StringFormat = new StringFormat();
        protected float m_PrintRate = 1F;  //印字倍率        
        
        protected static int A4PaperWidth = 827;
        protected static int A4PaperHeight = 1169;
 
        protected static int B5PaperWidth = 717;
        protected static int B5PaperHeight = 1012;
 
        #endregion
 
        #region  ★★★★★ Property ★★★★★
 
        protected List<Cell> m_Items = new List<Cell>();
        public List<Cell> Items
        {
            get { return m_Items; }
        }
 
        protected PrintDocument m_PrintDoc = new PrintDocument();
 
        public PrintDocument PrintDoc
        {
            get { return m_PrintDoc; }
            set { m_PrintDoc = value; }
        }
 
        /// <summary>
        /// 印字範囲
        /// </summary>
        public float PaperSizeRate { get; set; } = 0.95F;
 
        #endregion
 
        #region  ★★★★★ Class Event ★★★★★
 
        public PrintGdiPlus()
        {
            m_PrintDoc.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
            SystemFontPath = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
            if (FontFileDict == null) { FontFileDict = ReadFontInformation(); }
            m_StringFormat.FormatFlags |= StringFormatFlags.NoWrap;
        }
 
        ~PrintGdiPlus()
        {
            Dispose(false);
        }
 
        protected virtual void Dispose(bool disposing)
        {
            if (!m_Disposed)   //一回だけ
            {
                if (disposing)
                {
                    //Managed Resources
                    if (m_StringFormat != null) { m_StringFormat.Dispose(); m_StringFormat = null; }
                }
 
                //Unmanaged resources
                m_Disposed = true;
            }
        }
 
        public virtual void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
 
        #endregion
 
        #region  ★★★★★ Control Event ★★★★★
        #endregion
 
        #region  ★★★★★ Private Function ★★★★★
        #endregion
 
        #region  ★★★★★ Public  Function ★★★★★
 
        public void Clear()
        {
            m_Items.Clear();
        }
 
        public void Add(Cell cell)
        {
            m_Items.Add(cell);
        }
 
 
 
        public void PrintAll(Graphics g)
        {
            int i = 0;
            int count = m_Items.Count;
            foreach (Cell item in m_Items) 
            {
                if (FetchItem != null) { FetchItem(this, new FetchEventArgs(item, i, count)); }
                DrawCell(g, item);
                i++;
            }
        }
 
        public void DrawBorder(Graphics g, Border border, Cell item)
        {
            if (!border.Visible) { return; }
 
            using (Pen pen = new Pen(Brushes.Black, border.Weight))
            {
                pen.DashStyle = border.DashStyle;
 
                float x1 = 0F;
                float y1 = 0F;
                float x2 = 0F;
                float y2 = 0F;
 
                if (border.Index == Border.TypeIndex.Top)
                {
                    x1 = item.Style.Left - border.Weight * 0.5F;
                    x2 = item.Style.Left + item.Style.Width + border.Weight * 0.5F;
                    y1 = y2 = item.Style.Top;
                }
                else if (border.Index == Border.TypeIndex.Bottom)
                {
                    x1 = item.Style.Left - border.Weight * 0.5F;
                    x2 = item.Style.Left + item.Style.Width + border.Weight * 0.5F;
                    y1 = y2 = item.Style.Top + item.Style.Height;
                }
                else if (border.Index == Border.TypeIndex.Left)
                {
                    x1 = x2 = item.Style.Left;
                    y1 = item.Style.Top;
                    y2 = item.Style.Top + item.Style.Height;
                }
                else if (border.Index == Border.TypeIndex.Right)
                {
                    x1 = x2 = item.Style.Left + item.Style.Width;
                    y1 = item.Style.Top;
                    y2 = item.Style.Top + item.Style.Height;
                }
 
                g.DrawLine(pen, x1, y1, x2, y2);
            }
        }
 
        public void DrawCell(Graphics g, Cell item)
        {
            if (item.CellType == Cell.CellKind.Text || item.CellType == Cell.CellKind.PrintTime || item.CellType == Cell.CellKind.PageNo || item.CellType == Cell.CellKind.TimeAndPage)
            {
                //枠
                foreach (Border border in item.Style.BorderList) { DrawBorder(g, border, item); }
 
                RectangleF r = new RectangleF(item.Style.Left + item.Style.Padding.Left, item.Style.Top + item.Style.Padding.Top, item.Style.Width - item.Style.Padding.Left - item.Style.Padding.Right, item.Style.Height - item.Style.Padding.Top - item.Style.Padding.Bottom);
                m_StringFormat.Alignment = item.Style.Alignment;
                m_StringFormat.LineAlignment = item.Style.LineAlignment;
 
                string text = item.Text;
                if(item.CellType == Cell.CellKind.PrintTime)
                {
                    text = item.Style.Template.Length > 0 ? string.Format(item.Style.Template, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")) : DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                }
                else if (item.CellType == Cell.CellKind.PageNo)
                {
                    text = item.Style.Template.Length > 0 ? string.Format(item.Style.Template, m_PageIndex, m_PageCount) : $"{m_PageIndex} / {m_PageCount.ToString().PadLeft(5, ' ')}";
                }
                else if (item.CellType == Cell.CellKind.TimeAndPage)
                {
                    text = item.Style.Template.Length > 0 ? string.Format(item.Style.Template, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), m_PageIndex, m_PageCount) : $"{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")} {m_PageIndex} / {m_PageCount.ToString().PadLeft(5, ' ')}";
                }
 
                g.DrawString(text, item.Style.Font, Brushes.Black, r, m_StringFormat);
            }
            else if (item.CellType == Cell.CellKind.DotLine || item.CellType == Cell.CellKind.Line)
            {
                using (Pen pen = new Pen(Brushes.Black, 1))
                {
                    pen.DashStyle = (item.CellType == Cell.CellKind.DotLine) ? DashStyle.Dot : DashStyle.Solid;
                    g.DrawLine(pen, item.Style.Left, item.Style.Top, item.Style.Left + item.Style.Width, item.Style.Top + item.Style.Height);
                }
            }
            else if (item.CellType == Cell.CellKind.Image)
            {
                if (System.IO.File.Exists(item.Text))
                {
                    using (System.Drawing.Image img = System.Drawing.Image.FromFile(item.Text))
                    {
                        DrawImage(g, img, item.Style.RectangleF);
                        g.DrawRectangle(Pens.Black, item.Style.RectangleF.X, item.Style.RectangleF.Y, item.Style.RectangleF.Width, item.Style.RectangleF.Height);
                    }
                }
            }
        }
 
        public void Output()
        {            
            m_PrintDoc.Print();
        }
 
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="image"></param>
        /// <param name="rectangle"></param>
        public void DrawImage(Graphics graphics, Image image, RectangleF rectangle)
        {
            if (graphics == null) throw new ArgumentNullException();
            if (rectangle.Width <= 0 || rectangle.Height <= 0) throw new ArgumentOutOfRangeException();
            if (image == null) return;
 
            var l = rectangle.Left;
            var t = rectangle.Top;
            var w = (float)image.Width;
            var h = (float)image.Height;
            var r = h / w;
            w = rectangle.Height / r;
            h = rectangle.Height;
            if (w > rectangle.Width)
            {
                w = rectangle.Width;
                h = rectangle.Width * r;
            }
            l += (rectangle.Width - w) / 2;
            t += (rectangle.Height - h) / 2;
            graphics.DrawImage(image, l, t, w, h);
        }
 
        #endregion
    }
}