惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
罗磊的独立博客
小众软件
小众软件
T
Tailwind CSS Blog
宝玉的分享
宝玉的分享
博客园_首页
N
Netflix TechBlog - Medium
B
Blog
Recent Announcements
Recent Announcements
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
L
LangChain Blog
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
Last Week in AI
Last Week in AI
Jina AI
Jina AI
V
V2EX
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
博客园 - 【当耐特】

博客园 - 清雷

湘江老厨聚餐 我的青春 今日感想 东莞一日游 HP MC/SG 双机安装心得 周末的狂想 汉字转拼音的例子 回忆大学 Asp.Net下的DataGrid的多层表头 VS无法调试智能设备程序的解决方案 VS.Net下的新的单元测试工具-TestDriven.NET 怀念我的大学(2) 显示主窗体前显示登陆窗口的完美解决方案 windows消息大全 在.NET 中使用WIN32 API的宝库 一个封装的异步Socket客户端 一个很好的C#题目 开发.NET CF 蓝牙的通信模块 怀念我的大学(1)
设置窗体背景图片,并且让图片随着窗体的大小的调整而调整大小
清雷 · 2005-09-14 · via 博客园 - 清雷

         在一个窗体中设置窗体的背景图片后,如果窗体大小改变时,图片并不会随之发生变化,下面就是我的解决方案:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication9
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;
        
private Image image;
        
private RectangleF rect;

        
public Form1()
        
{
            image 
= Image.FromFile(@"G:\我的文档\My Pictures\frl1.jpg");
            InitializeComponent();
            
this.BackgroundImage = image;
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
#region Windows 窗体设计器生成的代码
        
/// <summary>
        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
        
/// 此方法的内容。
        
/// </summary>

        private void InitializeComponent()
        
{
            
// 
            
// Form1
            
// 
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
            
this.ClientSize = new System.Drawing.Size(616446);
            
this.Name = "Form1";
            
this.Text = "Form1";
            
this.Resize += new System.EventHandler(this.Form1_Resize);
            
this.Load += new System.EventHandler(this.Form1_Load);
            
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

        }

        
#endregion


        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }


        
private void Form1_Load(object sender, System.EventArgs e)
        
{
            
try
            
{
                rect 
= new RectangleF(0,0,this.ClientSize.Width,this.ClientSize.Height);
            }

            
catch(Exception ex)
            
{
                MessageBox.Show(ex.Message);
            }

        
        }

        
private void DrawBackgroudImage()
        
{
            
try
            
{
                Graphics g 
= this.CreateGraphics();
                g.DrawImage(image,rect);
                g.Dispose();
            }

            
catch(Exception e)
            
{
                MessageBox.Show(e.Message);
            }

        }


        
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        
{            
            DrawBackgroudImage();
        
        }


        
private void Form1_Resize(object sender, System.EventArgs e)
        
{
            rect 
= new RectangleF(0,0,this.ClientSize.Width,this.ClientSize.Height);
            DrawBackgroudImage();        
        }

        
    }

}