winform中对于form设置backgroundimage时会出现窗口无法显示的情况

    xiaoxiao2021-03-26  17

    在对一个已经成型的软件添加一个简单的欢迎界面是,偷懒只用一个不带边框的form来做,在主窗口初始化前显示该欢迎form,代码如下:

    public FrameWin() { WelcomeForm welcome = new WelcomeForm(); welcome.Show(); Thread.Sleep(2000); welcome.Close(); InitializeComponent(); } WelcomeFrom为欢迎界面窗口,当设置backgroundimage属性时,发现第一次引用资源,默认layout如下图,这时欢迎界面正常显示;

    但是当设置backgroundimagelayout属性为Zoom或者Stretch时,再打开软件运行,发现主程序界面初始化前的2s延时照常进行,但是这2s内丝毫看不到欢迎界面的影子,无奈改回backgroundimagelayout属性为tile时,仍看不到欢迎界面,邪了!!此时相关属性如下:

    为究其原因,就把这个form删掉重新创建,然后对比属性修改前后的Designer文件变化,记录如下,

    未设置backgroundimage时,form初始化信息如下,这时欢迎界面可以显示,只是显示一个窗口底色矩形而已:

    显示如下:

    代码如下:

    private void InitializeComponent() { this.SuspendLayout(); // // WelcomeForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(570, 427); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.Name = "WelcomeForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "WelcomeForm"; this.ResumeLayout(false); }第一次设置backgroundimage为一张资源时,form初始化信息如下,这时欢迎界面可以正常显示(偷懒自己p了个伪界面):

    显示如下:

    代码如下:

    private void InitializeComponent() { this.SuspendLayout(); // // WelcomeForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackgroundImage = global::MDIMonitor_CS.Properties.Resources.welcome; this.ClientSize = new System.Drawing.Size(570, 427); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.Name = "WelcomeForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "WelcomeForm"; this.ResumeLayout(false); }

    当保持backgroundimage不变,修改backgroundimagelayout为Zoom时,form初始化信息如下,这时欢迎界面不再显示:

    没显示就截不了图了:

    代码如下:

    private void InitializeComponent() { this.SuspendLayout(); // // WelcomeForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackgroundImage = global::MDIMonitor_CS.Properties.Resources.welcome; this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.ClientSize = new System.Drawing.Size(589, 426); this.DoubleBuffered = true; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.Name = "WelcomeForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "WelcomeForm"; this.ResumeLayout(false); }

    当保持backgroundimage不变,再修改backgroundimagelayout回Tile时,form初始化信息如下,这时欢迎界面不再显示:

    没显示就截不了图了:

    代码如下:

    private void InitializeComponent() { this.SuspendLayout(); // // WelcomeForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackgroundImage = global::MDIMonitor_CS.Properties.Resources.welcome; this.ClientSize = new System.Drawing.Size(589, 426); this.DoubleBuffered = true; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.Name = "WelcomeForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "WelcomeForm"; this.ResumeLayout(false); }

    对比以上三个不同内容,很容易看出在改变backgroundimagelayout时,初始化代码中会增加 this.DoubleBuffered = true;这句话,很明显是打开了双缓冲开关,但是一旦打开了却不再根据backgroundimagelayout的改变为tile而关闭,于是,手动设置 this.DoubleBuffered = false;

    这时欢迎界面又正常显示了,卧槽,真的好了,松了口气啊,虽然不能选择backgroundimagelayout为别的选项,但是至少能出来个欢迎了,开心!!显示如下:

    就总结了这一问题的出现和解决,这,难道是vs的bug?还是bug中的一个bug?拉倒吧,不求甚解

    转载请注明原文地址: https://ju.6miu.com/read-450215.html

    最新回复(0)