I am writing a WinForms program that prints text from the label on the pictureBox. Made it in two ways:
1) Dynamically creating a label and pictureBox
2) Statically creating them
The main parameters were set the same, but the results were different. Below is the code of the first and second methods (I’ll still throw the private void InitializeComponent() from Form1.Designer.cs so that you can clearly see that the properties are set the same):
one)
public Form1() { InitializeComponent(); Size = new Size(500, 500); var text = "Hello world"; var font = new Font("Times New Roman", 20); var pictureBox = new PictureBox() { Parent = this, BackColor = SystemColors.ControlLightLight, Size = new Size(300, 300), Location = new Point(50, 50) }; var label = new Label { Parent = pictureBox, Text = text, BackColor = SystemColors.Control, AutoSize = true, Font = font, Location = new Point(60, 60) }; label.MouseDown += (o, e) => { label.UseCompatibleTextRendering = !label.UseCompatibleTextRendering; }; pictureBox.MouseDown += (o, e) => { label.Visible = !label.Visible; }; pictureBox.Paint += (o, e) => { if (label.UseCompatibleTextRendering) e.Graphics.DrawString(label.Text, label.Font, new SolidBrush(label.ForeColor), label.Location); else TextRenderer.DrawText(e.Graphics, label.Text, label.Font, label.Location, label.ForeColor); }; } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Text = "Form1"; } 2)
public Form1() { InitializeComponent(); label.MouseDown += (o, e) => { label.UseCompatibleTextRendering = !label.UseCompatibleTextRendering; }; pictureBox.MouseDown += (o, e) => { label.Visible = !label.Visible; }; pictureBox.Paint += (o, e) => { if (label.UseCompatibleTextRendering) e.Graphics.DrawString(label.Text, label.Font, new SolidBrush(label.ForeColor), label.Location); else TextRenderer.DrawText(e.Graphics, label.Text, label.Font, label.Location, label.ForeColor); }; } private void InitializeComponent() { this.pictureBox = new System.Windows.Forms.PictureBox(); this.label = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); this.SuspendLayout(); // // pictureBox // this.pictureBox.BackColor = System.Drawing.SystemColors.ControlLightLight; this.pictureBox.Location = new System.Drawing.Point(50, 50); this.pictureBox.Name = "pictureBox"; this.pictureBox.Size = new System.Drawing.Size(300, 300); this.pictureBox.TabIndex = 0; this.pictureBox.TabStop = false; // // label // this.label.AutoSize = true; this.label.Font = new System.Drawing.Font("Times New Roman", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); this.label.Location = new System.Drawing.Point(60, 60); this.label.Name = "label"; this.label.Size = new System.Drawing.Size(146, 31); this.label.TabIndex = 1; this.label.Text = "Hello world"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(500, 500); this.Controls.Add(this.label); this.Controls.Add(this.pictureBox); this.Name = "Form1"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } Here are the results of the first and second methods:
Why are the conclusions different?

