.Net FrameWorkのPanelのみ印刷されるが、Panel上のコントロールが印刷されない

C#

12using System;3using System;4using System.Collections.Generic;5using System.ComponentModel;6using System.Data;7using System.Drawing;8using System.Drawing.Text;9using System.Linq;10using System.Text;11using System.Threading.Tasks;12using System.Windows.Forms;13using System.Diagnostics;14using System.Drawing.Printing;15using gv = AccountingBooks.GlobalVariablesA;16 17namespace AccountingBooks 18{19 public partial class Form13 : Form 20 {21 private Panel printPanel;22 23 private Label[] labelArrayA = new Label[3];24 private Label[,] labelArrayB = new Label[10,9];25 26 [STAThread]27 static void Main()28 {29 Application.EnableVisualStyles();30 Application.SetCompatibleTextRenderingDefault(false);31 Application.Run(new Form13());32 }33 34 35 public Form13()36 {37 InitializeComponent();38 Text = "印刷";39 Size = new System.Drawing.Size(1010, 600);40 BackColor = Color.White;41 42 printPanel = new Panel // 印刷範囲43 {44 Name = "printerPanel",45 Location = new System.Drawing.Point(0, 0),46 Size = new System.Drawing.Size(800, 540),47 BackColor = Color.White 48 49 };50 this.Controls.Add(printPanel); 51 52 Panel panel_a = new Panel 53 {54 Name = "panel_a",55 Location = new System.Drawing.Point(30, 0),56 Size = new System.Drawing.Size(800, 40),57 BackColor = Color.White 58 };59 printPanel.Controls.Add(panel_a);60 61 62 Panel panel_b = new Panel 63 {64 Name = "panel_a",65 Location = new System.Drawing.Point(30, 40),66 Size = new System.Drawing.Size(800, 500),67 BackColor = Color.Black 68 };69 printPanel.Controls.Add(panel_b);70 71 72 string strA = "日 付:2022年12月31日";73 74 var lbl_aa = new Label 75 {76 Location = new Point(0, 5),77 Width = 180,78 Height = 25,79 Font = new Font("BIZ UDGothic", 12),80 Text = strA,81 TextAlign = ContentAlignment.MiddleLeft,82 BackColor = Color.White,83 ForeColor = Color.Black 84 };85 labelArrayA[0] = lbl_aa;86 panel_a.Controls.Add(lbl_aa);87 88 89 strA = "1234567891";90 91 var lbl_ab = new Label 92 {93 Location = new Point(200, 5),94 Width = 160,95 Height = 25,96 Font = new Font("BIZ UDGothic", 12),97 Text = strA,98 TextAlign = ContentAlignment.MiddleLeft,99 BackColor = Color.White,100 ForeColor = Color.Black 101 };102 labelArrayA[1] = lbl_ab;103 panel_a.Controls.Add(lbl_ab);104 105 106 strA = "*****";107 108 var lbl_ac = new Label 109 {110 Location = new Point(360, 5),111 Width = 120,112 Height = 25,113 Font = new Font("BIZ UDGothic", 12),114 Text = strA,115 TextAlign = ContentAlignment.MiddleRight,116 BackColor = Color.White,117 ForeColor = Color.Black 118 };119 labelArrayA[2] = lbl_ac;120 panel_a.Controls.Add(lbl_ac);121 122 var lbl_ad = new Label 123 {124 Location = new Point(650, 5),125 Width = 150,126 Height = 25,127 Font = new Font("BIZ UDGothic", 18),128 Text = "プリンター",129 TextAlign = ContentAlignment.MiddleRight,130 BackColor = Color.White,131 ForeColor = Color.Black 132 };133 panel_a.Controls.Add(lbl_ad);134 135 string[] titleArray = { "タイトル1", "タイトル2", "タイトル3", "タイトル4", "タイトル5" };136 string[] lastTitle = { "000000", "", "Total", "", "000000"};137 int[] widthArray = { 120, 200, 300, 200, 120};138 139 int xStandardPosition = 1;140 int yStandardPosition = 1;141 int xPos = xStandardPosition;142 int yPos = yStandardPosition;143 int xSpace = 1;144 int ySpace = 1;145 int height = 50;146 int maxWidth = 0;147 ContentAlignment align = ContentAlignment.MiddleCenter;148 149 150 for (int i = 0; i < 10; i++)151 {152 for (int j = 0; j < titleArray.Length; j++)153 {154 if (i == 0) // 先頭行のタイトル155 {156 strA = titleArray[j];157 align = ContentAlignment.MiddleCenter;158 }159 160 else if (i == 9) // 最終行161 {162 strA = lastTitle[j];163 align = ContentAlignment.MiddleCenter;164 }165 166 else167 {168 strA = "テスト";169 if (j == 0 || j == 4)170 { align = ContentAlignment.MiddleRight; }171 else { align = ContentAlignment.MiddleLeft; }172 }173 174 var lbl_ba = new Label 175 {176 Location = new Point(xPos, yPos),177 Width = widthArray[j],178 Height = height,179 Font = new Font("BIZ UDGothic", 14),180 Text = strA,181 TextAlign = align,182 BackColor = Color.White,183 ForeColor = Color.Black 184 };185 labelArrayB[i,j] = lbl_ba;186 panel_b.Controls.Add(lbl_ba);187 xPos += widthArray[j] + xSpace;188 }189 maxWidth = xPos;190 xPos = xStandardPosition;191 yPos += height + ySpace;192 }193 panel_b.Size = new System.Drawing.Size(maxWidth,yPos);194 printPanel.Size = new System.Drawing.Size(maxWidth+30,yPos+40);195 PanelPrinting();196 197 } // public Form13 の最後198 199 200 201 private void PanelPrinting()202 {203 PrintDocument pd = new PrintDocument();204 205 pd.PrintPage += (s, ev) =>206 {207 // printPanelの部分だけを印刷する208 Bitmap bmp = new Bitmap(printPanel.Width, printPanel.Height);209 printPanel.DrawToBitmap(bmp, new Rectangle(0, 0, printPanel.Width, printPanel.Height));210 ev.Graphics.DrawImage(bmp, 0, 0);211 };212 213 PrintDialog printDialog = new PrintDialog();214 printDialog.Document = pd;215 216 if (printDialog.ShowDialog() == DialogResult.OK)217 {218 pd.Print();219 }220 }221 222 } // public partial class Form13 : Form の最後223} // namespaceの最後

コメントを投稿

0 コメント