简体   繁体   English

缩放父窗体的 MenuStrip 中最大化的 MDI 子级使用的图标

[英]Scaling the icon used by a maximised MDI child in the parent form's MenuStrip

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.在 MDI 应用程序中,当 MDI 子窗体最大化时,窗体的图标用作上下文菜单图标,显示在父窗体的 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.当我将在 MDI 应用程序中使用的表单的图标设置为大于 16x16(我使用的是 32x32)时,该图标未按比例绘制,并且菜单会调整大小以适应。 Note that if the ICO file also contains a 16x16 version of the icon it works fine.请注意,如果 ICO 文件还包含 16x16 版本的图标,它可以正常工作。

The following creates a basic application that shows the behaviour:以下创建了一个显示行为的基本应用程序:

  • create a new solution and WinForms project创建一个新的解决方案和 WinForms 项目
  • change Form1's IsMdiContainer to true将 Form1 的 IsMdiContainer 更改为true
  • add a MenuStrip to Form1MenuStrip添加到 Form1
  • create a new form, call it MdiChildForm创建一个新表单,命名为MdiChildForm
  • set MdiChildForm 's icon to a 32x32 ICO file, here's one I prepared earlierMdiChildForm的图标设置为 32x32 ICO 文件,这是我之前准备的
  • in Form1 add a menu item, double-click it to create the Click event handler and add the following:Form1中添加一个菜单项,双击它以创建Click事件处理程序并添加以下内容:

     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.我可能也会这样做,但我追求的是某种代码内包罗万象。

Inspired by @Jeremy Thompson's answer I found a possible workaround.受@Jeremy Thompson 的回答启发,我找到了一种可能的解决方法。 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.我采用超大图标,将其绘制到一个新的 16x16 bitmap,从中创建一个新图标,并将其分配回子窗体。 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:这段代码几乎没有经过测试,可能有漏洞和漏洞,而且我不是 GDI 专家,所以要小心它可能会破坏一些东西:

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();

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:除了 forms 标准 32x32 图标之外,我尝试了各种方法,唯一让它工作的方法是使用 16x16 大小的图标:

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;

I faced the same problem.我遇到了同样的问题。 But seem it was successfully solved by simply adding additional 16x16 icon image into *.ICO file.但似乎只需在 *.ICO 文件中添加额外的 16x16 图标图像即可成功解决。

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使用Axialis AX-Icons 我做了以下: 1)打开*.ICO文件,点击Ctrl+A到select完整图标图像,点击Ctrl+C(复制到剪贴板) 2)点击“新图像格式”,选择16x16,选择适合此 *.ICO 文件的 colors 3) 单击 Ctrl+V(粘贴),选择“调整图像大小以适合编辑器区域” 4) 单击“保存” 5) 对 MDI forms 中使用的每个 *.ICO 文件重复相同的操作我的应用程序 6) 然后打开我的应用程序中的每个 MDI 表单,单击表单的“图标”属性并选择使用相同 *.ICO 文件的新版本

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.在菜单菜单中它使用小图标,因此当 MDI 窗体最大化时,主菜单的大小不再跳跃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM