我已经尝试了所有我能得到的东西,但它就是行不通!有没有人解决过这个问题?
我可以把我的图像放在后面,但它只有在表单从不尝试调整大小时才有效(缩小是可以的,生长会留下空白的灰色空间......)
我已经尝试了所有我能得到的东西,但它就是行不通!有没有人解决过这个问题?
我可以把我的图像放在后面,但它只有在表单从不尝试调整大小时才有效(缩小是可以的,生长会留下空白的灰色空间......)
两种选择:
我尝试了 Form 的 resize 事件,它对我来说是这样的:
private void Main_Resize(object sender, EventArgs e)
{
this.BackgroundImage = Properties.Resources.stockandinventorymanagement;
}
将 RightToLeftLayout 属性设置为 FALSE
对于通过 Google 访问此页面的其他人,我在这里找到了解决此问题的方法。
通过将此代码放入表单的Initialize()
函数中,我能够获得正常工作的 MDI 容器背景图像,即使是WindowState
更改和调整表单大小:
// as this code occurs in the Form's "Initialize()" function, any references to
// "this." are to the Form instance itself (not the MDI control!)
MdiClient client = this.Controls.OfType<MdiClient>().First();
client.BackgroundImageLayout = this.BackgroundImageLayout;
client.Paint += ( s, e ) =>
e.Graphics.DrawImage( this.BackgroundImage, (s as MdiClient).ClientRectangle );
//Set this to repaint when the size is changed
typeof( Control ).GetProperty(
"ResizeRedraw",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance
).SetValue( client, true, null );
//set this to prevent flicker
typeof( Control ).GetProperty(
"DoubleBuffered",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance
).SetValue( client, true, null );
为了简单起见,我让 MDI 容器使用表单BackgroundImage
(无论如何它都隐藏了)和BackgroundImageLayout
属性,以便我可以在 Designer 中使用表单自己的属性轻松设置/修改这些设置,并将这些设置自动反映在设计器无法访问的 MDI 容器中。这也意味着我不必担心管理图像的垃圾收集,并且可以摆脱Using {}
在原始片段中找到的代码(这实际上会导致错误,因为图像本身由 Using 块处理,然后使表单崩溃一旦它试图显示现在丢失的图像)。
将 BackgroundImageLayout 属性更改为 Stretch。这将拉伸您的图像以始终填充背景空间。
试试这个代码:
重要提示:表单的属性'BackgroundImageLayout'必须等于代码
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is MdiClient Then
ctl.BackColor = Color.Yellow
ctl.BackgroundImageLayout = ImageLayout.Stretch
ctl.BackgroundImage = Image.FromFile("C:\Image.png")
End If
Next
谢谢你的想法!!!!只需更改面板的背景并将其放在代码c#下面就太容易了:
Form2 frm = new Form2();
frm.TopLevel = false;
panel1.Controls.Add(frm);
frm.Show();
这对于 vb.net
Dim frm = New Form2
frm .TopLevel = False
Panel1.Controls.Add(frm )
frm .Show()
private void MainMenu_Load(object sender, EventArgs e)
{
foreach (Control ctrlControl in this.Controls)
{
if (ctrlControl is MdiClient)
{
BackgroundImage = Properties.Resources.Mbck;
}
}
}
我认为这个想法是将背景图像添加到对象的实例中,而不是对象的静态定义。
考虑到这一点,我所做的是:
Me.picturebox1.image = image.fromfile("c:\filename.jpg")
Me.picturebox1.show()
它奏效了。
甚至可以通过不使用图片框和面板以及不是从磁盘而是从项目中的资源引用图像来使代码更流畅!
很久以前,我发现使用面板作为 MDI 容器可以轻松解决这个问题。