Windows Develop Bookmark and Share   
 index > Windows Forms General > ToolStrip: problem with DPI scaling
 

ToolStrip: problem with DPI scaling

Hi

I've got an application with several MDI-form, which are created dynamically at runtime. Every child-window has a toolstrip with a label and some buttons in it.

The application has to adjust to the users DPI-setting. The AutoScaleMode property is fine, but as i see it, it doesn't work for the dynamically created MDIs. Because of this i scale them by myself with the Scale method. This works quite good for everything but the ToolStrip. The toolstrip scales only the buttons that are visible (not in overflow).

This is really annoying, and till now, i couldn't find a workaround. Hope someone can help me here.

It's easy to reproduce:

  1. create a new windows application
  2. add a toolstrip and a button to your form
  3. let your code look like this:

public Form1()

{

InitializeComponent();

Button bn1 = new Button();

bn1.Height = 22;

bn1.Width = 85;

bn1.Text = "Test1";

ToolStripControlHost tsch1 = new ToolStripControlHost(bn1);

tsch1.AutoSize = false;

toolStrip1.Items.Add(tsch1);

Button bn2 = new Button();

bn2.Height = 22;

bn2.Width = 85;

bn2.Text = "Test2";

ToolStripControlHost tsch2 = new ToolStripControlHost(bn2);

tsch2.AutoSize = false;

toolStrip1.Items.Add(tsch2);

}

private void button1_Click(object sender, EventArgs e)

{

this.Scale(new SizeF(1.5F, 1.5F));

}


Now just start the app, resize the window so that one button gets pushed in the overflow-menu. Press the scale-button and resize the window again. You'll see that only the visible button got scaled!

Anybody got an idea how to get around this behaviour?

FaWei  Thursday, November 16, 2006 10:57 AM
I found a workaround. I inherited my own ToolStrip-class and overwrite the ScaleControl method:

protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
{
base.ScaleControl(factor, specified);

IEnumerator eItems = this.Items.GetEnumerator();
while (eItems.MoveNext())
{
object o = eItems.Current;
if (o is ToolStripControlHost)
{
if (((ToolStripControlHost)o).Placement == ToolStripItemPlacement.Overflow)
((ToolStripControlHost)o).Control.Scale(factor);
}
}

}


Hope this helps someone else.
But I'm still curious if this is a bug, or i did something wrong...
FaWei  Friday, November 17, 2006 2:12 PM
I found a workaround. I inherited my own ToolStrip-class and overwrite the ScaleControl method:

protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
{
base.ScaleControl(factor, specified);

IEnumerator eItems = this.Items.GetEnumerator();
while (eItems.MoveNext())
{
object o = eItems.Current;
if (o is ToolStripControlHost)
{
if (((ToolStripControlHost)o).Placement == ToolStripItemPlacement.Overflow)
((ToolStripControlHost)o).Control.Scale(factor);
}
}

}


Hope this helps someone else.
But I'm still curious if this is a bug, or i did something wrong...
FaWei  Friday, November 17, 2006 2:12 PM

You can use google to search for other answers

Custom Search

More Threads

• UserControl notifications
• Label AutoSize Property Problem
• How to set cursor position ?
• Getting URL info using WebClient
• Selecting a component library
• FormClosing e.Cancel always true
• ListView.Columns.AddCol(strText, nDataType, nWidth)
• multiline problem
• Automating Office from a Webserver
• Custom MessageBox-like sizing of text rectangle