Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 设置MDI窗体的背景图像_C#_Mdi - Fatal编程技术网

C# 设置MDI窗体的背景图像

C# 设置MDI窗体的背景图像,c#,mdi,C#,Mdi,我使用的技术,它可以很好地改变背景颜色,但不是图像。我的图像比窗口小,所以我使用BackgroundImageLayout来拉伸,但没有任何区别 在MDI表单的构造函数中,我使用以下代码: InitializeComponent(); Image img = Image.FromFile("C:\\duk.jpg"); foreach (Control control in this.Controls) {

我使用的技术,它可以很好地改变背景颜色,但不是图像。我的图像比窗口小,所以我使用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;
                }
            }

这个问题发生的原因非常清楚。MDIClient对象不支持ImageLayout.Stretch。这是有案可查的。要真正做到这一点是一个巨大的痛苦。尝试从类似以下内容继承MDI表单(根据需要编辑):

公共类MdiForm:System.Windows.Forms.Form
{
私有静态只读浮点_bg_scale=FormGraphics.mdi_background.Width/(float)FormGraphics.mdi_background.Height;
私有mdi客户端_mdi_客户端=null;
私有映像_background_cache=null;
公共表格()
{
固定方式(
ControlStyles.UserPaint|
ControlStyles.AllPaintingWimPaint|
ControlStyles.OptimizedDoubleBuffer,true);
所示+=所示的MDI形式;
SizeChanged+=MdiForm_SizeChanged;
IsMdiContainer=true;
}
显示私有无效MdiForm_(对象发送方、事件参数、事件参数)
{
foreach(Controls.OfType()中的MdiClient控件)
{
_mdi_客户端=控件;
control.Paint+=MdiClient\u Paint;
control.BackColor=Color.White;
//啦啦啦啦,我听不见你说话
//这是可行的,需要避免闪烁
MethodInfo mInfoMethod=typeof(MdiClient).GetMethod(
“设置样式”,
BindingFlags.Instance | BindingFlags.NonPublic,
Type.DefaultBinder,
新[]{typeof(ControlStyles),typeof(bool)},
无效);
调用(控件,新对象[]){
ControlStyles.UserPaint|
ControlStyles.AllPaintingWimPaint|
ControlStyles.OptimizedDoubleBuffer,true});
}
}
私有void MdiClient_Paint(对象发送方,PaintEventArgs e)
{
如果(_background_cache==null){_background_cache=新位图(FormGraphics.mdi_background,(int)(高度*_bg_scale),高度);}
e、 Graphics.DrawImageUnscaled(_background_cache,Point.Empty);
}
私有无效MdiForm_SizeChanged(对象发送方,事件参数e)
{
如果(_background_cache!=null){u background_cache.Dispose();}
_后台缓存=空;
如果(_mdi_client!=null){u mdi_client.Invalidate();}
}
}

显然,这里的错误处理由您自己来完成。

我认为这段代码将帮助您纠正错误,在界面中设置回显图像及其填充属性

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;
    }

我不能再责备它了。消息框弹出了吗?图像有效吗?是消息框弹出,图像也有效。这条路很好。你能设置背景图像吗?即使我打印图像的大小,它也能很好地打印尺寸。我在表单上放置了两个面板,它们都显示了背景图像。我不明白你的意思?到目前为止,我只是添加了一个表单,使其成为MDI,设置BG映像并运行它。尚未添加任何子项。我只想设置MDI的背景。根据那篇文章,您没有使用MDI。也就是说,您有
IsMdiContainer=false
。你的代码让你在“控件”中循环,而不是MDI子项。如果只是容器,那么只需设置它:
this.BackgroundImage=img
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;
    }