0

In an MDI application, when the MDI child form is maximised the icon for the form is used as the context menu icon, displayed on the left of the parent form's MenuStrip. When I set the icon of a form used in an MDI application to something larger than 16x16 (I'm using 32x32) the icon is drawn unscaled, and the menu is resized to suit. Note that if the ICO file also contains a 16x16 version of the icon it works fine.

The following creates a basic application that shows the behaviour:

  • create a new solution and WinForms project
  • change Form1's IsMdiContainer to true
  • add a MenuStrip to Form1
  • create a new form, call it MdiChildForm
  • set MdiChildForm's icon to a 32x32 ICO file, here's one I prepared earlier
  • in Form1 add a menu item, double-click it to create the Click event handler and add the following:

    var child = new MdiChildForm();
    child.MdiParent = this;
    child.Show();
    child.WindowState = FormWindowState.Maximized;
    
  • build and run, click the menu item

Note that when you click the menu item again the icon changes to the default one, this is described in this question and isn't the issue here.

I'm pretty sure this would be described as 'by design' but it is annoying nonetheless. Is there a way to force the context menu icon to scale down, sort of resizing the source icon? I'll probably do that as well, but I'm after some kind of in-code catch-all.

Community
  • 1
  • 1
Rebecca Scott
  • 2,421
  • 2
  • 25
  • 39

3 Answers3

3

Inspired by @Jeremy Thompson's answer I found a possible workaround. I take the oversized icon, draw it to a new 16x16 bitmap, create a new icon from that, and assign it back to the child form. The new icon needs to be assigned prior to showing the form.

This code is barely tested, and probably buggy and leaky, and I am not an expert on GDI so beware it may break stuff:

var bmp = new Bitmap(16, 16);
using (var g = Graphics.FromImage(bmp))
{
    g.DrawImage(child.Icon.ToBitmap(), new Rectangle(0, 0, 16, 16));
}
var newIcon = Icon.FromHandle(bmp.GetHicon());
child.Icon = newIcon;
// ...
child.Show();
Rebecca Scott
  • 2,421
  • 2
  • 25
  • 39
1

I tried various things and the only way I got it working was by using a 16x16 sized icon, in addition to the forms standard 32x32 icon:

var child = new MdiChildForm();
child.MdiParent = this;
child.Icon = new System.Drawing.Icon("scanner_magnifier_16x16.ico", new Size(16, 16));    
child.Show();
child.WindowState = FormWindowState.Maximized;
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
0

I faced the same problem. But seem it was successfully solved by simply adding additional 16x16 icon image into *.ICO file.

Used Axialis AX-Icons I did following: 1) Open *.ICO file, click Ctrl+A to select full icon image, click Ctrl+C (to copy into clipboard) 2) Click "New image format", choose 16x16, choose suitable colors for this *.ICO file 3) Click Ctrl+V (paste), choose "Resize image to fit the editor area" 4) Click "Save" 5) Repeat the same for each *.ICO file used by MDI forms in my application 6) Then opened every MDI form in my application, clicked "Icon" property for the Form and choose to use new version of the same *.ICO file

As I can see now it works fine. In menu menu it use small icon, so size of main menu is no longer jumps when MDI form is maximized.

dmitry_bond
  • 378
  • 1
  • 3
  • 15