Created
May 16, 2015 14:21
-
-
Save CodeZombieCH/b9def0b0d9c41a98593a to your computer and use it in GitHub Desktop.
WinForm: Get available caption size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.Windows.Forms; | |
namespace WindowsFormsApplication1 | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
UpdateStats(); | |
} | |
private void UpdateStats() | |
{ | |
var availableWidth = CalcAvaliableCaptionWidth(); | |
var textWidth = TextRenderer.MeasureText(captionTextTextBox.Text, SystemFonts.CaptionFont).Width; | |
availableSpaceTextBox.Text = availableWidth.ToString(); | |
captionTextWidthTextBox.Text = textWidth.ToString(); | |
statusPanel.BackColor = (textWidth > availableWidth ? Color.Red : Color.Green); | |
} | |
/// <summary> | |
/// Calculates an approximation of the available caption width | |
/// Depends on OS and theme | |
/// </summary> | |
/// <returns>Width</returns> | |
private int CalcAvaliableCaptionWidth() | |
{ | |
return | |
// Form width | |
Width | |
// Icon | |
- (Icon == null ? 0 : Icon.Width) | |
// Minimize button (26 on Win8) | |
- (MinimizeBox ? SystemInformation.CaptionButtonSize.Width : 0) | |
// Maximize button (26 on Win8) | |
- (MaximizeBox ? SystemInformation.CaptionButtonSize.Width : 0) | |
// Close button (45 on Win8) | |
- SystemInformation.CaptionButtonSize.Width; | |
} | |
private void captionTextTextBox_TextChanged(object sender, EventArgs e) | |
{ | |
Text = captionTextTextBox.Text; | |
UpdateStats(); | |
} | |
#region Windows Form Designer generated code | |
/// <summary> | |
/// Required designer variable. | |
/// </summary> | |
private System.ComponentModel.IContainer components = null; | |
/// <summary> | |
/// Clean up any resources being used. | |
/// </summary> | |
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> | |
protected override void Dispose(bool disposing) | |
{ | |
if(disposing && (components != null)) | |
{ | |
components.Dispose(); | |
} | |
base.Dispose(disposing); | |
} | |
/// <summary> | |
/// Required method for Designer support - do not modify | |
/// the contents of this method with the code editor. | |
/// </summary> | |
private void InitializeComponent() | |
{ | |
this.captionTextTextBox = new System.Windows.Forms.TextBox(); | |
this.label1 = new System.Windows.Forms.Label(); | |
this.label2 = new System.Windows.Forms.Label(); | |
this.availableSpaceTextBox = new System.Windows.Forms.TextBox(); | |
this.label3 = new System.Windows.Forms.Label(); | |
this.captionTextWidthTextBox = new System.Windows.Forms.TextBox(); | |
this.statusPanel = new System.Windows.Forms.Panel(); | |
this.label4 = new System.Windows.Forms.Label(); | |
this.SuspendLayout(); | |
// | |
// captionTextTextBox | |
// | |
this.captionTextTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | |
| System.Windows.Forms.AnchorStyles.Right))); | |
this.captionTextTextBox.Location = new System.Drawing.Point(12, 31); | |
this.captionTextTextBox.Name = "captionTextTextBox"; | |
this.captionTextTextBox.Size = new System.Drawing.Size(294, 20); | |
this.captionTextTextBox.TabIndex = 0; | |
this.captionTextTextBox.TextChanged += new System.EventHandler(this.captionTextTextBox_TextChanged); | |
// | |
// label1 | |
// | |
this.label1.AutoSize = true; | |
this.label1.Location = new System.Drawing.Point(12, 15); | |
this.label1.Name = "label1"; | |
this.label1.Size = new System.Drawing.Size(87, 13); | |
this.label1.TabIndex = 1; | |
this.label1.Text = "Your caption text"; | |
// | |
// label2 | |
// | |
this.label2.AutoSize = true; | |
this.label2.Location = new System.Drawing.Point(12, 54); | |
this.label2.Name = "label2"; | |
this.label2.Size = new System.Drawing.Size(123, 13); | |
this.label2.TabIndex = 3; | |
this.label2.Text = "Available space (approx)"; | |
// | |
// availableSpaceTextBox | |
// | |
this.availableSpaceTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | |
| System.Windows.Forms.AnchorStyles.Right))); | |
this.availableSpaceTextBox.Location = new System.Drawing.Point(12, 70); | |
this.availableSpaceTextBox.Name = "availableSpaceTextBox"; | |
this.availableSpaceTextBox.ReadOnly = true; | |
this.availableSpaceTextBox.Size = new System.Drawing.Size(294, 20); | |
this.availableSpaceTextBox.TabIndex = 2; | |
// | |
// label3 | |
// | |
this.label3.AutoSize = true; | |
this.label3.Location = new System.Drawing.Point(12, 93); | |
this.label3.Name = "label3"; | |
this.label3.Size = new System.Drawing.Size(71, 13); | |
this.label3.TabIndex = 5; | |
this.label3.Text = "Caption width"; | |
// | |
// captionTextWidthTextBox | |
// | |
this.captionTextWidthTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | |
| System.Windows.Forms.AnchorStyles.Right))); | |
this.captionTextWidthTextBox.Location = new System.Drawing.Point(12, 109); | |
this.captionTextWidthTextBox.Name = "captionTextWidthTextBox"; | |
this.captionTextWidthTextBox.ReadOnly = true; | |
this.captionTextWidthTextBox.Size = new System.Drawing.Size(294, 20); | |
this.captionTextWidthTextBox.TabIndex = 4; | |
// | |
// statusPanel | |
// | |
this.statusPanel.Location = new System.Drawing.Point(12, 148); | |
this.statusPanel.Name = "statusPanel"; | |
this.statusPanel.Size = new System.Drawing.Size(294, 40); | |
this.statusPanel.TabIndex = 6; | |
// | |
// label4 | |
// | |
this.label4.AutoSize = true; | |
this.label4.Location = new System.Drawing.Point(12, 132); | |
this.label4.Name = "label4"; | |
this.label4.Size = new System.Drawing.Size(90, 13); | |
this.label4.TabIndex = 7; | |
this.label4.Text = "Fit status (approx)"; | |
// | |
// Form1 | |
// | |
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); | |
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; | |
this.ClientSize = new System.Drawing.Size(318, 200); | |
this.Controls.Add(this.label4); | |
this.Controls.Add(this.statusPanel); | |
this.Controls.Add(this.label3); | |
this.Controls.Add(this.captionTextWidthTextBox); | |
this.Controls.Add(this.label2); | |
this.Controls.Add(this.availableSpaceTextBox); | |
this.Controls.Add(this.label1); | |
this.Controls.Add(this.captionTextTextBox); | |
this.Name = "Form1"; | |
this.Text = "Form1"; | |
this.ResumeLayout(false); | |
this.PerformLayout(); | |
} | |
private System.Windows.Forms.TextBox captionTextTextBox; | |
private System.Windows.Forms.Label label1; | |
private System.Windows.Forms.Label label2; | |
private System.Windows.Forms.TextBox availableSpaceTextBox; | |
private System.Windows.Forms.Label label3; | |
private System.Windows.Forms.TextBox captionTextWidthTextBox; | |
private System.Windows.Forms.Panel statusPanel; | |
private System.Windows.Forms.Label label4; | |
#endregion | |
[STAThread] | |
static void Main() | |
{ | |
Application.EnableVisualStyles(); | |
Application.SetCompatibleTextRenderingDefault(false); | |
Application.Run(new Form1()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment