我正在使用this技术,它可以很好地改变背景颜色而不是图像。我的图像大小比窗口小,所以我使用BackgroundImageLayout来拉伸,但它没有任何区别。
在我的MDI表单的构造函数中,我使用以下代码:
InitializeComponent();
Image img = Image.FromFile("C:\\duk.jpg");
foreach (Control control in this.Controls)
{
if (control is MdiClient)
{
control.BackgroundImageLayout = ImageLayout.Stretch;
control.BackgroundImage = System.Drawing.Image.FromFile("C:\\duk.jpg");
// control.BackColor = Color.AliceBlue;
//Properties.Resources.duk;
MessageBox.Show("MDI");
break;
}
}
答案 0 :(得分:0)
非常清楚为什么会出现这个问题。 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(); }
}
}
显然,你可以自行处理错误处理。
答案 1 :(得分:0)
我认为这段代码将帮助您进行更正,以在界面中设置背面图片及其填充属性...
private void MDIParent1_Load(object sender, EventArgs e)
{
BackgroundImage =
System.Drawing.Image.FromFile("C:\\Users\\sanoop\\Downloads\\2137969.png");
BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
}