這是完全清楚爲什麼這個問題發生。 MDIClient對象不支持ImageLayout.Stretch。這是記錄。要真正做到這一點是一個巨大的痛苦。嘗試(根據需要編輯)從一些類似的繼承,你的MDI形式:
public class MdiForm : System.Windows.Forms.Form
{
private static readonly float _bg_scale = FormGraphics.mdi_background.Width/(float)FormGraphics.mdi_background.Height;
private MdiClient _mdi_client = null;
private Image _background_cache = null;
public MdiForm()
{
SetStyle(
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer, true);
Shown += MdiForm_Shown;
SizeChanged += MdiForm_SizeChanged;
IsMdiContainer = true;
}
private void MdiForm_Shown(object sender, EventArgs eventArgs)
{
foreach (MdiClient control in Controls.OfType<MdiClient>())
{
_mdi_client = control;
control.Paint += MdiClient_Paint;
control.BackColor = Color.White;
//LA LA LA I CAN'T HEAR YOU
//this DOES work and IS required to avoid flicker
MethodInfo mInfoMethod = typeof(MdiClient).GetMethod(
"SetStyle",
BindingFlags.Instance | BindingFlags.NonPublic,
Type.DefaultBinder,
new[] { typeof(ControlStyles), typeof(bool) },
null);
mInfoMethod.Invoke(control, new object[] {
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer, true });
}
}
private void MdiClient_Paint(object sender, PaintEventArgs e)
{
if(_background_cache == null) { _background_cache = new Bitmap(FormGraphics.mdi_background, (int)(Height * _bg_scale), Height); }
e.Graphics.DrawImageUnscaled(_background_cache, Point.Empty);
}
private void MdiForm_SizeChanged(object sender, EventArgs e)
{
if (_background_cache != null) { _background_cache.Dispose(); }
_background_cache = null;
if (_mdi_client != null) { _mdi_client.Invalidate(); }
}
}
你對你自己來處理這裏的錯誤,很明顯。
無法重新制作它。 MessageBox彈出嗎?圖像是否有效? – LarsTech 2013-03-27 18:44:45
是MessageBox彈出並且圖像也是有效的。路徑設置得很好。你能夠設置背景圖片嗎?即使我打印圖像的大小,它也能很好地打印尺寸。 – Volatil3 2013-03-27 18:49:52
我在窗體上放置了兩個面板,它們都顯示了它們的背景圖像。 – LarsTech 2013-03-27 18:51:48